Module anthropic

Module anthropic 

Source
Expand description

The Anthropic messages adapter (DW-075).

Speaks the Anthropic messages API (POST /v1/messages). The translation is the deepest of the three dialects:

  • System messages lift to the top-level system field (joined with a blank line when several appear).
  • Tool RESULTS (OpenAI role: tool messages) become USER turns carrying a tool_result content block; assistant tool calls become tool_use blocks whose input is the parsed arguments object.
  • max_tokens is REQUIRED by Anthropic and absent from many OpenAI client calls: the adapter substitutes DEFAULT_MAX_TOKENS and says so in the module docs — the provider refuses an unbounded generation, so a floor is the only way to honor the request at all.
  • Finish reasons map end_turn/stop_sequence -> stop, max_tokens -> length, tool_use -> tool calls, refusal -> content filter.
  • Usage maps input_tokens/output_tokens to prompt/completion; total is derived (sum) because Anthropic does not send one.

Streaming (verified in tests, wired at the gateway in DW-077): the messages-API SSE stream (message_start -> content_block_* -> message_delta -> message_stop) translates event-by-event; tool argument fragments arrive as input_json_delta.partial_json and are re-expressed as OpenAI-style argument-fragment deltas.

Structs§

AnthropicAdapter
The Anthropic messages adapter. Stateless singleton.

Constants§

DEFAULT_MAX_TOKENS
The max_tokens the adapter substitutes when the client did not send one. Anthropic requires the field; OpenAI clients usually omit it. 4096 is a conservative default that no provider rejects.