Implementation for providers that speak the Anthropic Messages API:
POST {base_url}/v1/messages.
The Anthropic wire format differs from OpenAI Chat Completions in several load-bearing ways, all handled here:
systemis top-level —SystemPromptparts are lifted out of the message stream and sent as thesystemparameter, not as a message.- Content is an array of blocks — every message body is a list of
{"type": "text" | "tool_use" | "tool_result", ...}blocks. - Tool calls — the model emits
tool_useblocks (with aninputobject, not a JSON string); the agent replies withtool_resultblocks in a user message keyed bytool_use_id. - Auth —
x-api-key(real Anthropic) orAuthorization: Bearer(e.g. Z.AI's/api/anthropic), plus theanthropic-versionheader.
Pointing this at Z.AI's Anthropic-compatible endpoint gives access to GLM
models (glm-4.5-air, glm-4.7, glm-5.2, …) using the native format.
Summary
Functions
Split a message history into a top-level system block list and the
conversation messages (with System parts removed and consecutive
same-role messages merged — Anthropic requires strict alternation).
Encode Tool list into the Anthropic tools payload (or nil).
Perform a (non-streaming) Messages API request.
Perform a streaming Messages API request. Returns a lazy stream of
Functions
@spec encode_messages([ExAgent.Message.t()]) :: {[map()], [map()]}
Split a message history into a top-level system block list and the
conversation messages (with System parts removed and consecutive
same-role messages merged — Anthropic requires strict alternation).
@spec encode_tools(ExAgent.ModelRequestParameters.t()) :: [map()] | nil
Encode Tool list into the Anthropic tools payload (or nil).
@spec parse_response( map(), struct() ) :: ExAgent.Message.Response.t()
@spec request( struct(), [ExAgent.Message.t()], ExAgent.ModelSettings.t() | nil, ExAgent.ModelRequestParameters.t() ) :: {:ok, ExAgent.Message.Response.t(), struct()} | {:error, term()}
Perform a (non-streaming) Messages API request.
@spec request_stream( struct(), [ExAgent.Message.t()], ExAgent.ModelSettings.t() | nil, ExAgent.ModelRequestParameters.t() ) :: Enumerable.t() | {:error, term()}
Perform a streaming Messages API request. Returns a lazy stream of:
{:text_delta, binary},{:response, Response.t()}(final assembled),{:error, reason}on failure.