Expand description
§api_claude
Comprehensive Rust client for Anthropic’s Claude API with enterprise reliability features.
§🎯 Architecture: Stateless HTTP Client
This API crate is designed as a stateless HTTP client with zero persistence requirements. It provides:
- Direct HTTP calls to the Anthropic Claude API
- In-memory operation state only (resets on restart)
- No external storage dependencies (databases, files, caches)
- No configuration persistence beyond environment variables
This ensures lightweight, containerized deployments and eliminates operational complexity.
§🏛️ Governing Principle: “Thin Client, Rich API”
Expose all server-side functionality transparently while maintaining zero client-side intelligence or automatic behaviors.
Key principles:
- API Transparency: One-to-one mapping with Claude API endpoints
- Zero Automatic Behavior: No implicit decision-making or magic thresholds
- Explicit Control: Developer decides when, how, and why operations occur
- Configurable Reliability: Enterprise features available through explicit configuration
§Scope
§In Scope
- Messages API (conversational interface)
- Server-Sent Events streaming
- Tool/function calling
- Vision support (image analysis)
- Prompt caching (~90% cost savings)
- Token counting
- System prompts and safety settings
- Enterprise reliability (retry, circuit breaker, rate limiting, failover, health checks)
- Synchronous API wrapper
- Batch operations
§Out of Scope
- Embeddings (not offered by Anthropic)
- Audio processing (not available in Claude API)
- WebSocket streaming (Claude uses SSE only)
- Model tuning/deployment (managed service only)
§Features
Core Capabilities:
- Messages API with full conversational support
- SSE streaming responses with tool calling integration
- Complete function/tool calling with validation
- Vision support for image analysis
- Prompt caching for cost optimization
Enterprise Reliability:
- Retry logic with exponential backoff and jitter
- Circuit breaker for failure threshold management
- Rate limiting with token bucket algorithm
- Multi-endpoint failover (4 strategies)
- Health checks with endpoint monitoring
Client Enhancements:
- Sync API wrapper for blocking operations
- CURL diagnostics for debugging
- Dynamic configuration with file watching
- Enterprise quota management
- HTTP compression support
§Installation
Add to your Cargo.toml:
[dependencies]
api_claude = { version = "0.1.0", features = ["full"] }§Quick Start
§Basic Usage
use api_claude::{ Client, Secret, CreateMessageRequest, Message, Role, Content };
#[ tokio::main ]
async fn main() -> Result< (), Box< dyn std::error::Error > >
{
let secret = Secret::new( "sk-ant-api03-your-key-here".to_string() )?;
let client = Client::new( secret );
let request = CreateMessageRequest::builder()
.model( "claude-sonnet-4-5-20250929".to_string() )
.max_tokens( 1000 )
.messages( vec![
Message
{
role : Role::User,
content : vec![ Content::Text
{
r#type : "text".to_string(),
text : "Hello, Claude!".to_string(),
} ],
cache_control : None,
}
] )
.build();
let response = client.create_message( request ).await?;
println!( "Claude: {:?}", response.content );
Ok( () )
}§Streaming Response
use api_claude::{ Client, Secret, CreateMessageRequest, Message, Role, Content };
use futures_util::StreamExt;
#[ tokio::main ]
async fn main() -> Result< (), Box< dyn std::error::Error > >
{
let client = Client::from_workspace()?;
let request = CreateMessageRequest::builder()
.model( "claude-sonnet-4-5-20250929".to_string() )
.max_tokens( 1000 )
.stream( true )
.messages( vec![ Message::user( "Tell me a story" ) ] )
.build();
let mut stream = client.create_message_stream( request ).await?;
while let Some( event ) = stream.next().await
{
let event = event?;
if let Some( text ) = event.delta_text()
{
print!( "{}", text );
}
}
Ok( () )
}§Authentication
§Option 1: Workspace Secret (Recommended)
Create secret/-secrets.sh in your workspace root:
#!/bin/bash
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"use api_claude::Client;
let client = Client::from_workspace()?;§Option 2: Environment Variable
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"use api_claude::Client;
let client = Client::from_env()?;§Option 3: Direct Configuration
use api_claude::{ Client, Secret };
let secret = Secret::new( "sk-ant-api03-your-key-here".to_string() )?;
let client = Client::new( secret );See Secret Loading Guide for complete authentication options.
§Feature Flags
§Core Features
enabled- Master switch for core functionalitystreaming- SSE streaming supporttools- Function calling and toolsvision- Image understanding capabilities
§Enterprise Reliability
retry-logic- Exponential backoff retrycircuit-breaker- Circuit breaker patternrate-limiting- Token bucket rate limitingfailover- Multi-endpoint failoverhealth-checks- Health monitoring
§Client Enhancements
sync-api- Synchronous wrapperscurl-diagnostics- Debug utilitiescompression- HTTP compressionenterprise-quota- Usage trackingdynamic-config- Runtime configuration
§Presets
full- All features enabled
§Testing
§Test Coverage
- 435 tests passing (100%)
- Real API integration tests
- No-mockup policy: all integration tests use real API calls
§Supported Models
| Model | Context Window | Capabilities |
|---|---|---|
| claude-sonnet-4-5-20250929 | 200k tokens | Full capabilities |
| claude-3-5-sonnet-latest | 200k tokens | Fast, cost-effective |
| claude-3-opus-latest | 200k tokens | Highest capability |
| claude-3-haiku-latest | 200k tokens | Fastest |
§Documentation
- API Reference - Complete API documentation
- Examples - Real-world usage examples
- Secret Loading - Authentication and secret management
- Testing Guide - Testing organization and NO MOCKING policy
- Specification - Detailed project specification
§Dependencies
- reqwest: HTTP client with async support
- tokio: Async runtime
- serde: Serialization/deserialization
- workspace_tools: Secret management
- error_tools: Unified error handling
- secrecy: Secure credential handling
All dependencies workspace-managed for consistency.
§Contributing
- Follow established patterns in existing code
- Use 2-space indentation consistently
- Add tests for new functionality
- Update documentation for public APIs
- Ensure zero clippy warnings:
cargo clippy -- -D warnings - Follow zero-tolerance mock policy (real API integration only)
- Follow the “Thin Client, Rich API” principle
§License
MIT
§Links
- Anthropic Console - Get your API key
- Claude API Documentation - Official API docs
- Examples - Comprehensive usage examples
- Specification - Technical specification Anthropic API client for Rust
This crate provides a comprehensive HTTP client for interacting with Anthropic’s Claude API. It handles authentication, request/response serialization, and streaming support.
§Governing Principle : “Thin Client, Rich API”
This library follows the principle of “Thin Client, Rich API” - exposing all server-side functionality transparently while maintaining zero client-side intelligence or automatic behaviors.
Key Distinction: The principle prohibits automatic/implicit behaviors but explicitly allows and encourages explicit/configurable enterprise reliability features.
§Core Principles
- API Transparency: One-to-one mapping with Anthropic Claude API endpoints
- Zero Automatic Behavior: No implicit decision-making or magic thresholds
- Explicit Control: Developer decides when, how, and why operations occur
- Information vs Action: Clear separation between data retrieval and state changes
- Configurable Reliability: Enterprise features available through explicit configuration
§Enterprise Reliability Features
The following enterprise reliability features are explicitly allowed when implemented with explicit configuration and transparent operation:
- Configurable Retry Logic: Exponential backoff with explicit configuration
- Circuit Breaker Pattern: Failure threshold management with transparent state
- Rate Limiting: Request throttling with explicit rate configuration
- Failover Support: Multi-endpoint configuration and automatic switching
- Health Checks: Periodic endpoint health verification and monitoring
§State Management Policy
✅ ALLOWED: Runtime-Stateful, Process-Stateless
- Connection pools, circuit breaker state, rate limiting buckets
- Retry logic state, failover state, health check state
- Runtime state that dies with the process
- No persistent storage or cross-process state
❌ PROHIBITED: Process-Persistent State
- File storage, databases, configuration accumulation
- State that survives process restarts
Implementation Requirements:
- Feature gating behind cargo features (
retry,circuit_breaker,rate_limiting,failover,health_checks) - Explicit configuration required (no automatic enabling)
- Transparent method naming (e.g.,
execute_with_retries(),execute_with_circuit_breaker()) - Zero overhead when features disabled
§Examples
use api_claude::{ Client, Secret, CreateMessageRequest, Message, Role, Content };
// Create a client
let secret = Secret::new( "sk-ant-api03-your-key-here".to_string() )?;
let client = Client::new( secret );
// Create a simple message
let request = CreateMessageRequest::builder()
.model( "claude-sonnet-4-5-20250929".to_string() )
.max_tokens( 1000 )
.messages( vec![
Message {
role : Role::User,
content : vec![ Content::Text {
r#type : "text".to_string(),
text : "Hello, Claude! How are you?".to_string()
} ],
cache_control : None,
}
] )
.build();
// Send the request
let response = client.create_message( request ).await?;
println!( "Claude responded : {:?}", response.content );Modules§
- authentication
- Advanced authentication functionality for Anthropic API
- batch
- Batch Messages API
- buffered_
streaming - Buffered Streaming for Smoother UX
- circuit_
breaker - Circuit breaker implementation for the Anthropic API client
- client
- Anthropic API client implementation
- compression
- HTTP Compression Support
- content_
generation - Content generation functionality for Anthropic API
- curl_
diagnostics - CURL diagnostics functionality for Anthropic API client
- dynamic_
config - Dynamic Configuration with Hot-Reloading
- embeddings
- Text embedding functionality for Anthropic API client
- enhanced_
function_ calling - Enhanced function calling with type-safe execution and validation
- enterprise_
config - Enterprise Configuration Module
- enterprise_
quota - Enterprise Quota Management
- environment
- Environment configuration and secret validation for Anthropic API
- error
- Error handling for the Anthropic API client
- error_
tools - Error-related exports.
- exposed
- Exposed namespace of the module.
- failover
- Failover Module
- general_
diagnostics - General diagnostics functionality for Anthropic API client
- health_
checks - Health Check Module
- input_
validation - Input validation module for Claude API requests
- messages
- Message types for Anthropic API
- model_
comparison - Model Comparison for A/B Testing
- model_
management - Model management functionality for Anthropic API
- orphan
- Orphan namespace of the module.
- own
- Own namespace of the module.
- prelude
- Prelude to use essentials:
use my_module ::prelude :: *. - rate_
limiting - Rate limiting implementation for Anthropic Claude API
- request_
caching - Request caching implementation for Anthropic Claude API
- request_
templates - Request Templates for Common Use Cases
- retry_
logic - Retry logic implementation for the Anthropic API client
- secret
- Secret management for Anthropic API
- ser
- Serde-related exports.
- streaming
- Server-Sent Events streaming support for Anthropic API
- streaming_
control - Streaming control for pause/resume/cancel operations
- sync_
api - Synchronous API functionality for Anthropic Claude API
- validators
- Reusable validation functions for Claude API parameters
Structs§
- Actionable
Error - Actionable error with remediation steps
- Adaptive
Rate Limiter - Adaptive rate limiter that adjusts based on API responses
- Adaptive
Rate Limiter Config - Configuration for adaptive rate limiting
- Aggregated
Metrics - Aggregated metrics data
- Alert
- Alert generated by the monitoring system
- Alerting
System - Alerting system
- Anthropic
ApiError - Anthropic API error response structure
- Anthropic
Rate Limit Info - Rate limit information from Anthropic API response headers
- ApiError
Wrap - Wrapper for API error responses
- Audit
Log - Audit log entry
- Auth
Metrics - Authentication metrics
- Auth
Performance Monitor - Authentication performance monitor
- Authentication
Audit Logger - Authentication audit logger
- Authentication
Error - Enhanced authentication error
- Backoff
Calculator - Backoff calculator for rate limiting
- Backoff
Strategy Details - Backoff strategy details
- Backoff
Strategy Details - Detailed backoff strategy information
- Batch
Error - Batch error aggregation
- Batch
List Response - Batch list response
- Batch
Request Item - Request item for batch processing
- Batch
Response - Batch response from API
- Batch
Result - Individual batch result from JSONL file
- Batch
Result Error - Error information in batch result
- Buffer
Config - Configuration for buffered streaming
- Buffered
Stream - Buffered stream wrapper
- Cache
Config - Configuration for request caching
- Cache
Control - Cache control configuration for prompt caching
- Cache
Metrics - Cache metrics for monitoring
- Chained
Error - Chained error result
- Circuit
Breaker - Circuit breaker implementation
- Circuit
Breaker Config - Circuit breaker configuration
- Circuit
Breaker Metrics - Circuit breaker metrics
- Client
- Anthropic API client
- Client
Config - Configuration for Anthropic API client
- Client
Config Builder - Builder for client configuration requiring explicit values
- Comparison
Results - Results from comparing multiple models
- Compression
Config - Compression configuration
- Config
Watcher - Configuration watcher for hot-reloading
- Content
Generation Request - Content generation configuration and parameters
- Content
Generation Request Builder - Builder for content generation requests
- Content
Generation Response - Content generation response with additional metadata
- Content
Generator - Content generator with advanced capabilities
- Context
Window Details - Context window details with token breakdown
- Controlled
Stream - Controlled stream wrapper
- Correlation
Summary - Correlation summary
- Correlation
Tracker - Correlation tracker for cross-request errors
- Cost
Calculator - Cost calculator for Claude models (cost per million tokens)
- Cost
Comparison - Cost comparison between models
- Cost
Estimate - Cost estimation result
- Count
Message Tokens Request - Request to count tokens in a message
- Count
Message Tokens Response - Response from token counting endpoint
- Create
Batch Request - Batch creation request
- Create
Message Request - Request to create a message
- Create
Message Request Builder - Builder for
CreateMessageRequest - Create
Message Response - Response from create message API
- Credential
Hint Generator - Credential hint generator
- Credential
Hints - Credential hints for authentication errors
- Credential
Validation Info - Credential validation information
- Curl
Builder - Helper for building cURL commands
- Custom
Error - Custom error type for domain-specific errors
- Diagnostics
Aggregator - Diagnostics aggregator for comprehensive reporting
- Diagnostics
Collector - Diagnostics collector integrating with CURL diagnostics
- Diagnostics
Context - Diagnostics context for correlation
- Diagnostics
Data - Diagnostics data structure
- Diagnostics
Exporter - Mock exporter for testing
- Diagnostics
Summary - Summary of diagnostic information
- Embedding
Data - Individual embedding data point
- Embedding
Request - Embedding request structure for future Anthropic embedding models
- Embedding
Response - Response from embedding API
- Embedding
Usage - Usage statistics for embedding requests
- Enhanced
Anthropic Error - Enhanced error with context and classification
- Enhanced
Model Capabilities - Enhanced model capabilities with detailed information
- Enhanced
Model Details - Enhanced model details with comprehensive metadata
- Enterprise
Config - Enterprise configuration combining all reliability features
- Enterprise
Config Builder - Builder for enterprise configuration
- Environment
- Environment configuration for Anthropic API
- Environment
Credentials - Environment-specific credentials
- Error
Analyzer - Error analysis and categorization
- Error
Category - Error category containing related errors
- Error
Chain - Error chain for cause relationships
- Error
Classifier - Error classifier for categorization
- Error
Context - Error context information
- Error
Localizer - Error localizer for multi-language support
- Error
Logger - Error logger with structured data
- Error
Mapper - Error mapper for HTTP status codes
- Error
Metrics - Error metrics reporter
- Error
Parser - Error parser for API responses
- Error
Recovery - Error recovery strategy analyzer
- Error
Serializer - Error serializer
- Error
Summary - Summary of all errors across categories
- Estimated
Usage - Cost estimation request
- Expiration
Status - Credential expiration status
- Explicit
Retry Builder - Zero Magic: No automatic retry decisions or hidden retry logicTransparent Control: Every retry parameter is visible and configurableInformation vs Action: Provides retry information without making retry decisions
- Failover
Config - Failover configuration and policy
- Failover
Context - Failover context representing the current state of a failover attempt
- Failover
Endpoint - Endpoint configuration for failover
- Failover
Manager - Failover manager for endpoint selection and health tracking
- Feature
Compatibility - Feature compatibility information
- Feature
Compatibility Matrix - Feature compatibility matrix
- Feature
Timeline Entry - Feature timeline entry
- Filtered
Model - Filtered model result
- Generation
Metadata - Metadata about content generation
- Generation
Settings - Advanced content generation settings
- Health
Check Config - Configuration for health checks
- Health
Check Result - Health check result for a single endpoint
- Health
Checker - Stateless health check utilities
- Health
Metrics - Health metrics aggregator for multiple check results
- Health
Status - Health status information for explicit monitoring
- Http
Error - Structured HTTP error information
- Image
Content - Image content for vision support (requires vision feature)
- Image
Source - Image source specification (requires vision feature)
- Load
Test Results - Results from a load test
- Load
Tester - Load testing integration
- Localized
Error - Localized error message
- LogEntry
- Log entry with structured data
- Message
- Message in conversation
- Message
Builder - Builder for constructing messages with multiple content types
- Migration
Path - Migration path for deprecated models
- Model
Availability - Model availability information
- Model
Capabilities - Model capabilities structure
- Model
Comparator - Model comparator for A/B testing
- Model
Comparison - Model comparison functionality
- Model
Comparison Result - Result from comparing a single model
- Model
Details Cache - Model details cache
- Model
Filter - Model filtering criteria
- Model
Filter Builder - Builder for model filter
- Model
Info - Model information structure
- Model
Lifecycle - Model lifecycle information
- Model
Limits - Model limits and constraints
- Model
Manager - Model management system
- Model
Performance - Model performance metrics
- Model
Pricing - Model pricing information
- Model
Recommendation - Model recommendation result
- Model
Requirements - Model requirements for selection
- Model
Requirements Builder - Builder for model requirements
- Model
Search Result - Model search result
- Model
Status - Model status information
- Monitoring
Status - Status information for monitoring
- Network
Error - Network error details
- Network
Error Classification - Network error classification
- Network
Error Classifier - Network error classifier
- Operation
Metrics - Metrics for operations of a specific type
- Operation
Stats - Statistics for a specific operation type
- Performance
Comparison - Performance comparison between models
- Performance
Metrics - Performance metrics collection
- Performance
Monitor - Performance monitor for tracking API and operation performance
- Performance
Profile - Performance profile for a model
- Quota
Config - Quota limits configuration
- Quota
Exceeded Error - Quota violation error
- Quota
Manager - Quota manager for tracking and enforcing usage limits
- Rate
Limit Error - Rate limiting error with Anthropic API headers
- Rate
Limit Info - Rate limit information
- Rate
Limit Info - Rate limit information for explicit control
- Rate
Limiter - Token bucket rate limiter
- Rate
Limiter Config - Configuration for rate limiting
- Rate
Limiter Metrics - Rate limiter metrics
- Rate
Limits - Rate limiting information
- Realtime
Monitor - Real-time monitoring capabilities
- Recovery
Strategy - Recovery strategy information
- Request
Cache - Request cache implementation
- Request
Context - Request context for operations
- Request
Counts - Request counts for batch status
- Request
Template - Request template for common use cases
- Request
Tracker - Request lifecycle tracker
- Response
Content - Content in response
- Retry
Config - Configuration for retry behavior
- Retry
Executor - Retry executor for running operations with retry logic
- Retry
Metrics - Metrics tracking for retry attempts
- Retry
Strategy - Retry strategy implementation
- Runtime
Config - Runtime configuration that can be hot-reloaded
- Secret
- Anthropic API key secret
- Security
Event - Security event
- Security
Monitor - Security monitor for credential transmission
- Stream
Control - Handle for controlling stream operations
- Stream
Message - Stream message structure for streaming responses
- Structured
Logger - Structured logger for detailed observability
- Sync
Client - Synchronous client wrapper around the async Client
- Sync
Client Builder - Builder for configuring synchronous clients
- Sync
Runtime - Runtime manager for synchronous operations
- Sync
Stream Iterator - Synchronous iterator wrapper around async
EventStream - System
Content - System content block for count tokens endpoint
- System
Instructions - Builder for composing multi-part system instructions
- System
Prompt - System prompt with optional cache control
- Temperature
Range - Temperature range
- Timeout
Classification - Timeout error classifier
- Timeout
Error - Timeout error details
- Token
Breakdown - Token allocation breakdown
- Tool
Definition - Tool definition for function calling
- Tool
Registry - Registry for managing and executing tools
- Tool
Result Content - Tool result content in user messages
- Tool
UseContent - Tool use content in assistant messages
- Usage
- Usage statistics
- Usage
Metrics - Usage metrics for a specific time period
- Validation
Error - Validation error with detailed failure information
- Version
Compatibility - Version compatibility information
Enums§
- Anthropic
Error - Anthropic API error types
- Backoff
Strategy - Backoff strategy types
- Backoff
Strategy Type - Backoff strategy types for error handling
- Backoff
Type - Backoff types for rate limiting
- Batch
Processing Status - Batch processing status
- Circuit
State - Circuit breaker states
- Code
Complexity - Code complexity levels
- Content
- Content block in a message
- Content
Length - Content length categories
- Embedding
Input - Input for embedding requests - can be single text or batch
- Endpoint
Health - Endpoint health status
- Endpoint
Health Status - Health status for an endpoint
- Error
Class - Error classification categories
- Error
Severity - Error severity levels
- Error
Type - Specific error types for detailed classification
- Failover
Strategy - Failover strategy for selecting endpoints
- Health
Check Strategy - Health check strategy
- LogLevel
- Log level for structured logging
- LogSeverity
- Log severity levels
- Network
Error Type - Network error types
- Request
Result - Request result enumeration
- Request
Status - Request status enumeration
- Retry
Strategy Type - Types of retry strategies
- Role
- Message role in conversation
- Stream
Content Block - Content block in streaming response
- Stream
Delta - Delta updates for streaming content
- Stream
Event - Streaming events from Server-Sent Events
- Stream
State - State of a controlled stream
- Timeout
Type - Timeout error types
- Tool
Choice - Choice for how the model should use tools
- UseCase
- Use case definitions for model recommendations
- Writing
Tone - Writing tone styles
Constants§
- ANTHROPIC_
API_ BASE_ URL - Standard base URL for Anthropic API (no longer a magic default)
- ANTHROPIC_
API_ VERSION - Standard API version header value (no longer a magic default)
- RECOMMENDED_
MODEL - Current recommended model (no longer a magic default)
Traits§
- AsCurl
- Trait for converting API requests to equivalent cURL commands
- AsCurl
Client - Trait for client-level cURL generation with authentication
- Stream
Buffer Ext - Extension trait for streams
- Tool
Executor - Trait for executable tools with type-safe parameter handling
- Validate
- Trait for validating request parameters
Functions§
- add_
compression_ headers - Add compression headers to request
- compress
- Compress data using gzip
- create_
parameter_ schema - Helper for creating a parameter schema
- create_
tool_ definition - Helper for creating a simple tool definition
- decompress
- Decompress gzip-compressed data
- is_gzip
- Check if data appears to be gzip-compressed
- map_
deserialization_ error - Map deserialization error to
AnthropicError - parse_
sse_ events - Parse Server-Sent Events data into stream events
- secret_
diagnostic_ info - Get diagnostic information about secret loading configuration
- validate_
anthropic_ secret - Validate that required secrets are available for API operations
- validate_
workspace_ structure - Check if workspace secrets directory exists and is properly configured
Type Aliases§
- Anthropic
Result - Result type for Anthropic API operations
- Event
Stream - Stream of Server-Sent Events
- Tool
Result - Result type for tool execution