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

Kill and resume a local model call

The workflow kill test kills a process between tool calls. This one is harder: it kills the process while a local model is in the middle of generating text, which is exactly what happens when a phone OS stops an app.

The kill_model_call example runs an agent on a local GGUF model with its checkpoints in SQLite. One process starts a long story and is killed during the model call. A second process then resumes the run by its id.

Run it

You need the llama and sqlite features. First fetch llama.cpp and the test model:

cargo xtask fetch-llama
cargo xtask fetch-test-model

cargo xtask fetch-test-model downloads the qwen3-0.6b-q4-k-m test model, checks its SHA-256 and prints its path in the model folder. The example itself has two commands:

usage: kill_model_call start <db> <gguf> | resume <db> <gguf> <run id>

start runs a fresh story. It prints RUN <id> on the first event, then GENERATING once the local model call starts, which is your cue to kill it. resume continues that run id from its checkpoint and prints RESUMED <bytes>:

cargo run -p troupekit --features llama,sqlite --example kill_model_call -- start kill.db <path to the gguf>
cargo run -p troupekit --features llama,sqlite --example kill_model_call -- resume kill.db <path to the gguf> <run id>

The integration test crates/troupekit/tests/kill_model_call.rs drives both halves for you and kills the first process.

The agent

Here is the agent both commands build. We load the model, wrap it as a chat model, open the store, and attach a trace sink that watches for the start of generation:

#![allow(unused)]
fn main() {
async fn agent(db: &Path, gguf: &Path) -> Result<BuiltAgent, Box<dyn Error>> {
    let model = LlamaModel::load(gguf, LlamaSettings::default().with_context(CONTEXT))?;
    let settings = LocalChatSettings::default().with_ban_eos(true);
    let chat = LocalChat::new(Arc::new(model), "local/test").with_settings(settings);
    let store = SqliteStore::open(db).await?;
    let said = AtomicBool::new(false);
    let sink = CallbackSink::new(move |line| {
        let local_start =
            line.contains("\"t\":\"start\"") && line.contains("\"name\":\"model.local\"");
        if local_start && !said.swap(true, Ordering::SeqCst) {
            println!("GENERATING");
            std::io::stdout().flush().expect("stdout flushes");
        }
    });
    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?;
    Ok(built)
}
}

Without help, the model could finish its answer before the kill arrives. Banning the end-of-text token makes generation run all the way to max_tokens (960), so the kill always lands mid call. The context is 1 024 tokens.

LlamaModel::load opens the GGUF file with llama.cpp, and LocalChat serves it as a chat Model under the selector local/test. Storage::custom keeps the checkpoints in the SQLite file, which is what lets BuiltAgent::resume(run_id) work from the second process. The CallbackSink listens on the Models trace category and prints GENERATING once, when the first model.local span starts.

Local models and routing covers local models in general.