Overview
TheAnthropicClient provides a unified interface for interacting with Anthropic’s Claude models, including Claude 3.7 Sonnet, Claude 4.5, and Haiku variants.
Installation
Basic Usage
Constructor
LLMConfig | None
default:"None"
Configuration object. If
None, creates default config with:api_keyfromANTHROPIC_API_KEYenvironment variablemodelset to"claude-haiku-4-5-latest"max_tokensfrom parameter (default: 16384)
bool
default:"False"
Enable response caching (stored in
./llm_cache)AsyncAnthropic | None
default:"None"
Optional pre-configured
AsyncAnthropic client instance. If not provided, creates one from config.int
default:"16384"
Maximum output tokens. Defaults to 16384, but see model-specific limits below.
Supported Models
The client supports all Claude models with model-specific max token limits:Claude 4.5 (64K output)
claude-sonnet-4-5-latestclaude-sonnet-4-5-20250929claude-haiku-4-5-latest
Claude 3.7 Sonnet (64K output)
claude-3-7-sonnet-latestclaude-3-7-sonnet-20250219
Claude 3.7 supports up to 128K output tokens with the
anthropic-beta: output-128k-2025-02-19 header, but this is not currently implemented.Claude 3.5 (8K output)
claude-3-5-haiku-latestclaude-3-5-haiku-20241022claude-3-5-sonnet-latestclaude-3-5-sonnet-20241022claude-3-5-sonnet-20240620
Claude 3 (4K output)
claude-3-opus-latestclaude-3-opus-20240229claude-3-sonnet-20240229claude-3-haiku-20240307
Claude 2 (4K output)
claude-2.1claude-2.0
Max Tokens Resolution
The client uses intelligent max token resolution with the following precedence:- Explicit parameter to
generate_response() - Instance max_tokens set during initialization
- Model-specific maximum from the mapping above
- Default fallback: 8192 tokens
Structured Output via Tools
The client uses Anthropic’s tool-calling API for structured outputs:Error Handling
The client implements comprehensive error handling:Rate Limits
Content Policy Violations
Automatic Retries
The client automatically retries (max 2 times) for:- Validation errors (with error context for self-correction)
- Transient API errors
- JSON parsing failures
Token Usage Tracking
The client tracks token usage from the API response:JSON Fallback Extraction
If tool use fails, the client attempts to extract JSON from text:Example: Multi-turn Conversation
Performance Tips
- Use Haiku for simple tasks: Claude Haiku is 3x cheaper and faster than Sonnet
- Set appropriate max_tokens: Don’t request 64K if you only need 1K
- Enable caching for repeated queries with same context
- Use model_size parameter: Let Graphiti choose the right model