rust-claude-sdk
A small typed Rust client for Anthropic's Messages API (POST /v1/messages) and the endpoints around it, over raw HTTPS, following the documented wire format.
- Requests with system prompts,
documentblocks with citations, images, tools, thinking and effort settings,cache_controlbreakpoints and server-side refusalfallbacks(the matchinganthropic-betaheader is added automatically). - Responses with text, citations, thinking, tool calls and fallback markers. Unknown block, citation, event and delta types are kept as raw JSON (
Other) instead of failing, so a new API feature never breaks decoding and assistant turns echo back intact. - Retries on connection errors, 408, 409, 429 and 5xx (honouring
retry-afterandx-should-retry), bounded by an optional deadline that also cuts retries short. - Streaming over server-sent events, event by event or accumulated into the final message.
- Server tools: web search, web fetch, code execution and tool search, with typed definitions, typed result blocks (errors included), web search citations and
usage.server_tool_use. POST /v1/messages/count_tokens, Message Batches (results streamed line by line), the Files API and the Models API.
Usage
[]
= "0.2"
use ;
let client = new?;
let request = new
.system_cached
.message;
let message = client.create.await?;
println!;
Web search
use ;
let request = new
.tool
.user;
let message = client.create.await?;
for in message.citations
for in message.server_tool_errors
Batches, files and models
use ;
let batch = client.create_batch.await?;
// ...poll `client.batch(&batch.id)` until `is_ended()`, then:
let mut results = client.batch_results.await?;
while let Some = results.next.await
let file = client.upload_file.await?;
let request = new
.message;
let opus = client.model.await?;
println!;
let page = client.list_models.await?;
A streaming, grounded example: ANTHROPIC_API_KEY=... cargo run --example grounded -- "În cât timp primesc buletinul?"
Development
Rust ≥ 1.85.
Tests run against a local mock server and need no API key.
Releasing
Bump version in Cargo.toml, merge to main, then push a matching tag:
The Release workflow checks that the tag matches the crate version, runs the checks above and publishes to crates.io with the CARGO_REGISTRY_TOKEN repository secret.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Originally written as the rogov-anthropic crate in aoprisan/govromania.