openrouter_client

OpenRouter client package re-exporting legacy monolithic definitions for backward compatibility.

class openrouter_client.OpenRouterClient(api_key, model='x-ai/grok-4.1-fast', temperature=1.0, max_tokens=60000, top_p=0.99, tool_registry=None, max_tool_rounds=10, base_url='http://localhost:3000/openai', gemini_api_key='', gemini_count_tokens_model='gemini-3.1-flash-lite', max_tool_output_chars=150000, http_connect_timeout=10.0, http_read_timeout=1200.0, http_write_timeout=120.0, http_pool_timeout=180.0)[source]

Bases: OpenRouterExecutor

Thin async wrapper around an OpenAI-compatible chat-completions endpoint.

Parameters:
  • api_key (str)

  • model (str)

  • temperature (float)

  • max_tokens (int)

  • top_p (float)

  • tool_registry (Any | None)

  • max_tool_rounds (int)

  • base_url (str)

  • gemini_api_key (str)

  • gemini_count_tokens_model (str)

  • max_tool_output_chars (int)

  • http_connect_timeout (float)

  • http_read_timeout (float)

  • http_write_timeout (float)

  • http_pool_timeout (float)

__init__(api_key, model='x-ai/grok-4.1-fast', temperature=1.0, max_tokens=60000, top_p=0.99, tool_registry=None, max_tool_rounds=10, base_url='http://localhost:3000/openai', gemini_api_key='', gemini_count_tokens_model='gemini-3.1-flash-lite', max_tool_output_chars=150000, http_connect_timeout=10.0, http_read_timeout=1200.0, http_write_timeout=120.0, http_pool_timeout=180.0)[source]

Initialize the instance.

Parameters:
  • api_key (str)

  • model (str)

  • temperature (float)

  • max_tokens (int)

  • top_p (float)

  • tool_registry (Any | None)

  • max_tool_rounds (int)

  • base_url (str)

  • gemini_api_key (str)

  • gemini_count_tokens_model (str)

  • max_tool_output_chars (int)

  • http_connect_timeout (float)

  • http_read_timeout (float)

  • http_write_timeout (float)

  • http_pool_timeout (float)

Return type:

None

async count_input_tokens(messages, *, gemini_model=None)[source]

Public wrapper for Gemini countTokens on OpenAI-shaped messages.

Return type:

int | None

Parameters:
async close()[source]

Close HTTP client.

Return type:

None

async embed(text, model)[source]

Generate an embedding vector for text via the Gemini API.

Return type:

list[float]

Parameters:
async embed_batch(texts, model)[source]

Generate embedding vectors for multiple texts via the Gemini API.

Return type:

list[list[float]]

Parameters:
openrouter_client.get_tool_round_limit_box()[source]

Return the mutable [max_rounds] for the current chat invocation.

Return type:

list[int] | None

async openrouter_client.media_to_content_parts(data, mimetype, filename, body_text=None)[source]

Build an OpenRouter multimodal content-parts list from raw media.

Office / ODF documents are transparently converted to PDF via LibreOffice so the LLM never sees an unsupported MIME type.

Parameters:
  • data (bytes) – The raw file bytes.

  • mimetype (str) – MIME type of the file (e.g. "image/png").

  • filename (str) – Human-readable filename.

  • body_text (str | None) – Optional caption / message body text to include alongside the media. When present it is prepended as a text content part.

Returns:

A list of content-part dicts suitable for the content field of an OpenRouter user message.

Return type:

list[dict[str, Any]]

openrouter_client.get_openrouter_api_key()[source]

Return the OpenRouter API key for the embed fallback, from env or the default.

Prefers OPENROUTER_API_KEY, then the legacy API_KEY env var, and finally _DEFAULT_OPENROUTER_KEY so the OpenRouter embed path works out-of-the-box. The result becomes the Bearer token for the embeddings endpoint.

Called within this module by openrouter_embed_batch() and openrouter_embed_batch_sync() when no explicit api_key is passed.

Returns:

The resolved OpenRouter key (effectively always non-None given the baked-in default).

Return type:

str | None

openrouter_client.resolve_model_alias(model)[source]

Expand a short model alias to its canonical provider model id.

The local proxy accepts friendly short names (e.g. gemini-3-pro-preview) but routes them to a canonical backend model. Resolving the alias here ensures all downstream logic (token caps, Gemini detection, response header) sees the true model name.

Return type:

str

Parameters:

model (str)

openrouter_client.build_trailing_non_user_filler()[source]

Random SHA-256–tagged user-role filler for requests ending in non-user turns.

Return type:

str