<p align="center">
<h1 align="center">π¦ TraitClaw</h1>
<p align="center"><strong>A Rust AI Agent Framework β Simple by default, powerful when needed.</strong></p>
<p align="center">
<a href="https://crates.io/crates/traitclaw"><img src="https://img.shields.io/crates/v/traitclaw.svg" alt="crates.io"></a>
<a href="https://docs.rs/traitclaw"><img src="https://docs.rs/traitclaw/badge.svg" alt="docs.rs"></a>
<a href="https://github.com/traitclaw/traitclaw/actions"><img src="https://github.com/traitclaw/traitclaw/workflows/CI/badge.svg" alt="CI"></a>
<a href="https://github.com/traitclaw/traitclaw/blob/main/LICENSE-MIT"><img src="https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg" alt="License"></a>
<a href="https://github.com/traitclaw/traitclaw"><img src="https://img.shields.io/badge/MSRV-1.75-blue.svg" alt="MSRV"></a>
</p>
</p>
Build AI agents in Rust with **5 lines of code**. TraitClaw provides type-safe tools, streaming responses, persistent memory, multi-agent teams, reasoning strategies, and MCP integration β all with zero-cost abstractions and no garbage collector.
```rust
use traitclaw::prelude::*;
use traitclaw_openai_compat::OpenAiCompatProvider;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let agent = Agent::builder()
.provider(OpenAiCompatProvider::openai("gpt-4o-mini", api_key))
.system("You are a helpful assistant")
.build()?;
let output = agent.run("Hello!").await?;
println!("{}", output.text());
Ok(())
}
```
## Why TraitClaw?
- π§© **Composable Trait Architecture** β 8 core traits (`Provider`, `Tool`, `Memory`, `Guard`, `Hint`, `Tracker`, `ContextManager`, `OutputTransformer`) compose into any agent topology
- π§ **Type-Safe Tool Calling** β `#[derive(Tool)]` generates JSON schemas from Rust structs at compile time
- π§ **Multi-Strategy Reasoning** β Built-in ReAct, Chain-of-Thought, and MCTS strategies via `AgentStrategy` trait
- π₯ **Multi-Agent Teams** β Teams, routers, delegation, and verification chains out of the box
- π **MCP Integration** β Native Model Context Protocol client for tool discovery
- π‘ **First-Class Streaming** β `async Stream` with typed events, not string concatenation
- π **Production Observability** β `tracing` integration for structured logging and OpenTelemetry
- π¦ **Zero-Cost Abstractions** β No GC, no runtime, single static binary
## Quick Start
```bash
cargo add traitclaw traitclaw-openai-compat tokio anyhow
```
## Feature Matrix
| **Core** | Agent & Builder | β
|
| | 8 Core Traits | β
|
| | Tool System + `#[derive(Tool)]` | β
|
| | Real-Time Streaming | β
|
| | Structured Output (JSON β Rust) | β
|
| **Reasoning** | ReAct (ThinkβActβObserve) | β
|
| | Chain-of-Thought | β
|
| | Monte Carlo Tree Search | β
|
| | Custom Strategies | β
|
| **Providers** | OpenAI / GPT | β
|
| | Anthropic / Claude | β
|
| | OpenAI-Compatible (Ollama, Groq, Mistral, vLLM) | β
|
| **Memory** | In-Memory | β
|
| | SQLite Persistent | β
|
| | Compressed (LLM + Rule-Based) | β
|
| **Orchestration** | Multi-Agent Teams | β
|
| | Router Strategies (Sequential, Leader, Conditional) | β
|
| | Verification Chains | β
|
| | Agent Factory & Pool | β
|
| **Integration** | MCP Client (stdio + SSE) | β
|
| | RAG Pipeline (BM25, Embeddings, Hybrid) | β
|
| | Evaluation Framework | β
|
| **Safety** | Guards (Loop Detection, Rate Limit, Shell Deny) | β
|
| | Hints (Budget, System Prompt Reminder) | β
|
| | Trackers (Adaptive) | β
|
| **Observability** | `tracing` Integration | β
|
| | Lifecycle Hooks | β
|
| **Planned** | Benchmarks & Orchestration Strategy | π v1.1 |
| | Inter-Agent Contracts | π v1.2 |
| | Retry & Checkpoint Resilience | π v1.3 |
## Features
### π§ Type-Safe Tool Calling
```rust
#[derive(Tool)]
#[tool(description = "Search the web")]
struct WebSearch { query: String }
```
### π― Structured Output
```rust
#[derive(Deserialize, JsonSchema)]
struct Review { title: String, rating: u8 }
let review: Review = agent.run_structured("Review Inception").await?;
```
### π‘ Real-Time Streaming
```rust
let mut stream = agent.stream("Tell me a story");
while let Some(Ok(StreamEvent::TextDelta(text))) = stream.next().await {
print!("{text}");
}
```
### π‘οΈ Steering β Guards, Hints & Trackers
```rust
use traitclaw_steering::Steering;
let steering = Steering::auto(); // One-liner: guards + hints + tracking
```
### πΎ Persistent Memory
```rust
use traitclaw_memory_sqlite::SqliteMemory;
let memory = SqliteMemory::new("./agent.db")?;
```
### π₯ Multi-Agent Teams
```rust
use traitclaw_team::{Team, AgentRole};
let team = Team::new("research")
.add_role(AgentRole::new("researcher", "Research topics"))
.add_role(AgentRole::new("writer", "Write summaries"));
```
### π MCP Integration
```rust
use traitclaw_mcp::McpServer;
let server = McpServer::stdio("npx", &["-y", "@mcp/server-filesystem", "."]).await?;
```
### π§ Reasoning Strategies
```rust
use traitclaw_strategies::ReActStrategy;
let strategy = ReActStrategy::builder().max_steps(10).build();
```
## ποΈ Architecture Overview
TraitClaw follows a layered trait architecture:
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β traitclaw (meta-crate) β
β Re-exports everything. One dependency, full power. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Extension Crates β
β ββββββββββββ ββββββββββββ βββββββββ ββββββββ β
β β steering β β sqlite β β mcp β β team β ... β
β ββββββββββββ ββββββββββββ βββββββββ ββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Provider Crates β
β ββββββββββββββββ ββββββββββββββ ββββββββββββββββββββ β
β β openai-compatβ β anthropic β β openai (native) β β
β ββββββββββββββββ ββββββββββββββ ββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β traitclaw-core (foundation) β
β Agent Β· Provider Β· Tool Β· Memory Β· Guard Β· Hint Β· β
β Tracker Β· ContextManager Β· OutputTransformer Β· β
β ExecutionStrategy Β· AgentStrategy Β· AgentHook β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
### The Agent Loop
1. **Context Hydration** β Retrieve past dialogue from `Memory`, append user prompt
2. **Provider Generation** β LLM evaluates context, returns text or tool call
3. **Tool Resolution** β Parse arguments, execute Rust function, append result
4. **Recursive Reasoning** β Repeat steps 2β3 until task is complete
5. **Memory Commit** β Save final trajectory back to `Memory`
## βοΈ Feature Flags
```toml
[dependencies]
traitclaw = { version = "1.0", features = ["full"] }
```
| `openai-compat` | `traitclaw-openai-compat` | β
|
| `macros` | `traitclaw-macros` | β
|
| `steering` | `traitclaw-steering` | β |
| `sqlite` | `traitclaw-memory-sqlite` | β |
| `mcp` | `traitclaw-mcp` | β |
| `rag` | `traitclaw-rag` | β |
| `team` | `traitclaw-team` | β |
| `eval` | `traitclaw-eval` | β |
| `strategies` | `traitclaw-strategies` | β |
| `tiktoken` | Accurate token counting | β |
| `full` | All of the above | β |
## π¦ Crate Ecosystem
| [`traitclaw`](https://crates.io/crates/traitclaw) | **Meta-crate β start here** |
| [`traitclaw-core`](https://crates.io/crates/traitclaw-core) | Core traits, Agent runtime, Builder |
| [`traitclaw-macros`](https://crates.io/crates/traitclaw-macros) | `#[derive(Tool)]` proc macro |
| [`traitclaw-openai-compat`](https://crates.io/crates/traitclaw-openai-compat) | OpenAI, Ollama, Groq, Mistral, vLLM |
| [`traitclaw-openai`](https://crates.io/crates/traitclaw-openai) | Native OpenAI + Structured Output |
| [`traitclaw-anthropic`](https://crates.io/crates/traitclaw-anthropic) | Anthropic Claude provider |
| [`traitclaw-steering`](https://crates.io/crates/traitclaw-steering) | Guards, Hints, Trackers |
| [`traitclaw-memory-sqlite`](https://crates.io/crates/traitclaw-memory-sqlite) | SQLite persistent memory |
| [`traitclaw-mcp`](https://crates.io/crates/traitclaw-mcp) | MCP client (stdio + SSE) |
| [`traitclaw-rag`](https://crates.io/crates/traitclaw-rag) | RAG pipeline (BM25, embeddings, hybrid) |
| [`traitclaw-team`](https://crates.io/crates/traitclaw-team) | Multi-agent teams & orchestration |
| [`traitclaw-eval`](https://crates.io/crates/traitclaw-eval) | Evaluation & benchmarking |
| [`traitclaw-strategies`](https://crates.io/crates/traitclaw-strategies) | ReAct, CoT, MCTS reasoning |
| [`traitclaw-test-utils`](https://crates.io/crates/traitclaw-test-utils) | Mock providers & test helpers |
## π Examples
| 01 | [hello-agent](https://github.com/traitclaw/traitclaw/tree/main/examples/01-hello-agent) | Minimal 5-line agent |
| 02 | [tool-calling](https://github.com/traitclaw/traitclaw/tree/main/examples/02-tool-calling) | Custom tools with `#[derive(Tool)]` |
| 03 | [streaming](https://github.com/traitclaw/traitclaw/tree/main/examples/03-streaming) | Real-time streaming responses |
| 04 | [steering](https://github.com/traitclaw/traitclaw/tree/main/examples/04-steering) | Guards, Hints & auto-config |
| 05 | [structured-output](https://github.com/traitclaw/traitclaw/tree/main/examples/05-structured-output) | JSON β Rust types |
| 06 | [memory-persistence](https://github.com/traitclaw/traitclaw/tree/main/examples/06-memory-persistence) | SQLite conversation history |
| 10 | [mcp-client](https://github.com/traitclaw/traitclaw/tree/main/examples/10-mcp-client) | MCP tool discovery |
| 11 | [custom-strategy](https://github.com/traitclaw/traitclaw/tree/main/examples/11-custom-strategy) | Build your own AgentStrategy |
| 12 | [lifecycle-hooks](https://github.com/traitclaw/traitclaw/tree/main/examples/12-lifecycle-hooks) | Observability middleware |
| 13 | [custom-router](https://github.com/traitclaw/traitclaw/tree/main/examples/13-custom-router) | Custom team routing logic |
| 14 | [compressed-memory](https://github.com/traitclaw/traitclaw/tree/main/examples/14-compressed-memory) | LLM-powered memory compression |
| 15 | [context-manager](https://github.com/traitclaw/traitclaw/tree/main/examples/15-context-manager) | Custom context window management |
| 16 | [output-transformer](https://github.com/traitclaw/traitclaw/tree/main/examples/16-output-transformer) | Post-process agent output |
| 17 | [dynamic-tools](https://github.com/traitclaw/traitclaw/tree/main/examples/17-dynamic-tools) | Runtime tool registration |
| 18 | [context-managers](https://github.com/traitclaw/traitclaw/tree/main/examples/18-context-managers) | Built-in context managers |
| 19 | [grouped-registry](https://github.com/traitclaw/traitclaw/tree/main/examples/19-grouped-registry) | Grouped tool registry |
| 20 | [mcp-tool-registry](https://github.com/traitclaw/traitclaw/tree/main/examples/20-mcp-tool-registry) | MCP as ToolRegistry |
| 21 | [rag-pipeline](https://github.com/traitclaw/traitclaw/tree/main/examples/21-rag-pipeline) | RAG with BM25 + embeddings |
| 22 | [multi-agent-team](https://github.com/traitclaw/traitclaw/tree/main/examples/22-multi-agent-team) | Team orchestration |
| 23 | [eval-runner](https://github.com/traitclaw/traitclaw/tree/main/examples/23-eval-runner) | Evaluation test suites |
| 24 | [agent-factory](https://github.com/traitclaw/traitclaw/tree/main/examples/24-agent-factory) | Factory pattern for agents |
| 25 | [reasoning-strategies](https://github.com/traitclaw/traitclaw/tree/main/examples/25-reasoning-strategies) | ReAct, CoT, MCTS in action |
| 26 | [observability](https://github.com/traitclaw/traitclaw/tree/main/examples/26-observability) | Tracing & structured logging |
```bash
cd examples/01-hello-agent && cargo run
```
## π Stability Policies
### Semantic Versioning
TraitClaw follows [Semantic Versioning 2.0.0](https://semver.org/):
| Bug fixes | Patch (`1.0.x`) |
| New public types, traits, methods | Minor (`1.x.0`) |
| MSRV bump | Minor (`1.x.0`) |
| Deprecation notices | Minor (`1.x.0`) |
| Removal of deprecated items | Major (`2.0.0`) |
| Trait signature changes | Major (`2.0.0`) |
### MSRV (Minimum Supported Rust Version)
- **Current:** Rust 1.75
- Bumped only in **minor** releases, never in patches
- Always documented in [CHANGELOG.md](https://github.com/traitclaw/traitclaw/blob/main/CHANGELOG.md)
- Maintains a minimum 6-month lag from the latest Rust stable release
### Deprecation Policy
- Deprecated items are marked with `#[deprecated(since = "1.x", note = "Use Y instead")]`
- Items remain available for a minimum of **2 minor versions** after deprecation
- Deprecated items are removed only in the next **major** version (v2.0.0)
## πΊοΈ Roadmap
| **v1.0.0** | Production Ready | β
Stable API, crates.io publication |
| v1.1.0 | Benchmark & Orchestrate | Benchmarks, orchestration strategy |
| v1.2.0 | Contracts | Inter-agent contracts, typed delegation |
| v1.3.0 | Resilience | Retry, checkpoint, fault tolerance |
## License
Licensed under either of [Apache License, Version 2.0](https://github.com/traitclaw/traitclaw/blob/main/LICENSE-APACHE) or [MIT License](https://github.com/traitclaw/traitclaw/blob/main/LICENSE-MIT) at your option.