Claude SDK for Rust
A native Rust implementation of the Claude API client with streaming support, tool execution, and programmatic tool calling.
Features
- ✅ Non-streaming API - Send messages and get complete responses
- ✅ Streaming API - Stream responses with Server-Sent Events in real-time
- ✅ Tool Use - Define and execute tools with programmatic calling
- ✅ Conversation Management - Multi-turn conversations with ConversationBuilder
- ✅ Retry Logic - Exponential backoff for rate limits and transient errors
- ✅ Prompt Caching - 90% cost reduction on cached content
- ✅ AWS Bedrock - Full support with streaming (invoke_model)
- ✅ Token Counting - Accurate estimates with tiktoken-rs for context management
- ✅ Extended Thinking - Step-by-step reasoning with thinking blocks
- ✅ Batch Processing - 50% cost discount for async bulk requests
- ✅ Vision - Image analysis with base64/URL/file_id
- ✅ Files API - Upload once, use many times
- 🦀 Idiomatic Rust - Type-safe, async/await, zero-cost abstractions
- 📦 Standalone - No FFI, no subprocesses, pure Rust
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Basic Usage
use ;
async
Streaming Responses
use ;
use StreamExt;
async
Tool Use with Multi-turn Conversations
use ;
use StreamExt;
use json;
async
Retry Logic for Production
use ;
use RetryConfig;
async
Examples
Run the examples with your API key:
Available examples:
simple_chat- Basic message sending with complete responsesstreaming_chat- Real-time streaming responses with token displaytool_use- Tool definitions, multi-turn conversations, programmatic tool callingprompt_caching- Cost reduction with prompt caching (90% savings on cached tokens)
Supported Models
claude-3-5-sonnet-20241022- Most capable, balanced performanceclaude-3-5-haiku-20241022- Fast and cost-effectiveclaude-3-opus-20240229- Most powerful for complex tasks
API Coverage
✅ Implemented
- Basic message sending
- Streaming responses with SSE
- Tool definitions and tool use
- Multi-turn conversations (ConversationBuilder)
- Programmatic tool calling
- Request/response types
- Error handling
- Authentication
- Token usage tracking
- Model registry with constraints
- Bedrock regional/global endpoints
- Prompt caching (CacheControl, cached system/tools, 90% cost reduction)
- Retry logic with exponential backoff
- Respects retry-after headers
- Token counting with tiktoken-rs (cl100k_base)
- Context window validation
- AWS Bedrock support (streaming + non-streaming)
📋 Planned
- Vision support (images)
- Files API
- Search results (RAG with citations)
- Interactive REPL
- More examples and integration tests
Development Status
Phase 1 (Foundation) - COMPLETE ✅
- Non-streaming API
- Streaming API with SSE
- Model registry with all Claude 4.5/4.x/3.x models
- Comprehensive error handling
- CI/CD automation
Phase 2 (Tools & Conversations) - COMPLETE ✅
- Tool definitions and execution
- Multi-turn conversations with ConversationBuilder
- Programmatic tool calling
- Tool result handling
- Retry logic with exponential backoff
- Comprehensive error handling
- Prompt caching (90% cost reduction)
Phase 3 (Platform & Management) - COMPLETE ✅
- AWS Bedrock support (streaming + non-streaming)
- Token counting with tiktoken-rs
- Context window validation
- Extended context support (1M tokens)
Phase 4 (Developer Experience) - COMPLETE ✅
- Interactive REPL with streaming display
- Slash commands for configuration
- Conversation save/load
- Runtime backend switching
- Token counting in prompt
Phase 5 (Advanced Features) - COMPLETE ✅
- Extended thinking (step-by-step reasoning)
- Batch processing (50% cost reduction)
- Vision support (images)
- Files API (upload/download/manage)
- Document blocks (PDFs with citations)
Progress: 20 of 20 features complete (100%) 🎉
See .claude/system/features.json for detailed feature tracking.
Project Structure
claude-agent-sdk-rust/
├── src/
│ ├── lib.rs # Public API
│ ├── client.rs # HTTP client
│ ├── types.rs # Request/response types
│ └── error.rs # Error types
├── examples/
│ └── simple_chat.rs # Example usage
└── .claude/
└── system/ # Development tracking
Development Setup
Install Git Hooks
We use pre-commit hooks to ensure code quality. Install them with:
The pre-commit hook automatically runs:
cargo test- All tests must passcargo clippy -- -D warnings- No clippy warnings allowedcargo fmt --check- Code must be formattedcargo doc- Documentation must build
To bypass the hook (not recommended): git commit --no-verify
Running Checks Manually
# Run all checks
# Auto-fix formatting
Developer Tools
Interactive REPL:
Test the SDK interactively with a full-featured terminal interface:
Features:
- Real-time streaming responses
- Multi-turn conversations
- Token counting in prompt
- Slash commands (/help, /save, /load, /tokens, /model, /backend)
- Backend switching (Anthropic ↔ Bedrock)
- Conversation save/load
Automated Changelog Generation:
Use Claude to generate changelog entries from git commits:
This analyzes commits since the last release and generates Keep a Changelog format entries.
Contributing
This project is part of Colony Shell (F015 - Claude ADK integration) but can be used standalone.
See CONTRIBUTING.md for development guidelines.
Design Principles
- Native Rust - No FFI, no subprocess, pure Rust
- Type-safe - Leverage Rust's type system
- Async-first - Built on tokio
- API-compliant - Match official Claude API exactly
- Zero-copy - Minimize allocations where possible
License
MIT