Build a research pipeline that survives a kill
In the advanced level we killed a pipeline of plain functions. Real pipelines are made of agents, and agents are where the money and the minutes go. If a process dies after the first agent has done its work, paying for that work again is the expensive kind of waste.
course_app_research is a small report writer. One agent outlines, another drafts, plain Rust steps hold and save. We kill the process right after the outline, and a second process finishes the report.
Two agents, two plain steps
The agents are plain Agent values, not built yet, because the graph builds them:
#![allow(unused)]
fn main() {
fn outliner() -> Agent {
Agent::new("outliner")
.model(MODEL)
.instructions("List three short questions a report on the user's topic must answer, one per line, and nothing else.")
.max_turns(2)
.max_tokens(300)
}
}
The drafter looks the same, with its own instructions and a larger max_tokens. Between them sits a plain step that keeps a copy of the outline and, in the first process, ends it:
#![allow(unused)]
fn main() {
#[tool]
fn hold(outline: String, ctx: ToolContext) -> String {
std::fs::write(dir().join(format!("outline-{}.txt", mode())), &outline)
.expect("write outline copy");
if mode() == "crash" {
let run_id = ctx.run_id.expect("graph steps carry their run id");
std::fs::write(dir().join("run-id"), run_id.to_string()).expect("write run-id");
std::process::abort();
}
outline
}
}
Each process writes the outline it received to its own file, outline-crash.txt or outline-resume.txt. We use those two files as evidence at the end.
The pipeline
#![allow(unused)]
fn main() {
async fn pipeline(db: &Path) -> Result<BuiltGraph, KitError> {
Graph::new("research")
.agent("outline", outliner())
.step("hold", hold)
.agent("draft", drafter())
.step("save", save)
.checkpoints(Storage::sqlite(db))
.limits(Limits {
run: Some(30_000),
..Limits::default()
})
.build()
.await
}
}
Four nodes in a line, checkpointed into SQLite after each one, with one spend cap for the whole run. hold reads outline, the first agent's node. save reads draft, the second agent's node, and writes report.md.
Kill, resume, compare
The program runs itself twice, as in the advanced lesson: a first child that dies inside hold, then a second child that calls resume(run_id) on the same file. Then the parent compares the two outline copies:
#![allow(unused)]
fn main() {
let before = std::fs::read_to_string(folder.path().join("outline-crash.txt"))?;
let after = std::fs::read_to_string(folder.path().join("outline-resume.txt"))?;
println!("outline identical in both processes: {}", before == after);
}
Here is why that comparison means something. If the second process had asked the outline agent again, the model would have written a new outline, and two model replies to the same prompt are almost never word for word the same. An identical copy means the second process read the outline from the checkpoint. The outline agent ran once, and was paid for once.
Try it
cargo run -p troupekit --example course_app_research
You should see the first process end uncleanly, outline identical in both processes: true, and then the report: three questions as headings, each with a short answer.
What this buys you in production
A job that runs many agents in a row, such as research, document processing or a migration, can now be stopped at any point: by a deploy, a crash or you. No step whose checkpoint was saved is lost or paid for twice. Two habits keep it that way:
- Keep the graph the same between the process that starts a run and the one that resumes it. A changed graph is refused, because its checkpoints would mean something else.
- Treat the step that was in flight as one that may run twice. Here that is
hold, which is harmless. A step that sends or charges should use the idempotency key inToolContext.
For hosted runs on a job queue, with workers that claim and resume runs on their own, see Workflows and graphs.