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}]:extra_body- Additional request body fields, merged last: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.
Types
@type config() :: %{ :api_key => String.t(), :model => String.t(), optional(:max_tokens) => pos_integer(), optional(:system_prompt) => String.t(), optional(:api_url) => String.t(), optional(:api_version) => String.t(), optional(:extra_headers) => [{String.t(), String.t()}], optional(:extra_body) => map(), optional(:req_options) => keyword(), optional(:extended_thinking) => keyword(), optional(:on_event) => (term() -> :ok), optional(:cache) => boolean(), optional(:memory) => {module(), term()} }
Configuration for the Anthropic provider. See the module doc for field semantics.
Functions
@spec stream( [Alloy.Message.t()], [Alloy.Provider.tool_def()], config(), (String.t() -> :ok) ) :: {:ok, Alloy.Provider.completion_response()} | {:error, term()}
Stream a completion using Anthropic's SSE streaming API.
Calls on_chunk for each text delta as it arrives. Accumulates all
content blocks and returns the same {:ok, completion_response()} shape
as complete/3 once the stream finishes.