robotsix_llmio openrouter
OpenRouter transport layer — base model-family-agnostic provider, plus a derived DeepSeek layer that pins to DeepSeek models and configures per-tier reasoning policy.
DeepSeek (derived layer)
The module also exports DeepSeek-specific classes that pin the generic OpenRouter transport to DeepSeek models on OpenRouter:
OpenRouterDeepseekProvider— provider that mapslevel=2to"deepseek/deepseek-v4-pro"(reasoning at"xhigh") andlevel=1to"deepseek/deepseek-v4-flash-latest"(reasoning disabled). Routing is configurable viaprovider_kwargs:max_price_prompt,max_price_completion, andignore_providers(a list of upstream providers to exclude from fallback routing — seeOpenRouterDeepseekModelbelow).OpenRouterDeepseekModel— model that injectsprovider: {order: ["DeepSeek"], allow_fallbacks: true, max_price: …, ignore: …}and per-levelreasoningsettings into every request.orderkeeps DeepSeek first (prompt-cache warmth) whileallow_fallbackslets OpenRouter route past it when it fails;max_pricecaps the fallback's sticker price per 1M tokens — each tier's ceiling is set from DeepSeek's own sticker price plus headroom soorderandmax_pricenever contradict.ignorebars providers whose cache-read rate is a large multiple of DeepSeek's — a costmax_pricecannot see (OpenRouter'smax_priceaccepts onlyprompt,completion,requestandimage).
The DeepSeek classes are imported from robotsix_llmio.openrouter:
from robotsix_llmio.openrouter import (
OpenRouterDeepseekModel,
OpenRouterDeepseekProvider,
)
Exports
Provider & model
OpenRouterProvider— base provider that builds cost-instrumented OpenRouter models; tier→model mapping is supplied byTierConfig— subclasses optionally inject provider-family quirks such as upstream pinning and per-tier reasoning policyOpenRouterModel—OpenAIChatModelsubclass that opts into OpenRouter usage accounting (usage.include) and stampsusage.costonto the active OTel span. It also annotates the stable request prefix (last system message and last tool definition) withcache_control: {"type": "ephemeral"}markers so OpenRouter-compatible upstream providers (DeepSeek, Anthropic, …) apply prompt caching to the repeated system prompt + tool schemas. Subclasses or callers can opt out per-model by setting_prompt_caching_enabled = False. Cache semantics are documented in theopenrouter/model.pymodule docstring.
Transient error detection
is_openrouter_transient— returnsTruefor core transient errors or any of the OpenRouter upstream-error signatures below, walking the cause/context chainis_openrouter_upstream_error— detects OpenRouter'sfinish_reason='error'upstream-failure pattern (pydanticValidationErrormentioningfinish_reasonand'error')is_openrouter_upstream_payment_error— detects a 402 raised by the upstream provider (OpenRouter routed to a provider with no balance of its own, carryingmetadata.provider_nameandmetadata.is_byok: false), as distinct from our own account running out of credits — only the former is transientis_deepseek_reasoning_400— detects DeepSeek's reasoning-content 400 (missingreasoning_contentin thinking mode), treating it as an infrastructure hiccup to retry
Cost sources & recording
OpenRouterKeyCostSource— reads per-key cumulative lifetime usage fromGET /api/v1/auth/key; snapshot-and-diff to get a window's spend per keyOpenRouterProviderCostSource— reads OpenRouter's billed spend for a time window viaGET /api/v1/activity, summing across each UTC day the window coversAsyncOpenRouterClient— async REST client for per-key usage (fetch_key_usage) and account credits (fetch_credits); async counterpart ofOpenRouterKeyCostSourcerecord_openrouter_cost— stamps per-call cost, token counts, cache details, and gen_ai attributes onto the active OTel span (no-op without OTel or a recording span). When the response carries cached-token details it also emits an INFO log line summarising the cached-vs-uncached input-token split (total / cached /%hit / cache-creation tokens) so the prompt-caching win is measurable in logs without a Langfuse UI.
Usage data types
KeyUsage— frozen dataclass: cumulative lifetime usage and credit limit for an OpenRouter API key, as returned byOpenRouterKeyCostSource