llm#

A small client for any OpenAI-compatible chat completions API with tool calling.

OpenRouter, OpenAI, Google’s OpenAI-compatible Gemini endpoint, Groq and a local Ollama all speak this shape, so the provider and model are just settings.

Reasoning models (Claude Haiku 5.5, GPT-6 Luna) think by default and bill thinking as output. reasoning sets OpenRouter’s reasoning.effort for every request. When a model does think, its reasoning_details are kept on the assistant message so they go back with the tool results, which Claude needs to carry on after a tool call.

sidequest_bot.lib.llm.RATE_LIMIT_RETRIES: Final = 3#
sidequest_bot.lib.llm.MAX_RETRY_WAIT_SECONDS: Final = 20.0#
exception sidequest_bot.lib.llm.LLMError#

Bases: Exception

The model API failed or answered with something that isn’t a chat completion.

class sidequest_bot.lib.llm.ToolCall[source]#

One tool call the model asked for. arguments is None when the model sent JSON that doesn’t parse.

id: str#
name: str#
arguments: dict[str, Any] | None#
class sidequest_bot.lib.llm.Usage[source]#

Tokens a request used, and what it cost in USD when the API says (OpenRouter does).

input_tokens: int = 0#
output_tokens: int = 0#
cost: float | None = None#
class sidequest_bot.lib.llm.Reply[source]#

One model turn: message goes back into the history verbatim; text and tool_calls are parsed.

message: dict[str, Any]#
text: str#
tool_calls: list[ToolCall]#
usage: Usage#
class sidequest_bot.lib.llm.ChatClient(base_url: str, api_key: str, model: str, *, reasoning: str | None = None, max_tokens: int = 4000, http: httpx.AsyncClient | None = None)[source]#

POST {base_url}/chat/completions with tools. Pass http to reuse a client (tests pass a mocked one).

model#
async aclose() → None[source]#

Close the HTTP client if this instance created it.

async complete(messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None = None) → Reply[source]#

One model turn over messages, offering tools if any.