AGENTS.md
Documents the architecture, API managers, development commands, and rules for an Anthropic Claude Rust client library.
What this file does
Documents the architecture, API managers, development commands, and rules for an Anthropic Claude Rust client library.
When to use it
- Onboarding to the anthropic-rust codebase
- Adding a new API endpoint or manager
- Understanding the manager-style and builder patterns used
- Setting up local development and running examples
Assumes this stack
AGENTS.md
This file provides guidance to agents or humans when working with code in this repository.
Architecture Overview
This is an Anthropic Claude API client library written in Rust, designed with a modular architecture that separates concerns between the client and API endpoint managers. The library implements a chain-style API pattern similar to modern cloud SDKs.
Core Structure
anthropic-rust/src/
├── lib.rs # Library entry point with re-exports
├── client.rs # Main Client with builder pattern, HTTP handling
├── message.rs # Message API manager (create, stream, count_tokens)
├── batch.rs # Message Batches API manager
├── model.rs # Models API manager
└── types/ # Type definitions
├── mod.rs # Module exports
├── messages.rs # Messages API types (requests, responses, content blocks)
├── batches.rs # Batch API types
└── models.rs # Models API types
Key Design Patterns:
- Manager-style API:
client.messages().create(request).await - Builder pattern:
Client::builder().api_key(key).build()? - Request builders:
CreateMessageRequest::builder().model("...").build()?
Client
The Client struct is the primary entry point:
- Manages HTTP client, API key, base URL, and API version
- Provides manager accessor methods:
messages(),batches(),models() - Supports custom configuration via
ClientBuilder - Handles request/response serialization and error handling
- Supports beta feature flags via
beta()method
Message (API Manager)
Message provides access to the Messages API:
create()- send a message and get a complete responsecreate_stream()- SSE streaming for incremental responsescount_tokens()- pre-flight token estimation
Batch (API Manager)
Batch provides access to the Message Batches API:
create()- create a new batch of message requestsget()- retrieve batch statuslist()- list all batchescancel()- cancel a processing batchdelete()- delete a completed batch
Model (API Manager)
Model provides access to the Models API:
list()- list available modelsget()- get details for a specific model
Type System
- All types are exported from
types::* - Request/response types mirror the Anthropic API structure
- Content blocks support: text, image, document, tool_use, tool_result, thinking, redacted_thinking
- Stream types use
async-streamfor Server-Sent Events parsing - Full support for tool use, extended thinking, and caching
Development Commands
Building and Testing
# Build the library
cargo build
# Check compilation (faster)
cargo check
# Run tests
cargo test
Running Examples
# Basic message example
cargo run --example messages
# Streaming example
cargo run --example streaming
# Batches example
cargo run --example batches
# Models example
cargo run --example models
Workspace Structure
This is a Cargo workspace with:
- Main library:
anthropic-rust/ - Examples:
examples/*/(each is its own crate)
Development Rules
Core Reference Materials:
- Anthropic Claude API documentation (primary reference)
- Official Anthropic API specifications
Development Workflow:
- Compilation: Always compile after code changes with
cargo checkorcargo build - Testing: Write unit tests for new functionality, run with
cargo test - Examples: Create examples for each API capability in
examples/[name]/ - Documentation: Keep docs concise and current
Environment Variables:
Examples expect:
ANTHROPIC_API_KEY- API key for Anthropic Claude APIANTHROPIC_BASE_URL- Optional base URL override
Load via .env file using dotenvy::dotenv().
Adding New API Endpoints
- Add type definitions in
types/[name].rsif needed - Add API manager struct in
[name].rswith methods - Add manager accessor to
Clientinclient.rs - Update
types/mod.rsandlib.rsto export new types - Create example in
examples/[name]/ - Update documentation
API Coverage Status
Currently implemented:
- Messages API (
/v1/messages) - Streaming Messages API (SSE)
- Token Counting API (
/v1/messages/count_tokens) - Message Batches API (
/v1/messages/batches) - Models API (
/v1/models)
Features supported:
- Text generation
- Vision (images)
- Documents (PDF, text)
- Tool use (function calling)
- Extended thinking
- Prompt caching
- Server-side tools (web search)
What's inside
7 sections: architecture overview, 3 API managers, type system, development commands, workspace structure, development rules, API coverage status
Change this for your project
- Replace
wanggang316/anthropic-rustwith your own repository name - Replace
anthropic-rust/with your own crate path - Replace
examples/[name]/with your own example directory pattern
Where it goes
Save as AGENTS.md in your repository root. Read by Codex, Cursor and other agents that follow the AGENTS.md convention.
Worth borrowing
- Manager-style API accessors on the Client struct for modular endpoint groups
- Request builders mirroring the API structure for type-safe construction
- Separate types module per API domain for clear separation of concerns
Related Documents
Browser-only development
Guides AI assistants on an Electron + React + TypeScript desktop app for browsing and organizing AI-generated images locally.
Claude Agents — Reference & Recommendations
Catalogues 40+ Claude agents and marketing skills for building a cat adoption charity landing page, with a ready-to-paste prompt and backend API reference.
Golden DKG Prototype -- Master Plan
Defines an 8-phase implementation plan for a Rust prototype of the Golden non-interactive DKG protocol using BLS12-381 and tokio.
Swarms Examples Index
Lists 60+ example scripts for building single and multi-agent systems with the Swarms framework, organized by category and use case.