| Crates.io | anthropic-async |
| lib.rs | anthropic-async |
| version | 0.2.1 |
| created_at | 2025-12-02 23:25:35.558371+00 |
| updated_at | 2026-01-04 15:00:23.34247+00 |
| description | Anthropic API client for Rust with prompt caching support |
| homepage | |
| repository | https://github.com/allisoneer/agentic_auxilary |
| max_upload_size | |
| id | 1962842 |
| size | 237,236 |
A production-ready Anthropic API client for Rust with prompt caching support.
Add to your Cargo.toml:
[dependencies]
anthropic-async = "0.1.0"
use anthropic_async::{Client, types::messages::*};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Client will use ANTHROPIC_API_KEY environment variable
let client = Client::new();
let req = MessagesCreateRequest {
model: "claude-3-5-sonnet".into(),
max_tokens: 100,
messages: vec![Message {
role: MessageRole::User,
content: vec![ContentBlock::Text {
text: "Hello, Claude!".into(),
cache_control: None,
}],
}],
system: None,
temperature: None,
};
let response = client.messages().create(req).await?;
println!("{:?}", response.content);
Ok(())
}
The client supports two authentication methods:
// From environment variable
let client = Client::new(); // Uses ANTHROPIC_API_KEY
// Explicit
let config = AnthropicConfig::new()
.with_api_key("sk-ant-api03-...");
let client = Client::with_config(config);
// From environment variable
// Set ANTHROPIC_AUTH_TOKEN
let client = Client::new();
// Explicit
let config = AnthropicConfig::new()
.with_bearer("your-oauth-token");
let client = Client::with_config(config);
Reduce costs and latency with prompt caching:
use anthropic_async::types::common::CacheControl;
let req = MessagesCreateRequest {
model: "claude-3-5-sonnet".into(),
max_tokens: 100,
system: Some(vec![
ContentBlock::Text {
text: "You are an expert programmer.".into(),
cache_control: Some(CacheControl::ephemeral_1h()),
}
]),
messages: vec![Message {
role: MessageRole::User,
content: vec![ContentBlock::Text {
text: "Explain Rust ownership.".into(),
cache_control: Some(CacheControl::ephemeral_5m()),
}],
}],
temperature: None,
};
Enable beta features using the configuration:
use anthropic_async::config::BetaFeature;
let config = AnthropicConfig::new()
.with_beta_features([
BetaFeature::PromptCaching20240731,
BetaFeature::ExtendedCacheTtl20250411,
]);
Or use custom beta strings:
let config = AnthropicConfig::new()
.with_beta(vec!["new-beta-feature"]);
The client automatically retries on:
Retries use exponential backoff and respect Retry-After headers.
match client.messages().create(req).await {
Ok(response) => println!("Success: {:?}", response),
Err(AnthropicError::Api(error)) => {
println!("API error: {} ({})", error.message, error.r#type.unwrap_or_default());
}
Err(e) => println!("Other error: {}", e),
}
See the examples/ directory for complete examples:
01-basic-completion - Simple message creation04-model-listing - List available models05-kitchen-sink - All features demonstrationRun an example:
cd anthropic_async/examples/01-basic-completion && cargo run
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT OR Apache-2.0 license.