mixtape_anthropic_sdk/lib.rs
1//! Minimal Anthropic API client for mixtape
2//!
3//! This crate provides a lightweight, focused client for the Anthropic Messages API.
4//! It supports both regular and streaming message creation with tool use.
5//!
6//! # Quick Start
7//!
8//! ```no_run
9//! // Requires ANTHROPIC_API_KEY environment variable
10//! use mixtape_anthropic_sdk::{Anthropic, MessageCreateParams};
11//!
12//! # #[tokio::main]
13//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
14//! let client = Anthropic::from_env()?;
15//!
16//! let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
17//! .user("Hello, Claude!")
18//! .build();
19//!
20//! let response = client.messages().create(params).await?;
21//! println!("{:?}", response);
22//! # Ok(())
23//! # }
24//! ```
25//!
26//! # Streaming Responses
27//!
28//! For long responses, streaming provides a better user experience:
29//!
30//! ```no_run
31//! // Requires ANTHROPIC_API_KEY environment variable
32//! use mixtape_anthropic_sdk::{Anthropic, MessageCreateParams};
33//!
34//! # #[tokio::main]
35//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
36//! let client = Anthropic::from_env()?;
37//! let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
38//! .user("Tell me a story")
39//! .build();
40//!
41//! // Collect all text in one call
42//! let stream = client.messages().stream(params.clone()).await?;
43//! let text = stream.collect_text().await?;
44//!
45//! // Or reconstruct the full message
46//! let stream = client.messages().stream(params).await?;
47//! let message = stream.collect_message().await?;
48//! println!("Stop reason: {:?}", message.stop_reason);
49//! # Ok(())
50//! # }
51//! ```
52//!
53//! # Tool Use
54//!
55//! Define tools for the model to use:
56//!
57//! ```no_run
58//! // Requires ANTHROPIC_API_KEY environment variable
59//! use mixtape_anthropic_sdk::{
60//! Anthropic, MessageCreateParams, Tool, ToolInputSchema, ToolChoice, ContentBlock,
61//! };
62//!
63//! # #[tokio::main]
64//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
65//! let client = Anthropic::from_env()?;
66//!
67//! let tool = Tool {
68//! name: "get_weather".to_string(),
69//! description: Some("Get the current weather for a location".to_string()),
70//! input_schema: ToolInputSchema::new(),
71//! cache_control: None,
72//! tool_type: None,
73//! };
74//!
75//! let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
76//! .user("What's the weather in San Francisco?")
77//! .tools(vec![tool])
78//! .tool_choice(ToolChoice::auto())
79//! .build();
80//!
81//! let response = client.messages().create(params).await?;
82//!
83//! for block in &response.content {
84//! if let ContentBlock::ToolUse { id, name, input } = block {
85//! println!("Tool {} called with input: {}", name, input);
86//! }
87//! }
88//! # Ok(())
89//! # }
90//! ```
91//!
92//! # Rate Limits and Raw Response
93//!
94//! Access rate limit information and request IDs for debugging:
95//!
96//! ```no_run
97//! // Requires ANTHROPIC_API_KEY environment variable
98//! use mixtape_anthropic_sdk::{Anthropic, MessageCreateParams};
99//!
100//! # #[tokio::main]
101//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
102//! let client = Anthropic::from_env()?;
103//! let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 1024)
104//! .user("Hello!")
105//! .build();
106//!
107//! let response = client.messages().create_with_metadata(params).await?;
108//!
109//! println!("Response: {:?}", response.data);
110//!
111//! if let Some(request_id) = response.request_id() {
112//! println!("Request ID: {}", request_id);
113//! }
114//! if let Some(rate_limit) = response.rate_limit() {
115//! println!("Requests remaining: {:?}", rate_limit.requests_remaining);
116//! }
117//! # Ok(())
118//! # }
119//! ```
120//!
121//! # Retry Configuration
122//!
123//! Configure automatic retry behavior:
124//!
125//! ```
126//! use mixtape_anthropic_sdk::{Anthropic, RetryConfig};
127//! use std::time::Duration;
128//!
129//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
130//! let client = Anthropic::builder()
131//! .api_key("your-api-key")
132//! .max_retries(5)
133//! .build()?;
134//!
135//! // Or with full control
136//! let client = Anthropic::builder()
137//! .api_key("your-api-key")
138//! .retry_config(RetryConfig {
139//! max_retries: 3,
140//! base_delay: Duration::from_millis(500),
141//! max_delay: Duration::from_secs(10),
142//! jitter: 0.25,
143//! })
144//! .build()?;
145//! # Ok(())
146//! # }
147//! ```
148//!
149//! # Extended Thinking
150//!
151//! Enable extended thinking for complex reasoning tasks:
152//!
153//! ```
154//! use mixtape_anthropic_sdk::MessageCreateParams;
155//!
156//! let params = MessageCreateParams::builder("claude-sonnet-4-20250514", 16000)
157//! .user("Solve this complex math problem...")
158//! .thinking(4096) // Allow 4096 tokens for thinking
159//! .build();
160//! ```
161
162// Domain modules
163pub mod batch;
164mod client;
165mod error;
166pub mod messages;
167pub mod streaming;
168pub mod tokens;
169pub mod tools;
170
171// Client types
172pub use client::{
173 Anthropic, AnthropicBuilder, BatchListOptions, Batches, Messages, RateLimitInfo, RawResponse,
174 Response,
175};
176
177// Error types
178pub use error::{AnthropicError, ApiError, ApiErrorResponse, RetryConfig};
179
180// Streaming
181pub use streaming::{
182 ContentBlockDelta, DeltaUsage, MessageDeltaData, MessageStream, MessageStreamEvent,
183};
184
185// Messages - request types
186pub use messages::{
187 BetaFeature, CacheControl, CacheTtl, CitationsConfig, ContentBlockParam, DocumentSource,
188 ImageSource, MessageContent, MessageCreateParams, MessageCreateParamsBuilder, MessageParam,
189 Metadata, Role, ServiceTier, ThinkingConfig, ToolResultContent, ToolResultContentBlock,
190 WebSearchErrorCode, WebSearchResult, WebSearchToolResultContent, WebSearchToolResultError,
191};
192
193// Messages - response types
194pub use messages::{ContentBlock, Message, StopReason, Usage};
195
196// Tools
197pub use tools::{Tool, ToolChoice, ToolInputSchema};
198
199// Batch API
200pub use batch::{
201 BatchCreateParams, BatchError, BatchListResponse, BatchRequest, BatchRequestCounts,
202 BatchResult, BatchResultType, BatchStatus, MessageBatch,
203};
204
205// Token counting
206pub use tokens::{CountTokensParams, CountTokensParamsBuilder, CountTokensResponse};