troupekit-context 0.4.0

Memory scopes, chunking, embeddings and hybrid retrieval for the Troupekit LLM agent framework.
Documentation
<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/logo-dark.svg">
    <img src="https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/logo-light.svg" alt="troupekit" width="360">
  </picture>
</p>

<h3 align="center"><a href="https://chaoticqubit.github.io/troupekit/">Docs</a> · <a href="https://chaoticqubit.github.io/troupekit/install.html">Install</a> · <a href="https://github.com/ChaoticQubit/troupekit/tree/main/crates/troupekit/examples">Examples</a> · <a href="https://docs.rs/troupekit">API reference</a> · <a href="https://crates.io/crates/troupekit">crates.io</a></h3>

```sh
cargo add troupekit
cargo add tokio --features rt-multi-thread,macros
```

# Troupekit: a Rust framework for LLM agents

[![crates.io](https://img.shields.io/crates/v/troupekit?labelColor=231A2C&color=7A2848)](https://crates.io/crates/troupekit) [![docs](https://img.shields.io/badge/docs-guide-2A7058?labelColor=231A2C)](https://chaoticqubit.github.io/troupekit/) [![API](https://img.shields.io/docsrs/troupekit?label=API&labelColor=231A2C&color=2A7058)](https://docs.rs/troupekit) [![CI](https://img.shields.io/github/actions/workflow/status/ChaoticQubit/troupekit/ci.yml?branch=main&labelColor=231A2C)](https://github.com/ChaoticQubit/troupekit/actions/workflows/ci.yml?query=branch%3Amain) [![MSRV](https://img.shields.io/crates/msrv/troupekit?labelColor=231A2C&color=7A2848)](#supported-rust) [![license](https://img.shields.io/crates/l/troupekit?labelColor=231A2C&color=7A2848)](#license)

Troupekit is an open-source Rust framework for LLM agents that run inside your own program. You write an agent as a Rust function with `#[agent]`, or as a persona TOML file. You give it tools, memory and documents, and you can run it in graph workflows that survive a restart.

<p align="center"><img src="https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/readme/readme-workflow.gif" alt="Animated diagram of a workflow run through plan, approve, route and save steps, each checkpointed, to done. The code below parses the workflow with Workflow::parse, creates a GraphRun, enqueues it on troupe.runner and answers the human approval step with runner.resume(run_id, &quot;approve&quot;, json!(true))." width="720"></p>

## Why Troupekit

Everything lives in one Rust workspace, with no Python runtime: agents, workflows, retrieval, memory and tracing. Stores stay in memory until you ask for durability. Call `Troupe::builder().sqlite(path)` and every run checkpoints into one SQLite file, and a killed process can resume the run where it stopped.

<p align="center"><img src="https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/readme/readme-kill-resume.gif" alt="Animated diagram of the kill_resume example: a workflow of three tools where the first worker is killed with SIGKILL inside the second tool, a second worker finishes the run, and the result is the same as an uninterrupted run. The terminal shows the example's own output lines 2, 3 and 5." width="720"></p>

The model can be hosted or local. Troupekit calls Anthropic, OpenAI, Gemini and any OpenAI-compatible server, or runs a GGUF model on the device through llama.cpp. A route policy can pick between them for each call.

Your own documents are searchable by keyword, by vector or both, with HNSW and quantized indexes, all in the same SQLite file. CI builds the whole thing for Linux on x86_64 and aarch64, Windows, macOS, Android and iOS.

## Quick start

The quickest way to see it run is the bundled example, from a clone:

```sh
git clone https://github.com/ChaoticQubit/troupekit
cd troupekit
export OPENAI_API_KEY=sk-...
cargo run -p troupekit --example hello
```

The example is the whole program:

```rust
use troupekit::prelude::*;

/// Greet the user by name.
#[agent(model = "openai/gpt-6-luna")]
fn greeter() {}

#[tokio::main]
async fn main() -> Result<(), KitError> {
    println!("{}", greeter().run("I am Ada").await?);
    Ok(())
}
```

The doc comment becomes the agent's instructions. The model is a `vendor/model` selector from the built-in catalog in `crates/troupekit-providers/catalog/providers.toml`, and the key comes from `<VENDOR>_API_KEY`, here `OPENAI_API_KEY`.

`crates/troupekit/examples/` has more to try. `tool_agent` gives an agent a `#[tool]`. `kill_resume` runs a SQLite-backed workflow that resumes after its process is killed. `support_agent` answers customer questions from a folder of Markdown articles; it needs `--features knowledge`.

To use it in your own project, add it with the `cargo add` lines at the top, or in `Cargo.toml`:

```toml
[dependencies]
troupekit = "0.4"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
```

To try unreleased changes on `main`, depend on the repository instead: `troupekit = { git = "https://github.com/ChaoticQubit/troupekit", branch = "main" }`.

The command line tool is a separate crate. Install it with:

```sh
cargo install troupekit-cli
```

It has two commands. `troupekit ingest` builds a searchable index from a folder of documents, and `troupekit trace` shows a trace file in the terminal or as an HTML page.

<p align="center"><img src="https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/readme/readme-build-agent.gif" alt="Animated walkthrough that builds an agent step by step with Agent::new: instructions, a model, an add tool marked #[tool], memory, knowledge from a docs folder, a local GGUF model, a route policy, run limits and tracing, then runs it. The dependency line is cargo add troupekit --features knowledge,llama." width="720"></p>

## Crates

| Crate | What |
|---|---|
| `troupekit` | Entry crate. `use troupekit::prelude::*` for the macros and builders; `Troupe::builder()` wires in-memory or SQLite stores and hands out a runner; re-exports the engines |
| `troupekit-kit` | Ergonomic API: one builder or one macro per agent, tool and graph |
| `troupekit-macros` | The `#[agent]` and `#[tool]` attribute macros |
| `troupekit-core` | Messages, secrets, errors, events, ids, clock, storage traits and in-memory stores |
| `troupekit-providers` | Anthropic, OpenAI, Gemini and OpenAI-compatible wire protocols, provider catalog with prices, retry, fallback and cost metering |
| `troupekit-local` | On-device models: the `LocalModel` trait, a model catalog and llama.cpp |
| `troupekit-tools` | Tool trait, validating registry, SSRF-guarded web fetch, web search over Brave and SearXNG, declarative HTTP tools |
| `troupekit-context` | Memory scopes, profile facts, chunking, embeddings, hybrid retrieval and context blocks |
| `troupekit-index` | Vector indexes: exact brute force and HNSW, f16 and i8 quantization, persistable pages |
| `troupekit-agent` | Persona TOML agents: layered loader, checkpointable run, typed output, evals and LLM judge, budgets |
| `troupekit-graph` | TOML workflows of agent, tool, human, router, join and end nodes, with parallel branches, human pauses and retries |
| `troupekit-host` | Leased job queue, event log and a runner with worker and serverless step modes, crash-safe resume |
| `troupekit-store-sqlite` | One SQLite file for checkpoints, memory logs, blobs, the job queue and the usage ledger; optional SQLCipher |
| `troupekit-trace` | Zero-dependency trace spans, sinks and viewers |
| `troupekit-cli` | The `troupekit` command: `troupekit ingest <dir>` builds a searchable index in one SQLite file, `troupekit trace <file>` shows a trace |

## Feature flags

Features of the `troupekit` crate:

| Feature | Default | What |
|---|---|---|
| `kit` | yes | The `#[agent]` and `#[tool]` macros and the `Agent` and `Graph` builders |
| `sqlite` | yes | SQLite stores through `Troupe::builder().sqlite(path)` |
| `reqwest` | yes | The HTTP transport for cloud providers. Turn it off to build without the TLS stack |
| `knowledge` | no | Document loading, embeddings and hybrid search for agents |
| `sqlcipher` | no | Encrypted SQLite with a vendored OpenSSL |
| `sqlcipher-system` | no | Encrypted SQLite with the system OpenSSL |
| `local` | no | The `LocalModel` trait and model catalog |
| `llama` | no | llama.cpp models. Needs CMake and the llama.cpp source: run `cargo xtask fetch-llama` or set `TROUPEKIT_LLAMA_SRC` |

## Supported Rust

Rust 1.89 or newer for `troupekit`, `troupekit-kit`, `troupekit-store-sqlite` and `troupekit-cli`; 1.85 or newer for the other crates.

## Status and roadmap

Troupekit is at 0.3, before 1.0: the API can change between minor versions.

| Release | Theme | A user can |
|---|---|---|
| 0.1 | Survives a restart | Kill the app mid-run and resume from SQLite |
| 0.2 | Knows your docs | Search local documents by keyword, vector or hybrid, and remember facts |
| 0.3 (current) | Thinks offline | Run on a local GGUF model or switch to cloud by policy, and trace a run |
| 0.4 | In your app | Call Troupekit from Swift, Kotlin and Python, and use Apple and Android on-device models |
| 0.5 | Works in teams | Use team, crew and handoff patterns in TOML, parallel branches, and fork a run |
| 0.6 | Plugs in anywhere | Use MCP tools, and expose Troupekit as an MCP server |
| 0.7 | Proven | Prove workflow properties with Bend, replay runs deterministically and run evals |
| 1.0 | On-device core | Meet the 1.0 acceptance criteria on Linux, Windows, macOS, Android and iOS |

## Contributing

See [CONTRIBUTING.md](https://github.com/ChaoticQubit/troupekit/blob/main/CONTRIBUTING.md). Report security issues as described in [SECURITY.md](https://github.com/ChaoticQubit/troupekit/blob/main/SECURITY.md). Everyone taking part follows the [Code of Conduct](https://github.com/ChaoticQubit/troupekit/blob/main/CODE_OF_CONDUCT.md).

## License

Licensed under either of [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE), at your option. Unless you state otherwise, any contribution you submit for inclusion is dual licensed the same way, without any additional terms or conditions.