Rust Deep Agents SDK
A high-performance Rust framework for building AI agents with custom tools, sub-agents, and persistent state management. Built for production use with enterprise-grade features like token tracking, cost monitoring, and human-in-the-loop workflows.
🆕 What's New in v0.0.24
- Streaming Events: Real-time token-by-token event broadcasting for streaming responses
- StreamingToken Events: New
AgentEvent::StreamingTokenvariant for live updates - Opt-in Streaming: Broadcasters can enable streaming via
supports_streaming()method - Backward Compatible: Existing broadcasters work unchanged (streaming disabled by default)
- Enhanced Streaming:
handle_message_stream()now emits events for SSE/WebSocket integrations - Example: New
streaming-events-demoshowing real-time token broadcasting
Quick Start
Installation
Add to your Cargo.toml:
[]
= "0.0.24"
= { = "1.0", = ["full"] }
= "1.0"
Basic Agent
use ;
use tool;
use AgentStateSnapshot;
use Arc;
// Define a tool using the #[tool] macro
async
Core Features
🤖 Agent Builder API
The ConfigurableAgentBuilder provides a fluent interface for constructing agents:
use ;
use Arc;
// OpenAI
let config = new?;
let model = new;
let agent = new
.with_model
.build?;
// Anthropic
let config = new?;
let model = new;
let agent = new
.with_model
.build?;
// Gemini
let config = new?;
let model = new;
let agent = new
.with_model
.build?;
🛠️ Tool System
Define tools using the #[tool] macro:
use tool;
// Simple synchronous tool
// Async tool
async
// Tool with optional parameters
// Use the tools
let agent = new
.with_model
.with_tools
.build?;
💾 State Persistence
Choose from multiple persistence backends:
use ;
// In-memory (development)
let checkpointer = new;
// Redis (production)
let checkpointer = new;
let agent = new
.with_model
.with_checkpointer
.build?;
// Save and load state across sessions
let thread_id = "user-123";
agent.save_state.await?;
agent.load_state.await?;
📊 Token Tracking & Cost Monitoring
Monitor LLM usage and costs with built-in token tracking:
use ;
// Enable token tracking with default settings
let model = new;
let agent = new
.with_model
.with_token_tracking // Enable with defaults
.build?;
// Or configure with custom settings
let token_config = TokenTrackingConfig ;
let model = new;
let agent = new
.with_model
.with_token_tracking_config
.build?;
Features:
- Real-time token usage tracking
- Cost estimation with predefined pricing models
- Performance metrics (duration, throughput)
- Event broadcasting integration
- Flexible configuration options
🔒 Human-in-the-Loop (HITL)
Require human approval for critical operations:
use ;
use HashMap;
// Configure HITL policies
let mut policies = new;
policies.insert;
let agent = new
.with_model
.with_tool_interrupts
.with_checkpointer // Required for HITL
.build?;
// Handle interrupts
match agent.handle_message.await
// Resume with approval
agent.resume_with_approval.await?;
📡 Event System
Real-time progress tracking and multi-channel notifications:
use ;
use AgentEvent;
use async_trait;
;
let agent = new
.with_model
.with_event_broadcaster
.build?;
🔐 Security & PII Protection
Built-in security features prevent PII leakage:
// PII sanitization is enabled by default
let agent = new
.with_model
.build?;
// Events automatically have:
// - Message previews truncated to 100 characters
// - Sensitive fields (passwords, tokens, etc.) redacted
// - PII patterns (emails, phones, credit cards) removed
// Disable only if you need raw data and have other security measures
let agent = new
.with_model
.with_pii_sanitization // Not recommended for production
.build?;
Advanced Features
Sub-Agents
Delegate tasks to specialized sub-agents:
use ;
let subagent = SubAgentConfig ;
let agent = new
.with_model
.with_subagent
.with_auto_general_purpose // Enable automatic delegation
.build?;
Built-in Tools
The SDK includes useful built-in tools:
let agent = new
.with_model
.with_builtin_tools
.build?;
Prompt Caching
Optimize performance with prompt caching:
let agent = new
.with_model
.with_prompt_caching
.build?;
Examples
The SDK includes comprehensive examples:
simple-agent- Basic agent with OpenAItoken-tracking-demo- Token usage monitoringhitl-financial-advisor- Human-in-the-loop workflowsevent-system-demo- Event broadcastingcheckpointer-demo- State persistencesubagent-demo- Sub-agent delegation
Run examples:
# Clone the repository
# Run a specific example
Architecture
Workspace Layout
crates/agents-core- Core traits, message structures, and state modelscrates/agents-runtime- Runtime engine, builders, and middlewarecrates/agents-toolkit- Built-in tools and utilitiescrates/agents-aws- AWS integrations (DynamoDB, Secrets Manager)crates/agents-persistence- Persistence backends (Redis, PostgreSQL)crates/agents-sdk- Unified SDK with feature flagsexamples/- Working examples and demosdocs/- Documentation and guides
Middleware Stack
The SDK includes a powerful middleware system:
- Planning Middleware: Todo list management
- Filesystem Middleware: Mock filesystem operations
- SubAgent Middleware: Task delegation
- HITL Middleware: Human approval workflows
- Token Tracking Middleware: Usage and cost monitoring
- Summarization Middleware: Context window management
- PII Sanitization: Automatic data protection
Provider Support
- OpenAI: GPT models (gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbo)
- Anthropic: Claude models (claude-3-5-sonnet-20241022, claude-3-haiku-20240307)
- Gemini: Google's Gemini models (gemini-2.0-flash-exp, gemini-1.5-pro)
Development
Building from Source
# Format, lint, and test
# Build release
Feature Flags
The SDK supports feature flags for modular functionality:
[]
= { = "0.0.23", = ["aws", "redis"] }
# Available features:
# - "aws" - AWS integrations (DynamoDB, Secrets Manager)
# - "redis" - Redis persistence backend
# - "postgres" - PostgreSQL persistence backend
# - "dynamodb" - DynamoDB persistence backend
# - "full" - All features enabled
Environment Variables
Required environment variables:
# OpenAI
# Anthropic
# Gemini
# Optional: Tavily for web search
Performance
The Rust SDK is designed for high performance:
- Memory Efficient: Zero-copy message handling where possible
- Async First: Built on Tokio for concurrent operations
- Type Safe: Compile-time guarantees for agent configurations
- Fast Compilation: Optimized build times with feature flags
- Low Latency: Minimal overhead for tool calls and state management
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run
cargo fmtandcargo clippy - Submit a pull request
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Support
Roadmap
- Custom sub-agent execution graphs
- Dict-based model configuration
- Advanced state features (encryption, migrations)
- Enhanced tool system (composition, validation)
- Performance optimizations
- Additional LLM providers
Built with ❤️ in Rust for the AI community