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:
OpenRouterExecutorThin 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
countTokenson OpenAI-shaped messages.
- async finish_codex_device_login(subject, login_id)
- async validate_codex_quota_route(model, effort, account_id, *, service_tier=None)
Validate one linked account and its current model capabilities.
- async codex_model_catalog(account_id, *, refresh=False)
Return non-secret live capabilities for one linked account.
- async embed(text, model)
Generate an embedding vector for text via the Gemini API.
- openrouter_client.get_tool_round_limit_box()
Return the mutable
[max_rounds]for the current chat invocation.
- 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:
- Returns:
A list of content-part dicts suitable for the
contentfield of an OpenRouter user message.- Return type:
- openrouter_client.get_openrouter_api_key()
Return the OpenRouter API key for the embed fallback tier.
Prefers the legacy
API_KEYenv var when it is the only thing set, then resolvesopenrouter.embedthroughsystem_secret_store.get_secret_sync(), which itself checksOPENROUTER_API_KEY, the TTL cache warmed byreload_pool(), Redis, and finally theapi_keyentry inconfig.yaml. The result becomes theBearertoken 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()andopenrouter_embed_batch_sync()when no explicitapi_keyis passed.
- 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.