Graphs
An agent decides its own steps. Often that is exactly what you want. Sometimes it is not. If every support message must be cleaned up first, then answered, then filed, there is nothing for a model to decide about the order, and you should not pay a model to decide it.
A graph is how you fix the order yourself. Some nodes are plain Rust functions, some are agents, and troupekit runs them in the order you chain them.
Two plain steps
#![allow(unused)]
fn main() {
/// Collapses runs of spaces and trims the customer's message.
#[tool]
fn clean(input: String) -> String {
input.split_whitespace().collect::<Vec<_>>().join(" ")
}
/// Turns the drafted reply into a ticket line that starts with its length.
#[tool]
fn ticket(reply: String) -> String {
format!("[{} chars] {reply}", reply.chars().count())
}
}
These are the same #[tool] functions you met two lessons ago. In a graph they are not offered to a model; the graph calls them directly. Look at the argument names. clean takes input, the run's input. ticket takes reply. That name is the wiring: a function node reads the output of the node whose id matches its argument name.
The chain
#![allow(unused)]
fn main() {
let writer = Agent::new("reply")
.model(MODEL)
.instructions("Write a friendly two-sentence reply to the customer's message.")
.max_turns(2);
let report = Graph::new("triage")
.step("clean", clean)
.agent("reply", writer)
.step("ticket", ticket)
.limits(Limits {
run: Some(20_000),
..Limits::default()
})
.run_with(" my parcel is late and nobody answers ")
.await?;
}
Three nodes. clean reads the input. The agent node reply reads what clean wrote. ticket reads what reply wrote, because its argument is called reply. The writer is an Agent that was never built: the graph builds it, keeps its model and instructions, and takes over its storage and retries.
The spend limit sits on the graph, not on the agent. That is deliberate. An agent that brings its own limits or ledger into a graph makes build fail, because the graph would otherwise silently drop a cap you set.
Every step stays in the state
#![allow(unused)]
fn main() {
println!("ticket: {}", report.output().as_str().unwrap_or_default());
println!("clean: {}", report.state()["clean"]);
println!(
"steps: {}, cost: {} millionths of a dollar",
report.steps(),
report.usage().cost_usd_micros
);
}
output() is what the last node wrote. state() keeps the output of every node by its id, so you can read what clean produced after the run has finished. steps() and usage() tell you how much work it was.
Try it
cargo run -p troupekit --example course_graph
The ticket line starts with the reply's length in brackets, followed by a two-sentence reply. The clean line shows the message with the extra spaces gone. Only one of the three steps called a model, and the cost line shows it.
Why this matters later
A graph run can be checkpointed after every step. This graph keeps the default, Storage::memory(), which cannot resume a run, so nothing survives the process. That sounds like a detail now. In the advanced lessons it is the whole point: give the graph .checkpoints(Storage::sqlite(path)), and when a process dies halfway through a graph, the steps whose checkpoint was saved stay finished. A step that did its work but died before its checkpoint runs again on resume, so a step with side effects should use the idempotency key described in Tool context. For branches, human approval steps and joins, the Workflows and graphs page shows the TOML workflow format.
Next: Tracing.