Claude SDK for Rust
A complete, production-ready Rust SDK for Claude with full API coverage. Built for Colony Shell but usable standalone.
⚡ What Makes This Special
- 100% Feature Complete - Every Claude API feature implemented
- Both Platforms - Anthropic API + AWS Bedrock with unified interface
- Production Ready - 90 tests, retry logic, error handling, token counting
- Developer Experience - Interactive REPL, auto-changelog, pre-commit hooks
- Type Safe - Leverage Rust's type system for correctness
- Zero Cost - Idiomatic async Rust with no runtime overhead
🚀 Features
Core API
✅ Streaming & Non-streaming - SSE streams + complete responses ✅ Error Handling - Comprehensive error taxonomy with retry logic ✅ Exponential Backoff - Automatic retry on rate limits (429) and server errors (5xx) ✅ Token Counting - Accurate context window management with tiktoken-rs
Intelligence & Tools
✅ Tool Use - Programmatic calling, parallel execution, input examples (beta) ✅ Tool Choice - Force specific tools (auto/any/tool/none) ✅ Conversations - Multi-turn state management with ConversationBuilder ✅ Extended Thinking - Step-by-step reasoning (thinking blocks with signatures) ✅ Effort Control - Token efficiency (high/medium/low for Opus 4.5)
Content Types
✅ Text - Standard messages with optional citations ✅ Images - Vision support (base64, URL, or file_id) ✅ Documents - PDFs and text files with citations ✅ Search Results - RAG with automatic source attribution ✅ Thinking Blocks - Extended reasoning (including redacted)
Cost Optimization
✅ Prompt Caching - 90% cost reduction on cached content (system prompts, tools, docs) ✅ Batch Processing - 50% discount for async bulk requests (up to 100K requests) ✅ Files API - Upload once, reference many times
Platform Support
✅ Anthropic API - Full support including all beta features ✅ AWS Bedrock - Complete implementation with streaming ✅ Regional Endpoints - standard/global/us/eu/ap prefixes
Developer Tools
✅ Interactive REPL - Full terminal UI with streaming, token display, backend switching ✅ Auto-Changelog - Uses Claude to generate changelog from git commits ✅ Pre-commit Hooks - Automated quality checks (test, clippy, fmt, docs) ✅ CI/CD - GitHub Actions for testing, releases, upstream monitoring ✅ System Prompts - Battle-tested prompts including Claude Code
Advanced Features
✅ Structured Outputs - Forced JSON extraction with schema validation ✅ Context Validation - Proactive window checking (200K/1M tokens) ✅ Model Registry - 9 models with full metadata, constraints, pricing ✅ Model Capabilities - Track extended context, effort support, thinking support
📦 Installation
Add to your Cargo.toml:
[]
= "1.0"
= { = "1", = ["full"] }
# Optional features
= { = "1.0", = ["bedrock", "repl"] }
Features:
anthropic(default) - Anthropic API supportbedrock- AWS Bedrock supportrepl- Interactive REPL binaryfull- All features enabled
🎯 Quick Start
Basic Chat
use ;
async
Streaming Responses
use ;
use StreamExt;
async
AWS Bedrock
Same API works with AWS Bedrock:
use ;
async
Tool Use
use ;
use json;
let mut conversation = new
.with_cached_system // Cached for 5min!
.with_cached_tool;
conversation.add_user_message;
// Claude will request tool use automatically
let request = conversation.build;
Extended Thinking
let request = new
.with_thinking; // 10K token budget for reasoning
// Response includes thinking blocks showing step-by-step reasoning
Batch Processing (50% Discount!)
use ;
let batch_client = new;
let requests = vec!;
let batch = batch_client.create.await?;
let completed = batch_client.wait_for_completion.await?;
// 50% cost reduction on all tokens!
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
All Claude 4.5, 4.x, and 3.x models supported with full metadata:
| Model | Context | Output | Extended Context | Thinking | Effort |
|---|---|---|---|---|---|
| Claude Sonnet 4.5 | 200K | 64K | ✅ 1M | ✅ | ❌ |
| Claude Haiku 4.5 | 200K | 64K | ❌ | ✅ | ❌ |
| Claude Opus 4.5 | 200K | 64K | ❌ | ✅ | ✅ |
| Claude Opus 4.1 | 200K | 32K | ❌ | ✅ | ❌ |
| Claude Sonnet 4 | 200K | 64K | ✅ 1M | ✅ | ❌ |
| Others | 200K | varies | varies | varies | ❌ |
Model Registry Includes:
- Context windows & output limits
- Extended context support (1M)
- Thinking capability
- Effort parameter support
- Pricing per million tokens
- Bedrock & Vertex AI IDs
- Regional endpoint prefixes
✅ Complete API Coverage
Messages API: 100%
- Non-streaming & streaming
- System prompts (cached & non-cached)
- Multi-turn conversations
- Stop reasons (end_turn, max_tokens, stop_sequence, tool_use, pause_turn)
Content Types: 100%
- Text (with citations)
- Images (base64, URL, file_id)
- Documents (PDFs, text with citations)
- Search Results (RAG with auto-citations)
- Tool Use & Tool Results
- Thinking & Redacted Thinking
Tool Use: 100%
- Tool definitions with JSON schemas
- Programmatic calling (disable_user_input)
- Tool choice (auto/any/tool/none)
- Parallel tool use (configurable)
- Input examples (beta)
- Tool result error handling
Platform: 100%
- Anthropic API (complete)
- AWS Bedrock (streaming + non-streaming)
- Regional endpoints (standard/global/us/eu/ap)
- Automatic model ID conversion
Optimization: 100%
- Prompt caching (90% savings)
- Batch processing (50% discount)
- Token counting (tiktoken-rs)
- Context validation (200K/1M)
- Retry logic with backoff
- Effort control (Opus 4.5)
Advanced: 100%
- Extended thinking (reasoning blocks)
- Files API (upload/download/manage)
- Structured outputs (forced JSON)
- Vision (image analysis)
- Citations (automatic source attribution)
Developer Tools: 100%
- Interactive REPL
- Token counting display
- Conversation save/load
- Auto-changelog generator
- Pre-commit hooks
- CI/CD pipelines
- System prompts (Claude Code)
🎉 Development Status: v1.0.0 - COMPLETE
All Features Implemented: 22/22 (100%)
✅ Core SDK (17/17) ✅ Advanced Features (5/5) ✅ 90 tests passing ✅ Zero technical debt ✅ Production ready
What This Means:
- Every documented Claude API feature is implemented
- Both Anthropic and AWS Bedrock fully supported
- All content types (text, image, document, search, thinking)
- All optimization features (caching, batching, retry)
- Complete developer tooling (REPL, hooks, CI/CD)
- Ready for immediate production use
💡 Why Use This SDK?
vs. Official SDKs (TypeScript/Python):
- ✅ Type Safety - Rust's type system catches errors at compile time
- ✅ Model Registry - Full metadata with constraints and validation
- ✅ Unified Bedrock - Same API for both platforms
- ✅ System Prompts - Pre-built prompts (Claude Code, RAG, coding, etc.)
- ✅ Better Regional Support - First-class Bedrock endpoint handling
- ✅ Performance - Zero-cost abstractions, no GC pauses
- ✅ Memory Safety - No segfaults, no undefined behavior
vs. Building Your Own:
- ✅ Complete - Every API feature implemented
- ✅ Tested - 90 tests covering all features
- ✅ Maintained - Auto-monitoring for upstream changes
- ✅ Documented - Full API docs + examples
- ✅ Production Ready - Error handling, retry logic, validation
Perfect For:
- 🦀 Rust applications needing Claude integration
- 🤖 Building AI agents (like Colony Shell)
- 🏢 Enterprise apps needing AWS Bedrock
- 📊 Batch processing workloads (50% savings)
- 💰 Cost-sensitive applications (caching, batching)
📁 Project Structure
claude-agent-sdk-rust/
├── src/
│ ├── lib.rs # Public API & re-exports
│ ├── client.rs # HTTP client (Anthropic + Bedrock)
│ ├── conversation.rs # ConversationBuilder
│ ├── types.rs # Request/response types (400+ lines)
│ ├── error.rs # Error taxonomy
│ ├── models.rs # Model registry (9 models)
│ ├── streaming.rs # SSE event types
│ ├── tokens.rs # Token counting (tiktoken-rs)
│ ├── retry.rs # Exponential backoff
│ ├── files.rs # Files API client
│ ├── batch.rs # Batch processing
│ ├── prompts.rs # System prompts (Claude Code, etc.)
│ ├── structured.rs # Structured output helpers
│ └── bin/
│ ├── claude-repl.rs # Interactive REPL (440+ lines)
│ └── update-changelog.rs # Changelog generator
├── examples/
│ ├── simple_chat.rs # Basic non-streaming
│ ├── streaming_chat.rs # Real-time streaming
│ ├── tool_use.rs # Multi-turn with tools
│ └── prompt_caching.rs # Cost optimization
├── .github/workflows/
│ ├── ci.yml # PR checks
│ ├── release.yml # Auto-publish
│ └── upstream-check.yml # Model monitoring
├── hooks/
│ └── pre-commit # Quality checks
├── scripts/
│ └── install-hooks.sh # Hook installer
├── .claude/system/
│ ├── features.json # Feature tracking (22 features)
│ ├── future-features.json # Advanced features
│ └── history.txt # Development log
├── CHANGELOG.md # Keep a Changelog format
├── CONTRIBUTING.md # Development guide
├── RELEASE.md # Release process
├── ROADMAP.md # Feature roadmap
└── README.md # This file
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 feature-complete at v1.0.0, but we welcome:
- 🐛 Bug reports and fixes
- 📝 Documentation improvements
- ✨ Additional examples
- 🔧 Performance optimizations
See CONTRIBUTING.md for development guidelines.
Development
# Setup
# Run tests
# Quality checks
📚 Documentation
- API Documentation - Full API reference
- Examples - Working code examples
- ROADMAP.md - Feature tracking & future plans
- CHANGELOG.md - Version history
- CONTRIBUTING.md - Development guide
- RELEASE.md - Release process
Official Claude Resources:
🎯 Design Principles
- API Compliance - Match official Claude API exactly
- Type Safety - Leverage Rust's type system for correctness
- Zero Cost - Idiomatic async Rust with no runtime overhead
- Platform Agnostic - Same API for Anthropic & Bedrock
- Production Ready - Comprehensive error handling, retry logic, validation
- Developer Friendly - Clear APIs, good docs, helpful tools
🏆 Status
Version: 1.0.0 Status: Production Ready ✅ Features: 22/22 (100%) Tests: 90 passing Platforms: Anthropic + AWS Bedrock
Built for: Colony Shell F015 - Claude ADK Integration
📄 License
MIT License - See LICENSE for details
🙏 Acknowledgments
Built with the official Claude API and inspired by:
Special thanks to Anthropic for creating Claude and maintaining excellent API documentation.