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
- Prompt Testing Framework: Comprehensive testing utilities for prompts with assertions and configurable test vectors
- Command Line Tools: Ready-to-use binaries for prompt testing and text processing
- 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.16.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
Command Line Tools
Claudius includes several command-line tools for working with the Anthropic API:
median-text
The median-text binary is designed for document transcription improvement. Given multiple transcriptions of the same document, it uses Claude to select and provide the best unified transcription.
# Provide the best transcription from multiple files
# The tool will output the improved/unified transcription to stdout
This tool is particularly useful for:
- Improving OCR results by comparing multiple scans
- Consolidating different transcription attempts
- Cleaning up and unifying document content
The tool uses Claude Opus with thinking enabled to analyze the provided documents and output a single, improved transcription.
Prompt Testing Framework
Claudius includes a comprehensive prompt testing framework that allows you to test prompts against the Anthropic API with configurable assertions and test vectors. This is especially useful for ensuring prompt reliability, regression testing, and CI/CD integration.
Using the claudius-prompt Binary
The claudius-prompt binary provides a command-line interface for running prompt tests:
# Run a simple text prompt file
# Run a YAML configuration with assertions
# Run multiple tests
# Test mode with exit codes (useful for CI/CD)
# Get verbose output with timing and token information
# Output in different formats
Prompt Test Configuration
Test configurations can be written in YAML format with comprehensive assertion support:
name: "Simple Math Test"
prompt: "What is 2 + 2? Please respond with just the number."
model: "claude-3-5-haiku-latest"
max_tokens: 50
temperature: 0.0
system: "You are a helpful math assistant."
# Assertion configuration
expected_contains:
- "4"
expected_not_contains:
- "5"
- "3"
min_response_length: 1
max_response_length: 10
Advanced configurations support multi-turn conversations, tool usage, and inheritance:
name: "Multi-turn Conversation Test"
messages:
- role: "user"
content: "I'm learning Rust programming."
- role: "assistant"
content: "That's great! What would you like to learn about first?"
- role: "user"
content: "Tell me about ownership and borrowing."
system: "You are a helpful Rust programming tutor."
model: "claude-3-5-haiku-latest"
max_tokens: 400
temperature: 0.3
expected_contains:
- "ownership"
- "borrowing"
expected_not_contains:
- "garbage collection"
min_response_length: 100
Configuration inheritance allows for reusable base configurations:
# base.yaml
name: "Base Configuration"
model: "claude-3-5-haiku-latest"
max_tokens: 100
temperature: 0.5
system: "You are a helpful assistant."
# specific_test.yaml
inherits: "../base.yaml"
name: "Specific Test"
prompt: "What is the capital of France?"
expected_contains:
- "Paris"
File references enable modular prompt and system configurations:
# Using external files for prompt and system content
name: "File Reference Test"
prompt: "prompt.yaml" # Contents loaded from prompt.yaml
system: "system.md" # Contents loaded from system.md
model: "claude-3-5-haiku-latest"
max_tokens: 400
expected_contains:
- "helpful"
File references support relative paths and work with configuration inheritance:
# In subdirectory: prompts/test.yaml
name: "Modular Configuration"
prompt: "prompt.yaml" # Loaded from prompts/prompt.yaml
system: "../common/system.md" # Loaded from common/system.md
inherits: "../base.yaml" # Configuration inheritance
Programmatic Testing
You can also use the testing framework programmatically in Rust:
use ;
async
Assertion Types
The testing framework supports several types of assertions:
- Content assertions:
expected_containsandexpected_not_containscheck for specific text in responses - Length assertions:
min_response_lengthandmax_response_lengthvalidate response size - Tool call assertions:
expected_tool_callsverifies that specific tools were called (when tools are configured) - Error assertions:
expect_errorandexpected_error_messagetest error handling
File References
The framework supports loading content from external files to enable modular configurations:
- Prompt files: Use
prompt: "prompt.yaml"to load prompt content from external files - System files: Use
system: "system.md"to load system prompts from external files - Relative paths: File references are resolved relative to the configuration file's directory
- Security: Only files named exactly
prompt.yamlorsystem.mdare automatically resolved - Compatibility: File references work seamlessly with configuration inheritance
CI/CD Integration
The claudius-prompt binary is designed for CI/CD integration:
# .github/workflows/prompt-tests.yml
- name: Run prompt tests
run: |
cargo run --bin claudius-prompt -- --test --verbose prompts/*.yaml
env:
CLAUDIUS_API_KEY: ${{ secrets.CLAUDIUS_API_KEY }}
The binary exits with status code 0 on success and 1 on failure when using the --test flag, making it suitable for automated testing pipelines.
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