class

LLM::Anthropic

Inherits LLM::General < Reference < Object

Anthropic's native Messages API (POST /v1/messages).

Anthropic's OpenAI-compatible endpoint ignores response_format, so the endpoint schema the analyzer asks for never reached Claude. This client sends it as output_config.format instead, and reads Claude's own reply shape (content blocks, stop_reason). Retries, the sampling and structured-output fallbacks and the fail-fast streak are General's.

Constants

API_VERSION = "2023-06-01"
MAX_OUTPUT_TOKENS = 16000

max_tokens is required here. Adaptive thinking spends from the same budget, and a reply cut at it is reported like any other truncation. ponytail: fixed; make it a flag if 16k proves short for big bundles.

Constructors

new(url : String, model : String, api_key : String | Nil)
Source

Class methods

api_url(url : String) : String

https://api.anthropic.com and .../v1 both mean the Messages endpoint under them.

Source
extract_agent_action(response : JSON::Any) : String

The first tool_use block as the agent's action payload, else the reply's text.

Source
max_output_tokens(model : String) : Int32

Claude 3 and 3.5 reject a max_tokens over their smaller output cap with a 400 nothing recovers from, so every request would fail.

Source
reply_text(response : JSON::Any) : String

The text of a reply, or "" for a refusal. Thinking and other non-text blocks are skipped.

Source
tools(openai_tools : String) : JSON::Any

OpenAI {"type":"function","function":{...}} tool definitions as Anthropic's {name, description, input_schema}.

Source

Instance methods

request_messages(messages : Array(Hash(String, String)), format : String = "json", raise_overflow : Bool = false)

Make a request with chat-style messages raise_overflow turns a context-window rejection into ContextOverflow instead of "" (see post).

Source
request_messages_with_tools(messages : Array(Hash(String, String)), tools : String)

Request next action with provider-native tool-calling. tools must be a JSON array string compatible with OpenAI-style chat completions API.

Source

Nested types