Durable runs
You start a long agent run. It has made four model calls, each one slow and paid for, and called two tools. Then the process dies: a deploy restarts the server, or the phone's OS kills the app in the background. A plain agent loop has lost everything, and the next attempt pays for the same calls again.
Troupekit treats a run as a state machine that moves one step at a time, and it saves a checkpoint after every step. Point an agent at the SQLite store, and a new process can pick up the run by its id and carry on from the last finished step.
In code
The agent below writes its checkpoints to a SQLite file. It comes from the kill_model_call example, where a parent process kills the agent in the middle of a local model call:
#![allow(unused)]
fn main() {
let built = Agent::new("storyteller")
.instructions("Write a very long story. /no_think")
.model("local/test")
.model_client(Arc::new(chat))
.max_tokens(MAX_TOKENS)
.checkpoints(Storage::custom(Arc::new(store)))
.events(Arc::new(PrintRun(AtomicBool::new(false))))
.trace([Trace::Models])
.trace_to(Arc::new(sink))
.build()
.await?;
}
A second process builds the same agent and continues the killed run by its id:
#![allow(unused)]
fn main() {
("resume", Some(id)) => {
let run_id: RunId = serde_json::from_str(&format!("\"{id}\""))?;
let text = built.resume(run_id).await?;
println!("RESUMED {}", text.len());
}
}
The kill a local model call guide walks through it, and kill and resume a workflow does the same for a graph run with SIGKILL inside a tool.
What it guarantees
- Every step is saved. A checkpoint is written after each step of the run state machine, so a resume repeats at most the step that was running when the process died, including every tool call or parallel branch in that step. Make those side effects safe to repeat.
- The same result as an uninterrupted run, in the example. The
kill_resumeexample, whose steps are deterministic, compares the resumed run with one that was never killed, on state, steps and usage, and exits with failure if they differ. A repeated model call can answer differently, so this is not a guarantee for model runs (source). - Commits survive power loss by default. The SQLite store defaults to
Durability::Full, which usesfullfsyncon every commit, as Apple platforms need.Normalis faster and can lose the last commits on power loss, never on a process kill (source). - Hosted runs are leased. In the job queue, a worker holds a lease on a run. When a killed worker's lease expires, another worker claims the job and steps on from the latest checkpoint.
- The file can be encrypted. With the
sqlcipherfeature, the store opens the file with SQLCipher (feature flags).
What it does not guarantee
A step that was running when the process died runs again. For a model call that means paying for it twice. For a tool it means the tool runs twice, which is harmless for a lookup and wrong for sending an email. Every tool call gets an idempotency_key in its ToolContext that stays the same when a step replays, so a tool with side effects can recognise the replay and skip it. Using that key is your tool's job; troupekit cannot undo a side effect. Tools shows it.
The default checkpoint storage of an agent is in memory, which survives nothing. Durability starts when you pass the SQLite store, or your own storage.
Proof
- Examples:
kill_resume.rsandkill_model_call.rs. - End-to-end tests:
killed_graph_resumes_in_a_new_processkills a worker that runs a real hosted model and checks that the resume kept the saved draft and ran the first step once across both processes;killed_local_graph_resumes_in_a_new_processdoes the same on a local GGUF model.
Elsewhere
LangGraph, Rig and others also resume runs; the comparison says what each needs for it.