Expand description
Minimal Anthropic API client for mixtape
This crate provides a lightweight, focused client for the Anthropic Messages API. It supports both regular and streaming message creation with tool use.
§Quick Start
// Requires ANTHROPIC_API_KEY environment variable
use mixtape_anthropic_sdk::{Anthropic, MessageCreateParams};
let client = Anthropic::from_env()?;
let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
.user("Hello, Claude!")
.build();
let response = client.messages().create(params).await?;
println!("{:?}", response);§Streaming Responses
For long responses, streaming provides a better user experience:
// Requires ANTHROPIC_API_KEY environment variable
use mixtape_anthropic_sdk::{Anthropic, MessageCreateParams};
let client = Anthropic::from_env()?;
let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
.user("Tell me a story")
.build();
// Collect all text in one call
let stream = client.messages().stream(params.clone()).await?;
let text = stream.collect_text().await?;
// Or reconstruct the full message
let stream = client.messages().stream(params).await?;
let message = stream.collect_message().await?;
println!("Stop reason: {:?}", message.stop_reason);§Tool Use
Define tools for the model to use:
// Requires ANTHROPIC_API_KEY environment variable
use mixtape_anthropic_sdk::{
Anthropic, MessageCreateParams, Tool, ToolInputSchema, ToolChoice, ContentBlock,
};
let client = Anthropic::from_env()?;
let tool = Tool {
name: "get_weather".to_string(),
description: Some("Get the current weather for a location".to_string()),
input_schema: ToolInputSchema::new(),
cache_control: None,
tool_type: None,
};
let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
.user("What's the weather in San Francisco?")
.tools(vec![tool])
.tool_choice(ToolChoice::auto())
.build();
let response = client.messages().create(params).await?;
for block in &response.content {
if let ContentBlock::ToolUse { id, name, input } = block {
println!("Tool {} called with input: {}", name, input);
}
}§Rate Limits and Raw Response
Access rate limit information and request IDs for debugging:
// Requires ANTHROPIC_API_KEY environment variable
use mixtape_anthropic_sdk::{Anthropic, MessageCreateParams};
let client = Anthropic::from_env()?;
let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
.user("Hello!")
.build();
let response = client.messages().create_with_metadata(params).await?;
println!("Response: {:?}", response.data);
if let Some(request_id) = response.request_id() {
println!("Request ID: {}", request_id);
}
if let Some(rate_limit) = response.rate_limit() {
println!("Requests remaining: {:?}", rate_limit.requests_remaining);
}§Retry Configuration
Configure automatic retry behavior:
use mixtape_anthropic_sdk::{Anthropic, RetryConfig};
use std::time::Duration;
let client = Anthropic::builder()
.api_key("your-api-key")
.max_retries(5)
.build()?;
// Or with full control
let client = Anthropic::builder()
.api_key("your-api-key")
.retry_config(RetryConfig {
max_retries: 3,
base_delay: Duration::from_millis(500),
max_delay: Duration::from_secs(10),
jitter: 0.25,
})
.build()?;§Extended Thinking
Enable extended thinking for complex reasoning tasks:
use mixtape_anthropic_sdk::MessageCreateParams;
let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 16000)
.user("Solve this complex math problem...")
.thinking(4096) // Allow 4096 tokens for thinking
.build();Re-exports§
pub use streaming::ContentBlockDelta;pub use streaming::DeltaUsage;pub use streaming::MessageDeltaData;pub use streaming::MessageStream;pub use streaming::MessageStreamEvent;pub use messages::BetaFeature;pub use messages::CacheControl;pub use messages::CacheTtl;pub use messages::CitationsConfig;pub use messages::ContentBlockParam;pub use messages::DocumentSource;pub use messages::ImageSource;pub use messages::MessageContent;pub use messages::MessageCreateParams;pub use messages::MessageCreateParamsBuilder;pub use messages::MessageParam;pub use messages::Metadata;pub use messages::Role;pub use messages::ServiceTier;pub use messages::ThinkingConfig;pub use messages::ToolResultContent;pub use messages::ToolResultContentBlock;pub use messages::WebSearchErrorCode;pub use messages::WebSearchResult;pub use messages::WebSearchToolResultContent;pub use messages::WebSearchToolResultError;pub use messages::ContentBlock;pub use messages::Message;pub use messages::StopReason;pub use messages::Usage;pub use tools::Tool;pub use tools::ToolChoice;pub use tools::ToolInputSchema;pub use batch::BatchCreateParams;pub use batch::BatchError;pub use batch::BatchListResponse;pub use batch::BatchRequest;pub use batch::BatchRequestCounts;pub use batch::BatchResult;pub use batch::BatchResultType;pub use batch::BatchStatus;pub use batch::MessageBatch;pub use tokens::CountTokensParams;pub use tokens::CountTokensParamsBuilder;pub use tokens::CountTokensResponse;
Modules§
- batch
- Batch API types for the Anthropic Messages API
- messages
- Message types for the Anthropic Messages API
- streaming
- Streaming support for the Anthropic API
- tokens
- Token counting types for the Anthropic Messages API
- tools
- Tool definitions for the Anthropic Messages API
Structs§
- Anthropic
- Anthropic API client
- Anthropic
Builder - Builder for Anthropic client configuration
- ApiError
- API error details
- ApiError
Response - API error response wrapper
- Batch
List Options - Options for listing batches
- Batches
- Batches API handle
- Messages
- Messages API handle
- Rate
Limit Info - Rate limit information from response headers
- RawResponse
- Raw HTTP response metadata for debugging
- Response
- A response with both parsed data and raw HTTP metadata
- Retry
Config - Configuration for automatic retry behavior
Enums§
- Anthropic
Error - Errors that can occur when using the Anthropic API