Anthropic Provider
Handles the Anthropic Messages API with SSE streaming. Selected automatically when ModelConfig.api == ApiProtocol::AnthropicMessages.
Usage
#![allow(unused)] fn main() { use phi_core::BasicAgent; use phi_core::provider::ModelConfig; let api_key = std::env::var("ANTHROPIC_API_KEY").unwrap(); let agent = BasicAgent::new(ModelConfig::anthropic( "claude-sonnet-4-20250514", "Claude Sonnet 4", &api_key, )); }
Features
Streaming SSE
Uses reqwest-eventsource to parse Anthropic's SSE stream. Events handled:
message_start— Input token usage, cache statscontent_block_start— Text, thinking, or tool_use blockcontent_block_delta— Text, thinking, input JSON, or signature deltascontent_block_stop— Block completemessage_delta— Stop reason, output usagemessage_stop— Stream complete
Extended Thinking
Set thinking_level to enable thinking with a token budget:
| Level | Budget Tokens |
|---|---|
Minimal | 128 |
Low | 512 |
Medium | 2,048 |
High | 8,192 |
Thinking content is streamed as Content::Thinking with a cryptographic signature for verification.
Cache Control
Automatic prompt caching via cache_control markers:
- System prompt: Always cached with
{"type": "ephemeral"} - Second-to-last message: Gets
cache_controlon its last content block, creating a cache breakpoint
This means on repeated calls, only the latest message is processed at full price.
Configuration
| Setting | Value |
|---|---|
| API URL | https://api.anthropic.com/v1/messages |
| API Version | 2023-06-01 |
| Auth Header | x-api-key |
| Default Max Tokens | 8,192 |
Environment Variables
| Variable | Purpose |
|---|---|
ANTHROPIC_API_KEY | API key |