Summary
Functions
build(client, request)server
Builds a request to the Anthropic Messages API.
build(client, request)server
Builds a request to an OpenAI-compatible chat completions API.
from_json(str)server
Decodes JSON text with
util.JSONToTable, without depth limits and without turning keys that look like numbers into numbers.new(options)server
Creates a client.
parse(data)server
Turns a reply of the Anthropic Messages API into a response.
parse(data)server
Turns a reply of an OpenAI-compatible chat completions API into a response.
to_json(tab)server
Encodes a table as JSON with
util.TableToJSON, writing LLM.empty_object as {}.Creates a message with the result of a tool call.
user(content)server
Creates a user message.
Functions
Builds a request to the Anthropic Messages API.
Parameters
- client Client
- request Map the request given to Client:chat
Returns
- String endpoint path
- Map headers
- Map body
Builds a request to an OpenAI-compatible chat completions API.
Parameters
- client Client
- request Map the request given to Client:chat
Returns
- String endpoint path
- Map headers
- Map body
Decodes JSON text with util.JSONToTable, without depth limits and without turning keys that
look like numbers into numbers.
Parameters
- str String JSON text
Returns
- Table the decoded table, or nil if the text is not valid JSON
Creates a client.
LLM.new { provider = 'anthropic', api_key = key }
LLM.new { provider = 'openai', base_url = 'http://localhost:11434/v1', model = 'qwen3:8b' }
Parameters
- options Map with the fields: provider='anthropic' [String 'anthropic', 'openai' or another key of LLM.providers] api_key=nil [String API key, may be omitted for local servers] model=nil [String model name; 'claude-opus-5-5' for Anthropic, required for other providers] base_url=nil [String API root that the endpoint path is appended to] max_tokens=16000 [Number maximum number of tokens in a reply, thinking included] max_tokens_key='max_tokens' [String body field of max_tokens for OpenAI-compatible APIs, OpenAI's reasoning models want 'max_completion_tokens'] effort=nil [String 'low', 'medium', 'high', ... sent as output_config.effort to Anthropic and as reasoning_effort to OpenAI-compatible APIs] fallbacks='default' [String/List/Boolean Anthropic refusal fallbacks, false to turn them off] betas=nil [List<String> extra Anthropic beta headers] headers=nil [Map extra HTTP headers] extra=nil [Map extra fields merged into every request body] timeout=120 [Number seconds to wait for a reply] max_retries=2 [Number retries of rate-limited, failed and timed out requests]
Returns
- Client
Turns a reply of the Anthropic Messages API into a response.
Parameters
- data Map the decoded reply
Returns
- Map the response
Turns a reply of an OpenAI-compatible chat completions API into a response.
Parameters
- data Map the decoded reply
Returns
- Map the response, or nil if the reply has no choices
- String the reason the reply could not be read
Encodes a table as JSON with util.TableToJSON, writing LLM.empty_object as {}.
Parameters
- tab Table
Returns
- String JSON text
Creates a message with the result of a tool call.
Parameters
- call Map/String tool call from response.tool_calls, or its ID
- content String/Table optional, defaults to
''result of the call, tables are sent as JSON - is_error Boolean optional, defaults to
falsewhether the call has failed
Returns
- Map the message
Creates a user message.
Parameters
- content String text of the message, or a List of provider-specific content blocks
Returns
- Map the message