Skip to content

Examples

Steven Enamakel edited this page Jun 29, 2026 · 3 revisions

Examples

The repository keeps runnable examples under examples/. Run all commands from the repository root.

Local Graph

cargo run --example basic_graph

The core shape is:

use tinyagents::graph::END;
use tinyagents::harness::message::Message;
use tinyagents::{GraphBuilder, NodeContext, NodeResult, Result};

#[derive(Clone, Debug)]
struct AgentState {
    messages: Vec<Message>,
    needs_tool: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
    let graph = GraphBuilder::<AgentState, AgentState>::overwrite()
        .add_node("agent", |mut state: AgentState, _ctx: NodeContext| async move {
            state.messages.push(Message::assistant("I should check the local tool."));
            Ok(NodeResult::Update(state))
        })
        .add_node("tool", |mut state: AgentState, _ctx: NodeContext| async move {
            state.messages.push(Message::tool("echo", "tool result"));
            state.needs_tool = false;
            Ok(NodeResult::Update(state))
        })
        .set_entry("agent")
        .add_conditional_edges(
            "agent",
            |state: &AgentState| {
                if state.needs_tool { "tool".to_string() } else { "done".to_string() }
            },
            [("tool", "tool"), ("done", END)],
        )
        .add_edge("tool", "agent")
        .compile()?;

    let run = graph
        .run(AgentState {
            messages: vec![Message::user("Can you use a tool?")],
            needs_tool: true,
        })
        .await?;

    println!("visited: {:?}", run.visited);
    Ok(())
}

Use this example when you want graph behavior without a live model provider.

OpenAI Chat

export OPENAI_API_KEY=...
cargo run --features openai --example openai_chat

This registers an OpenAiModel in AgentHarness, sets it as the default model, and sends one user message through the harness.

OpenAI-Backed Graph Agent

export OPENAI_API_KEY=...
cargo run --features openai --example openai_graph_agent

This composes the harness and graph runtime: a graph node invokes an OpenAI-backed harness, stores the answer in graph state, and then routes to END.

Tool Calling

export OPENAI_API_KEY=...
cargo run --features openai --example openai_tools

The harness exposes a local typed tool to the model. The model requests the tool, the harness executes it, and the tool result is fed back into the model loop.

Structured Output

export OPENAI_API_KEY=...
cargo run --features openai --example openai_structured

This demonstrates schema-constrained model output and the harness-side structured extraction path.

Model-Authored Blueprint

export OPENAI_API_KEY=...
cargo run --features openai --example openai_self_blueprint

The model emits .rag source, TinyAgents parses and compiles it, the registry binds referenced capabilities, and the resulting graph is executed through the same runtime as Rust-built graphs.

TinyAgents

Recursive language-model (RLM) harness for Rust.

Getting started

Concepts

Modules

Providers

Contributing


Clone this wiki locally