Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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_resume example, 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 uses fullfsync on every commit, as Apple platforms need. Normal is 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 sqlcipher feature, 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

Elsewhere

LangGraph, Rig and others also resume runs; the comparison says what each needs for it.