Anthropic (adk-anthropic)

The adk-anthropic crate is a dedicated Anthropic API client for ADK-Rust. It provides direct access to the full Anthropic Messages API surface, including streaming, extended thinking, prompt caching, citations, vision, PDF processing, and token pricing.

Architecture

adk-anthropic is a standalone client crate that adk-model wraps via its Anthropic adapter. You can use it directly for low-level API access, or through adk-model for the unified Llm trait.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Your Code  │────▢│   adk-model   │────▢│adk-anthropic │────▢ Anthropic API
β”‚             β”‚     β”‚ (Llm trait)   β”‚     β”‚ (HTTP client)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Supported Models

ModelAPI IDNotes
Claude Opus 4.7claude-opus-4-7Most capable GA model, 1M context, 128K output, adaptive thinking only
Claude Opus 4.6claude-opus-4-6Previous flagship, 1M context, 128K output
Claude Sonnet 4.6claude-sonnet-4-6Best speed/intelligence balance, 1M context
Claude Haiku 4.5claude-haiku-4-5Fastest, 200K context
Claude Opus 4.5claude-opus-4-5Previous generation
Claude Sonnet 4.5claude-sonnet-4-5Previous generation
Claude Sonnet 4claude-sonnet-4-0Legacy (retiring June 2026)
Claude Opus 4claude-opus-4-0Legacy (retiring June 2026)

Setup

Set your API key:

export ANTHROPIC_API_KEY=sk-ant-...

Direct Client Usage

use adk_anthropic::{Anthropic, KnownModel, MessageCreateParams};

let client = Anthropic::new(None)?; // reads ANTHROPIC_API_KEY
let params = MessageCreateParams::simple("Hello!", KnownModel::ClaudeSonnet46);
let response = client.send(params).await?;

Through adk-model

use adk_model::anthropic::{AnthropicClient, AnthropicConfig};

let api_key = std::env::var("ANTHROPIC_API_KEY")?;
let model = AnthropicClient::new(AnthropicConfig::new(api_key, "claude-sonnet-4-6"))?;

Custom base URL (gateways, proxies, compatible endpoints)

Point the client at a different endpoint to route through a corporate proxy or a Messages-API-compatible gateway. Provide the root URL without the /v1/ suffix β€” it is appended automatically. AnthropicConfig::with_base_url flows through adk-model to the underlying client:

use adk_model::anthropic::{AnthropicClient, AnthropicConfig};

let model = AnthropicClient::new(
    AnthropicConfig::new(api_key, "claude-sonnet-4-6")
        .with_base_url("https://gateway.internal/anthropic"),
)?;

Or set it directly on the low-level client, and read back the effective endpoint with base_url():

use adk_anthropic::Anthropic;

let client = Anthropic::new(Some(api_key))?
    .with_base_url("https://api.minimax.io/anthropic".to_string())?;
assert_eq!(client.base_url(), "https://api.minimax.io/anthropic");

When unset, the client uses Anthropic's public API (https://api.anthropic.com).

with_base_url returns Result because the client attaches the Anthropic API key to every request. Only https://, or http:// with a loopback host (localhost, 127.0.0.1, [::1]) for local development, is accepted β€” anything else is rejected as a validation error rather than silently sending the key in cleartext. The same rule applies to AnthropicConfig::with_base_url, which is validated when AnthropicClient::new builds the underlying client.

Key Features

Adaptive Thinking (4.6+ models)

Opus 4.7 only supports adaptive thinking β€” budget_tokens is rejected.

use adk_anthropic::{ThinkingConfig, OutputConfig, EffortLevel};

// Opus 4.7: use xhigh effort (recommended for coding/agentic)
let mut params = MessageCreateParams::simple("Solve this...", KnownModel::ClaudeOpus47)
    .with_thinking(ThinkingConfig::adaptive());
params.output_config = Some(OutputConfig::with_effort(EffortLevel::XHigh));

// Sonnet 4.6: any effort level works
let mut params = MessageCreateParams::simple("Solve this...", KnownModel::ClaudeSonnet46)
    .with_thinking(ThinkingConfig::adaptive());
params.output_config = Some(OutputConfig::with_effort(EffortLevel::High));

Prompt Caching

use adk_anthropic::CacheControlEphemeral;

let mut params = MessageCreateParams::simple("Question", KnownModel::ClaudeSonnet46)
    .with_system("Large system prompt...");
params.cache_control = Some(CacheControlEphemeral::new());

Structured Output

use adk_anthropic::{OutputConfig, OutputFormat};

let mut params = MessageCreateParams::simple("Extract data", KnownModel::ClaudeSonnet46);
params.output_config = Some(OutputConfig::new(OutputFormat::json_schema(schema)));

Token Pricing

use adk_anthropic::pricing::{ModelPricing, estimate_cost};

let cost = estimate_cost(ModelPricing::SONNET_46, &response.usage);
println!("${:.6}", cost.total());

Examples

Run with cargo run -p adk-anthropic --example <name>:

  • basic β€” non-streaming chat
  • streaming β€” SSE streaming
  • thinking β€” adaptive + budget thinking
  • tools β€” tool calling
  • structured_output β€” JSON schema
  • caching β€” multi-turn caching with costs
  • context_editing β€” tool/thinking clearing (beta)
  • compaction β€” server-side compaction
  • token_counting β€” pre-send token estimation
  • stop_reasons β€” handling all stop reasons
  • fast_mode β€” fast inference (beta)
  • citations β€” document citations
  • pdf_processing β€” PDF analysis
  • vision β€” image understanding