Anthropic Rust SDK
An unofficial, comprehensive, type-safe Rust SDK for the Anthropic API with full feature parity to the TypeScript SDK. Built with modern async/await patterns, extensive error handling, and ergonomic builder APIs.
โจ Features
- ๐ Full API Coverage: Messages, Streaming, Tools, Vision, Files, Batches, and Models APIs
- ๐ก๏ธ Type Safety: Comprehensive type system with serde integration
- ๐ Async/Await: Built on tokio with efficient async operations
- ๐ง Builder Patterns: Ergonomic fluent APIs for easy usage
- ๐ก Streaming Support: Real-time streaming responses with proper backpressure
- ๐ ๏ธ Tool Integration: Complete tool use system with custom tool support
- ๐๏ธ Vision Support: Image analysis with base64 and URL inputs
- ๐ File Management: Upload, manage, and process files
- ๐ Batch Processing: Efficient batch operations for large workloads
- ๐ค Model Intelligence: Smart model selection and comparison utilities
- โก Production Ready: Comprehensive error handling, retries, and logging
๐ฆ Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["rt-multi-thread", "macros"] }
๐ Quick Start
Basic Setup
use ;
async
Simple Message
use ;
async
๐ Comprehensive Usage Guide
๐ง Client Configuration
use ;
use Duration;
let config = new
.with_timeout
.with_max_retries
.with_log_level
.with_base_url;
let client = with_config?;
๐ฌ Messages API
Basic Conversation
let response = client.messages
.create
.await?;
Multi-turn Conversation
let response = client.messages
.create
.await?;
Advanced Parameters
let response = client.messages
.create
.await?;
๐ Streaming Responses
use ;
use StreamExt;
let mut stream = client.messages
.stream
.await?;
while let Some = stream.next.await
๐๏ธ Vision (Image Analysis)
use ;
// Using base64 image
let response = client.messages
.create
.await?;
// Using image URL
let response = client.messages
.create
.await?;
๐ ๏ธ Tool Use
use ;
// Define a weather tool
let weather_tool = Tool ;
let response = client.messages
.create
.await?;
// Handle tool use in response
if let Some = response.content.get_tool_use
๐ File Management
// Upload a file
let file = client.files
.upload
.await?;
println!;
// List files
let files = client.files
.list
.await?;
// Use file in conversation
let response = client.messages
.create
.await?;
๐ Batch Processing
use ;
// Create batch requests
let requests = vec!;
// Create and submit batch
let batch = client.batches
.create
.await?;
println!;
// Check batch status
let status = client.batches.retrieve.await?;
println!;
๐ค Model Intelligence
use ;
// List available models
let models = client.models.list.await?;
for model in models.data
// Find best model for requirements
let requirements = new
.max_cost_per_million_tokens
.min_context_length
.required_capabilities
.build;
let best_model = client.models
.find_best_model
.await?;
println!;
// Compare models
let comparison = client.models
.compare_models
.await?;
println!;
๐๏ธ Architecture & Design
Type System
The SDK uses a comprehensive type system with:
- Builders: Fluent APIs for easy construction
- Enums: Type-safe model and parameter selection
- Validation: Compile-time and runtime validation
- Serialization: Automatic JSON handling with serde
Error Handling
use ;
match client.messages.create.await
Async Patterns
Built on tokio with:
- Efficient connection pooling
- Automatic retries with exponential backoff
- Proper timeout handling
- Stream processing with backpressure
๐ง Configuration Options
Environment Variables
ANTHROPIC_API_KEY=your-api-key
ANTHROPIC_BASE_URL=https://api.anthropic.com # Optional
ANTHROPIC_TIMEOUT=30 # Seconds
ANTHROPIC_MAX_RETRIES=3 # Retry attempts
Custom Configuration
use ;
use Duration;
let config = new
.with_base_url
.with_timeout
.with_max_retries
.with_log_level
.with_user_agent;
let client = with_config?;
๐ Examples
The examples/ directory contains comprehensive demonstrations:
basic_client.rs- Basic client setup and configurationmessages_api.rs- Complete Messages API usagestreaming_example.rs- Real-time streaming responsescomprehensive_tool_use.rs- Advanced tool integrationcomprehensive_file_upload.rs- File management workflowsphase5_1_batches.rs- Batch processing examplesphase5_3_models_api.rs- Model selection and comparisonproduction_patterns.rs- Production-ready patterns
Run examples:
๐งช Testing
# Run all tests
# Run with output
# Test specific module
# Run integration tests
๐ Documentation
Generate and view documentation:
# Generate docs
# View online documentation
# https://docs.rs/anthropic-sdk-rust
๐ Performance & Production
Connection Pooling
The SDK automatically manages HTTP connections with:
- Keep-alive connections
- Connection pooling via reqwest
- Automatic retries with exponential backoff
Memory Management
- Streaming responses to handle large outputs
- Efficient JSON parsing with serde
- Minimal allocations in hot paths
Error Recovery
- Automatic retries for transient failures
- Circuit breaker patterns for reliability
- Comprehensive error types for debugging
๐ก๏ธ Security
- API keys handled securely (never logged)
- HTTPS-only communication
- Input validation and sanitization
- Rate limiting awareness
๐ค Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and add tests
- Run tests:
cargo test - Commit:
git commit -m 'Add amazing feature' - Push:
git push origin feature/amazing-feature - Open a Pull Request
๐ Roadmap
- Phase 1: Foundation & Client Setup
- Phase 2: Messages API
- Phase 3: Streaming & Tools
- Phase 4: Vision & Advanced Features
- Phase 5: Files, Batches & Models APIs
- Phase 6: Cloud Integrations (AWS Bedrock, GCP Vertex AI)
- Phase 7: Advanced Streaming & WebSockets
- Phase 8: Caching & Performance Optimization
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐โโ๏ธ Support
- Documentation: docs.rs/anthropic-sdk-rust
- Issues: GitHub Issues
- Discussions: GitHub Discussions
๐ฏ Examples Gallery
Real-world Use Cases
// Customer Support Bot
let response = client.messages
.create
.await?;
// Code Review Assistant
let response = client.messages
.create
.await?;
// Document Analysis
let response = client.messages
.create
.await?;
Ready to build amazing AI-powered applications with Rust? Get started with the Anthropic SDK today! ๐ฆโจ