Alloy.Provider.Anthropic (alloy v0.10.1)

Copy Markdown View Source

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 the signature field).
  • :on_event - Streaming event callback (event -> :ok). Called for each streaming delta. When used via Server.stream_chat/4, event is 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 via Server.stream_chat/4 opts: on_event: fn event -> ... end. Note: direct callers of Alloy.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.

Functions

stream(messages, tool_defs, config, on_chunk)

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.