Claudius
Claudius is a comprehensive Rust SDK for the Anthropic API, providing both low-level API access and a powerful agent framework for building AI-powered applications. This library enables seamless integration with Claude through direct API calls, streaming responses, and high-level agent abstractions with built-in tool support for filesystem operations, shell commands, and custom integrations.
Quick Example
use ;
use tokio;
async
Features
- Complete API Coverage: Access all of Anthropic's AI capabilities through Claude
- Agent Framework: High-level abstractions for building AI-powered applications with state management
- Built-in Tools: Filesystem operations, shell commands, text editing, and web search capabilities
- Budget System: Token allocation and tracking for cost control and resource management
- Streaming Support: Real-time streaming for conversational applications
- Strongly Typed: Take advantage of Rust's type system for predictable API interactions
- Async First: Built with async/await for efficient I/O operations
- Error Handling: Comprehensive error types for robust application development
- Extensible: Modular design with builder patterns and trait-based tool system
Installation
Add Claudius to your Cargo.toml:
[]
= "0.13.0"
Authentication
Claudius uses the Anthropic API key for authentication. You can provide it in two ways:
- Set the
ANTHROPIC_API_KEYenvironment variable:
- Provide it directly when creating the client:
let client = new?;
Usage
Basic Chat
// Create a client
let client = new?; // Uses CLAUDIUS_API_KEY env var and falls back to ANTHROPIC_API_KEY
// Create a user message
let message = new_with_string;
// Set up request parameters
let params = new
.with_system_string;
// Send the request and get the response
let response = client.send.await?;
// Process the response
for content in response.content
Streaming Responses
// Create streaming request parameters
let params = new_streaming;
// Get a stream of events
let stream = client.stream.await?;
// Pin the stream so it can be polled
use StreamExt;
use pin;
pin!;
// Process the stream events
while let Some = stream.next.await
Model Selection
Claudius supports all Anthropic models via a typed enum:
// Use a known model (latest version)
let model = Known;
// Use a specific model version
let model = Known;
// Use a custom model identifier
let model = Custom;
Advanced Configuration
// Customize the client
let client = new?
.with_base_url
.with_timeout;
// Configure request parameters
let params = new
.with_system_string
.with_temperature
.with_top_p
.with_top_k
.with_stop_sequences;
Agent Framework
Claudius provides a powerful agent framework that abstracts away message management, tool integration, and resource budgeting. Agents can be customized with filesystem access, shell commands, and custom tools.
Basic Agent Usage
use ;
use Arc;
async
Custom Agent with Filesystem
use ;
use Path;
async
Budget Management
The budget system provides token allocation and tracking:
use Budget;
use Arc;
// Create a budget with 1000 tokens
let budget = new;
// Allocate tokens for a request
if let Some = budget.allocate
// Check remaining budget (approximately, due to concurrent access)
Built-in Tools
Agents can use built-in tools for common operations:
Filesystem Operations
// Through the FileSystem trait, agents can:
agent.search.await?; // Search for text in files
agent.view.await?; // View file contents
agent.str_replace.await?; // Replace text
agent.insert.await?; // Insert at line
Text Editor Tool
use ;
// The text editor tool provides structured file editing
let editor = new;
// Can be used within agent tool integrations
Bash Tool
use ;
// Execute shell commands
let bash_tool = new;
// Integrated into agent workflows
Custom Tools
Create custom tools by implementing the Tool trait:
use ;
;
Error Handling
Claudius provides a robust error system:
match client.send.await
Examples
The repository includes several examples:
basic_chat.rs: Simple request and response with Claudestreaming.rs: Streaming response handlingagent.rs: Agent framework with filesystem operationsmodels_example.rs: Model listing and information retrievalretry_example.rs: Error handling and retry logic
Run examples with:
# Set your API key
# Run the basic chat example
# Run the streaming example
# Run the agent framework example
# Run the models example
# Run the retry example
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Acknowledgments
- Thanks to Anthropic for creating Claude and providing the API that powers this SDK
- This project is not officially affiliated with Anthropic