Provider for Anthropic's Claude Messages API.
Uses Req for HTTP calls. Since Anthropic's wire format uses content blocks (the most expressive format), this provider has the simplest normalization.
Config
Required:
:api_key- Anthropic API key:model- Model name (e.g., "claude-opus-4-6", "claude-sonnet-4-6", "claude-haiku-4-5")
Optional:
:max_tokens- Max output tokens (default: 4096):system_prompt- System prompt string:api_url- Base URL (default: "https://api.anthropic.com"):api_version- API version header (default: "2023-06-01"):extra_headers- Additional headers as[{name, value}]:req_options- Additional options passed to Req (useful for testing):extended_thinking- Enable extended thinking. Pass a keyword list with:budget_tokens(e.g.,[budget_tokens: 5000]). Thinking blocks are returned in the message content and must be round-tripped verbatim in subsequent turns (Anthropic requires thesignaturefield).:on_event- Streaming event callback(event -> :ok). Called for each streaming delta. When used viaServer.stream_chat/4,eventis a normalized envelope map:%{v: 1, seq:, correlation_id:, turn:, ts_ms:, event: :text_delta, payload: text}%{v: 1, seq:, correlation_id:, turn:, ts_ms:, event: :thinking_delta, payload: text}Pass viaServer.stream_chat/4opts:on_event: fn event -> ... end. Note: direct callers ofAlloy.Provider.Anthropic.stream/4(without Turn) receive provider-native tuples (for example{:thinking_delta, text}).
Example
Alloy.run("What is Elixir?",
provider: {Alloy.Provider.Anthropic,
api_key: System.get_env("ANTHROPIC_API_KEY"),
model: "claude-sonnet-4-6"
}
)
Summary
Functions
Stream a completion using Anthropic's SSE streaming API.