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

Build an offline assistant

Every application so far sent its prompts to a hosted model. Sometimes that is not allowed, or not possible. The data is private. The app has to work on a train. Or every call costing money is the wrong deal for a feature people use all day.

This lesson builds a help assistant whose model runs on the same machine as the program. Search happens in plain Rust, the answer comes from a GGUF model through llama.cpp, and the agent is marked private, so troupekit refuses any setup that would send its text to a cloud model.

Note

This lesson runs with llama.cpp on a desktop or server. Since 0.4, troupekit can also be called from Swift, Kotlin and Python. The Swift binding can use Apple Foundation Models through AppleFoundationModel, and the Kotlin binding a LiteRT-LM model file on Android through OnDeviceModel. The bindings are built from source; there is no SPM, Maven or PyPI package yet. The shape of this program stays the same; the model behind it changes.

Be honest about the model

The local catalog has one row in 0.4: smollm2-135m-instruct-q2k, a 135M-parameter model, 88 MB on disk. It is small enough for a phone, and much weaker than a hosted model. It will not plan a multi-step tool loop well. So we do not ask it to. We let plain Rust do the part that needs to be reliable, finding the right article, and ask the small model only to phrase an answer from it.

Search without a model

#![allow(unused)]
fn main() {
#[tool]
fn lookup(input: String) -> Result<String, String> {
    let docs = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/support/docs");
    let words: Vec<String> = input
        .split(|c: char| !c.is_alphanumeric())
        .filter(|w| w.len() > 3)
        .map(str::to_lowercase)
        .collect();
}

lookup splits the question into words longer than three letters, scores every help article by how many of those words it contains, and keeps the best one. It is crude, it is fast, and it behaves the same every time. It returns the question and the article together, because the next node in the graph reads only what this one returns.

A private agent on the device

#![allow(unused)]
fn main() {
async fn assistant(gguf: Option<&Path>) -> Result<BuiltGraph, KitError> {
    let answer = Agent::new("pocket")
        .instructions("Answer the question in two sentences, using only the help article.")
        .private(true)
        .max_turns(2)
        .max_tokens(1024);
    let answer = match gguf {
        Some(file) => answer.local(file.to_path_buf()),
        None => answer.local(CATALOG_MODEL).download(true),
    };
    Graph::new("pocket")
        .step("lookup", lookup)
        .agent("answer", answer)
        .build()
        .await
}
}

There is no .model(..) here, only .local(..), which makes the agent local only. It takes either a GGUF file you already have, or the catalog id with .download(true), which fetches the file once into the model cache and checks its SHA-256 before using it.

.private(true) is the promise. A private agent's model requests stay on the device, even on fallback, and build refuses a private agent that would send text to a cloud model another way. If you later give this agent a cloud knowledge embedder, or a judge eval that names a cloud model, the program fails at build instead of leaking. A cloud .model(..) added next to .local(..) is never planned for a private agent.

.private(true) covers tools too, as far as troupekit can see them: build refuses a private agent with web_fetch, web_search, an HTTP tool, a hosted tool, or any tool that reports uses_network. A tool you write counts as local unless it reports that, so a function of yours that calls the network still sends whatever the model puts in its arguments. This assistant has no network tools, and yours should not either.

Try it

This lesson needs the llama feature, which builds llama.cpp, so the first build takes a while and needs CMake and a C++ compiler.

cargo run -p troupekit --features llama --example course_app_offline -- "How long do I have to return an item?"

The first run downloads the 88 MB model into the cache; after that it starts from the cache and needs no network at all. To use a GGUF file you already have, set TROUPEKIT_TEST_GGUF to its path. A reasoning model such as Qwen3 thinks before it answers, which is why the agent leaves room for 1024 output tokens: a reply cut off inside its thinking is refused, not returned as an answer. The answer should be two sentences drawn from the return window article. Expect plain wording from a 135M model; the facts come from the article lookup found.

Where this goes next

The same program on a hosted model would be one changed line. Troupekit can also route each call between the device and the cloud by policy, and pick the cloud only when the network is up and the daily cloud budget allows it. Local models and routing covers route policies, tiers and network and device state.

That is the end of the course. You started with an empty function and a doc comment. You finished with a support agent, a pipeline that survives a kill, a team under one budget and an assistant that runs on the device.