Skip to main content

anthropic_client_rs/
lib.rs

1// Copyright (c) 2025 xiefujin <490021684@qq.com>
2// Licensed under Apache-2.0, see LICENSE file for full license terms.
3
4//! # anthropic-client-rs
5//!
6//! A Rust client for the Anthropic Messages API — streaming SSE, tool use,
7//! extended thinking, and prompt caching. Compatible with Claude, MiniMax,
8//! DeepSeek-anthropic, and any Anthropic-compatible provider.
9//!
10//! ## Features
11//!
12//! - **Sync** (default) — lightweight `ureq`
13//! - **Async** — optional `reqwest` + `tokio` (`features = ["async"]`)
14//! - **Messages API** — streaming SSE with content block deltas
15//! - **Tool use** — parallel tool calling with partial JSON accumulation
16//! - **Extended thinking** — thinking blocks with reasoning support
17//! - **Vision** — image inputs (base64, URL)
18//! - **System prompts** — top-level `system` parameter
19//! - **Prompt caching** — cache_control breakpoints
20//! - **Auto-retry** — exponential backoff with jitter
21//! - **Token counting** — approximate message token estimation
22//! - **Multi-provider** — Anthropic, MiniMax, DeepSeek
23//!
24//! ## Quick start
25//!
26//! ```rust,no_run
27//! use anthropic_client_rs::{AnthropicClient, ChatMessage};
28//!
29//! let client = AnthropicClient::new("sk-ant-xxx", "claude-sonnet-4-20250514");
30//! let resp = client.messages_create(
31//!     &[ChatMessage::user("Hello!")],
32//!     Some("You are helpful."), None, 1024, None,
33//! ).unwrap();
34//! println!("{}", resp.text);
35//! ```
36
37pub mod api_common;
38pub mod cache;
39pub mod error;
40pub mod request;
41pub mod retry;
42pub mod sse;
43pub mod tokens;
44pub mod types;
45
46mod client;
47mod messages;
48
49#[cfg(feature = "async")]
50mod async_client;
51#[cfg(feature = "async")]
52pub mod async_messages;
53#[cfg(feature = "async")]
54pub mod async_sse;
55
56#[cfg(feature = "async")]
57pub use async_client::AnthropicAsyncClient;
58pub use cache::CacheConfig;
59pub use client::AnthropicClient;
60pub use error::{AnthropicError, ApiError, Result};
61pub use messages::{parse_anthropic_stream, StreamEventStream};
62pub use request::{MessageRequest, Metadata, ThinkingConfig, ThinkingDisplay};
63pub use retry::RetryConfig;
64pub use types::{
65    ChatMessage, ContentBlock, CountTokensResponse, DocumentSource, ImageSource, LlmResponse,
66    MessageResponse, OutputTokensDetails, ServerToolUse, SimplifiedToolCall, StreamEvent, Tool,
67    ToolCall, ToolChoice, Usage,
68};