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, mandatory_tool_round_max_tokens=8192, max_transport_retries=1, codex_quota_client=None, codex_quota_owner=False)

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)

  • mandatory_tool_round_max_tokens (int)

  • max_transport_retries (int)

  • codex_quota_client (CodexQuotaClient | None)

  • codex_quota_owner (bool)

__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, mandatory_tool_round_max_tokens=8192, max_transport_retries=1, codex_quota_client=None, codex_quota_owner=False)

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)

  • mandatory_tool_round_max_tokens (int)

  • max_transport_retries (int)

  • codex_quota_client (CodexQuotaClient | None)

  • codex_quota_owner (bool)

Return type:

None

async count_input_tokens(messages, *, gemini_model=None)

Public wrapper for Gemini countTokens on OpenAI-shaped messages.

Return type:

int | None

Parameters:
async close()

Close HTTP and optional Codex app-server clients.

Return type:

None

async begin_codex_device_login(subject)
Return type:

Any

Parameters:

subject (str)

async finish_codex_device_login(subject, login_id)
Return type:

Any

Parameters:
  • subject (str)

  • login_id (str)

async codex_login_pending(subject)
Return type:

bool

Parameters:

subject (str)

async cancel_codex_device_login(subject)
Return type:

bool

Parameters:

subject (str)

async codex_account_status(account_id)
Return type:

Any

Parameters:

account_id (str)

async logout_codex_account(account_id)
Return type:

None

Parameters:

account_id (str)

async validate_codex_quota_route(model, effort, account_id, *, service_tier=None)

Validate one linked account and its current model capabilities.

Return type:

None

Parameters:
  • model (str)

  • effort (str)

  • account_id (str)

  • service_tier (str | None)

async codex_model_catalog(account_id, *, refresh=False)

Return non-secret live capabilities for one linked account.

Return type:

Any

Parameters:
async embed(text, model)

Generate an embedding vector for text via the Gemini API.

Return type:

list[float]

Parameters:
async embed_batch(texts, model)

Generate embedding vectors for multiple texts via the Gemini API.

Return type:

list[list[float]]

Parameters:
openrouter_client.get_tool_round_limit_box()

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)

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()

Return the OpenRouter API key for the embed fallback tier.

Prefers the legacy API_KEY env var when it is the only thing set, then resolves openrouter.embed through system_secret_store.get_secret_sync(), which itself checks OPENROUTER_API_KEY, the TTL cache warmed by reload_pool(), Redis, and finally the api_key entry in config.yaml. The result becomes the Bearer token for the embeddings endpoint. No key is baked into source, so an unprovisioned store yields None and the caller raises rather than silently authenticating with a leaked credential.

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, or None when unprovisioned.

Return type:

str | None

openrouter_client.resolve_model_alias(model)

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()

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

Return type:

str