Implementation Details
Source:~/workspace/source/clients/agent-runtime/src/providers/anthropic.rs:10
The Anthropic provider implements:
- ✅ Native tool calling with
input_schema - ✅ Streaming responses
- ✅ Automatic prompt caching for system prompts and tools
- ✅ OAuth setup token support (setup-tokens)
- ✅ Multi-turn conversations
- ✅ Connection warmup
Configuration
Basic Setup
In~/.config/corvus/config.toml:
API Key Setup
Anthropic supports two authentication methods:Method 1: API Key (Standard)
Method 2: OAuth Setup Token
~/workspace/source/clients/agent-runtime/src/providers/mod.rs:328):
- Explicit
api_keyparameter (trimmed) ANTHROPIC_OAUTH_TOKENenvironment variable (setup-tokens)ANTHROPIC_API_KEYenvironment variable (regular API keys)CORVUS_API_KEYfallbackAPI_KEYfallback
~/workspace/source/clients/agent-runtime/src/providers/anthropic.rs:172):
- Setup tokens use
Authorization: Bearerheader +anthropic-beta: oauth-2025-04-20 - Regular API keys use
x-api-keyheader
Supported Models
Claude 3.5 Series (Latest)
- 200K context window
- Superior reasoning and coding
- Extended thinking capability
- ~15/M output tokens
- 200K context window
- 3x faster than Sonnet
- ~4/M output tokens
Claude 3 Series
Prompt Caching
Anthropic’s prompt caching reduces costs by ~90% for repeated content. Corvus implements automatic caching based on heuristics:Automatic Caching Rules
From~/workspace/source/clients/agent-runtime/src/providers/anthropic.rs:190-198:
- System prompts > 3KB (automatically cached)
- Last message in conversations > 4 messages
- Last tool definition (caches entire tool set)
Cache Control Format
Corvus uses theephemeral cache type:
Caching Benefits
| Scenario | Without Cache | With Cache | Savings |
|---|---|---|---|
| Large system prompt (10K tokens) | $0.03 | $0.003 | 90% |
| Long conversation (50 messages) | $0.15 | $0.015 | 90% |
| Tool definitions (20 tools) | $0.02 | $0.002 | 90% |
Usage Examples
Simple Chat
Chat with System Prompt (Cached Automatically)
Multi-turn Conversation (Auto-cached after 4 messages)
Tool Calling with Auto-cached Tool Definitions
Custom Base URL (For Proxies)
Advanced Configuration
Connection Warmup
Reduce first-request latency:Custom Timeouts
Defaults from~/workspace/source/clients/agent-runtime/src/providers/anthropic.rs:164:
- Request timeout: 120 seconds
- Connect timeout: 10 seconds
With Resilient Provider Chain
Response Format
Anthropic’s response structure:ChatResponse:
Error Handling
Common errors and solutions:Missing Credentials
Rate Limiting
- Use prompt caching to reduce token usage
- Implement exponential backoff (automatic with
create_resilient_provider) - Upgrade to higher rate limits
Invalid Model
Token Limit Exceeded
- Reduce conversation history
- Use prompt caching to reduce effective token count
- Claude models have 200K context windows
Best Practices
- Use Claude 3.5 Sonnet for best overall performance
- Use Claude 3.5 Haiku for cost-effective, fast responses
- Let automatic caching work - structure prompts to exceed 3KB for system prompts
- Keep conversations long (>4 messages) to trigger message caching
- Use environment variables for credentials, never hardcode
- Call
warmup()during initialization - Set appropriate temperature:
0.0-0.3for factual/deterministic0.7(default) for balanced1.0+for creative
- Enable fallback providers for production
- Monitor cache hit rates in Anthropic console
- Use tool calling for structured workflows
Cost Optimization
Model Selection
| Model | Input (per 1M tokens) | Output (per 1M tokens) | Cached Input | Use Case |
|---|---|---|---|---|
| claude-3-5-sonnet | $3.00 | $15.00 | $0.30 (90% off) | General-purpose, best |
| claude-3-5-haiku | $0.80 | $4.00 | $0.08 (90% off) | High-volume, fast |
| claude-3-opus | $15.00 | $75.00 | $1.50 (90% off) | Complex reasoning |
| claude-3-sonnet | $3.00 | $15.00 | $0.30 (90% off) | Legacy balanced |
| claude-3-haiku | $0.25 | $1.25 | $0.03 (90% off) | Legacy fast |
Caching Tips
- Structure system prompts to exceed 3KB for automatic caching
- Place static content first in system prompts
- Define all tools upfront (last tool gets cached)
- Keep conversations going to leverage message caching
- Monitor cache metrics in Anthropic dashboard
Cost Comparison Example
Scenario: 100 requests with 10KB system prompt| Without Caching | With Caching | Savings |
|---|---|---|
| $30.00 | $3.03 | $26.97 (90%) |