1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Type-safe Rust client for the [Anthropic API](https://docs.anthropic.com/).
//!
//! Every documented Anthropic endpoint is reachable through a typed
//! namespace handle off [`Client`]. Forward-compatible by design --
//! unknown content blocks, stream events, citations, and similar
//! discriminated unions round-trip through `Other(Value)` arms so
//! new server-side variants don't break older SDK builds.
//!
//! # Quick start
//!
//! ```no_run
//! use claude_api::{Client, messages::CreateMessageRequest, types::ModelId};
//!
//! # async fn run() -> Result<(), claude_api::Error> {
//! let client = Client::new(std::env::var("ANTHROPIC_API_KEY").unwrap());
//!
//! let resp = client
//! .messages()
//! .create(
//! CreateMessageRequest::builder()
//! .model(ModelId::SONNET_4_6)
//! .max_tokens(256)
//! .user("Hello!")
//! .build()?,
//! )
//! .await?;
//!
//! for block in &resp.content {
//! if let claude_api::messages::ContentBlock::Known(
//! claude_api::messages::KnownBlock::Text { text, .. },
//! ) = block
//! {
//! println!("{text}");
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Module map
//!
//! Endpoints are organized by Anthropic resource. Each namespace
//! handle is reached via a method on [`Client`]:
//!
//! | Module | Namespace | Default? |
//! |---|---|---|
//! | [`messages`] | `client.messages()` | yes |
//! | [`models`] | `client.models()` | yes |
//! | [`batches`] | `client.batches()` | yes |
//! | [`files`] | `client.files()` | yes |
//! | [`skills`] | `client.skills()` | feature `skills` |
//! | [`user_profiles`] | `client.user_profiles()` | feature `user-profiles` |
//! | [`managed_agents`] | `client.managed_agents()` | feature `managed-agents-preview` |
//! | [`admin`] | `client.admin()` | feature `admin` |
//!
//! Cross-cutting machinery:
//!
//! | Module | Purpose |
//! |---|---|
//! | [`auth`] | API-key wrapper + [`auth::RequestSigner`] trait |
//! | [`bedrock`] | AWS sigv4 [`bedrock::BedrockSigner`] (feature `bedrock`) |
//! | [`vertex`] | GCP `OAuth2` [`vertex::VertexSigner`] (feature `vertex`) |
//! | [`beta`] | [`BetaHeader`] open-string enum for the `anthropic-beta` header |
//! | [`retry`] | [`retry::RetryPolicy`] honoring `Retry-After` |
//! | [`error`] | [`Error`] with `request_id`, retry classification |
//! | [`pagination`] | [`Paginated`](pagination::Paginated) and [`PaginatedNextPage`](pagination::PaginatedNextPage) |
//! | [`types`] | Shared primitives: [`ModelId`](types::ModelId), [`Role`](types::Role), [`Usage`](types::Usage), [`StopReason`](types::StopReason) |
//! | [`conversation`] | Multi-turn helper with cumulative usage (feature `conversation`) |
//! | [`tool_dispatch`] | [`ToolRegistry`](tool_dispatch::ToolRegistry), agent loop, parallel dispatch, approval gates |
//! | [`pricing`] | [`PricingTable`](pricing::PricingTable) (feature `pricing`) |
//! | [`cost_preview`] | Pre-flight USD estimates (features `async` + `pricing`) |
//! | [`dry_run`] | Render the would-be HTTP request without sending |
//! | [`sse`] | SSE wrapper used by streaming endpoints (feature `streaming`) |
//!
//! # Forward compatibility
//!
//! Every wire-level discriminated union has an `Other` arm:
//!
//! - [`messages::ContentBlock`] -- text / image / `tool_use` /
//! thinking / ... / `Other(Value)`
//! - [`messages::KnownBlock`] -- the typed variants
//! - [`messages::stream::StreamEvent`] -- SSE events
//! - [`messages::citation::Citation`] -- citation kinds
//! - [`BetaHeader`] -- known beta header values + `Other(String)`
//!
//! When Anthropic adds a new variant, your code that pattern-matches
//! on `Known(...)` continues to compile; the new variant lands in
//! `Other(...)` until you upgrade. See the **upgrade contract** in
//! the README before bumping past a release that promotes an `Other`
//! to `Known`.
//!
//! # Error handling
//!
//! All endpoint methods return [`Result<T>`](Result) where
//! [`Error`] carries:
//!
//! - HTTP status (when the API responded)
//! - `request-id` (always populated when the server sent one)
//! - Retry classification (`is_retryable()`) and parsed
//! `Retry-After` (`retry_after()`)
//! - Structured kind ([`error::ApiErrorKind`]) for known
//! error types
//!
//! The [`retry::RetryPolicy`] applied by the client respects
//! `Retry-After` by default and uses jittered exponential backoff
//! otherwise. Disable retries with `RetryPolicy::none()` if your
//! caller wraps its own retry logic.
//!
//! # Observability
//!
//! Every HTTP request emits a `tracing` span at `debug` level with
//! `method`, `path`, and `request_id` fields. Retries log at `warn`
//! with `attempt`, `retry_in_ms`, and `status`. No env-var gating
//! is required; install a `tracing-subscriber` to capture the
//! events.
pub const ANTHROPIC_VERSION: &str = "2023-06-01";
pub const DEFAULT_BASE_URL: &str = "https://api.anthropic.com";
pub const USER_AGENT: &str = concat!;
pub
pub use BetaHeader;
pub use ;
pub use ;
/// Procedural-macro re-exports for the `derive` feature.
///
/// Bring [`Tool`](crate::derive::Tool) into scope to derive
/// [`tool_dispatch::Tool`] on a struct that
/// implements [`serde::Deserialize`] and [`schemars::JsonSchema`].
/// Implementation detail: re-exports used by macros generated by the
/// `derive` feature. Not part of the public API; do not use directly.