Skip to content

openrouter: read supported params from OpenRouter instead of the bundled model-cost map - #8

Merged
jwbron merged 2 commits into
mainfrom
openrouter-live-capabilities
Jul 25, 2026
Merged

jwbron merged 2 commits into
mainfrom
openrouter-live-capabilities

Conversation

@jwbron

@jwbron jwbron commented Jul 25, 2026

Copy link
Copy Markdown
Owner

Companion to #7. That PR makes a silent drop_params drop visible; this one stops the most common cause of it. They are independent and neither needs the other.

The problem

OpenrouterConfig.get_supported_openai_params gates reasoning_effort and thinking on litellm.supports_reasoning, which reads the bundled model-cost map. For OpenRouter that map is wrong by construction: OpenRouter publishes new slugs continuously, the bundled map lags, and an unlisted model answers False. The gate is a bare if, so it fails closed, and drop_params then discards the parameter without an exception or a log line.

Absent is the normal state, not an exotic one. Every OpenRouter slug in the deployment where this was found is missing from the map at the pinned release:

Slug In model_cost supports_reasoning
openrouter/moonshotai/kimi-k3 absent False
openrouter/z-ai/glm-5.2 absent False
openrouter/poolside/laguna-s-2.1 absent False
openrouter/deepseek/deepseek-v4-pro absent False
openrouter/deepseek/deepseek-r1 (control) present True

The fix

OpenRouter publishes the answer itself. GET https://openrouter.ai/api/v1/models returns every model with a supported_parameters list and requires no API key (verified: HTTP 200 unauthenticated, 345 models). This reads that, caches it for the process, and unions it with the existing map-based answer.

moonshotai/kimi-k3      -> reasoning_effort, reasoning, include_reasoning, ...
z-ai/glm-5.2            -> reasoning_effort, reasoning, include_reasoning, ...
poolside/laguna-s-2.1   -> reasoning, include_reasoning, ...   (no reasoning_effort)

Why union, and not "live data wins"

I built it the other way first and the existing test suite caught it, correctly.

supported_parameters under-reports reasoning_effort. deepseek/deepseek-r1 is flagged supports_reasoning: true in the bundled map and is unambiguously a reasoning model, yet OpenRouter advertises only reasoning and include_reasoning for it. The OpenAI-compatible reasoning_effort spelling is treated as an alias of the native reasoning object rather than listed in its own right. Treating that absence as a denial drops a working parameter, which is the exact failure this PR exists to remove, so it would have traded one silent drop for another.

The lookup therefore admits knobs the map does not know about and never withholds one the map allows. The worst case is precisely what shipped before, which also means this cannot regress an existing deployment.

Fails soft, by construction

This sits behind a hot synchronous path, so:

  • Any error, timeout, non-200, or malformed payload returns "no opinion" and the caller falls back to supports_reasoning. A capability lookup must never be able to break a request.
  • A failed fetch is cached for the TTL too. An offline or firewalled deployment costs one attempt per hour, not one per request.
  • A lock serialises refreshes so concurrent first requests do not stampede the endpoint.
  • LITELLM_OPENROUTER_CAPABILITY_FETCH=0 disables it entirely. LITELLM_OPENROUTER_CAPABILITY_TTL and LITELLM_OPENROUTER_CAPABILITY_TIMEOUT tune it.
  • No API key is read on purpose: capability data must be available to a proxy that has not been handed credentials yet.

Verification

Driven through a Router model_list entry with the transport mocked and the capability fetch hitting the real endpoint:

Model reasoning_effort on the wire, before after
openrouter/moonshotai/kimi-k3 absent high
openrouter/z-ai/glm-5.2 absent high
openrouter/poolside/laguna-s-2.1 absent absent (advertises reasoning, not reasoning_effort; in neither source)
openrouter/moonshotai/kimi-k3, fetch disabled absent absent

Testing

  • 17 new tests: lookup and slug normalisation (provider prefix, :variant suffixes), fail-soft across non-200 / transport error / malformed payload, the negative cache, TTL expiry, the opt-out, and the gate's behaviour in both directions including the union case above.
  • 127 passed in tests/test_litellm/llms/openrouter/, including the 4 pre-existing reasoning tests that the first design broke.
  • 250 passed in tests/test_litellm/test_utils.py.
  • A conftest.py disables the lookup for the rest of the OpenRouter unit tests so they stay deterministic and offline. The directory runs in 1.8s with the network blocked; without the conftest those tests were doing real HTTP.
  • ruff check and ruff format --check clean.

Scope

Chat completions only, and only the reasoning knobs, which is where the failure was observed. The same data could inform other params (several models do not advertise seed, top_p, or temperature), but that would mean withholding params that pass today, so it wants its own change and its own argument.

OpenrouterConfig gated reasoning_effort/thinking on litellm.supports_reasoning,
which reads the bundled model-cost map. For OpenRouter that map is wrong by
construction: OpenRouter publishes new slugs continuously and the map lags, so
a current model answers False and the knob is dropped before the request body
is built, silently.

OpenRouter publishes the answer itself. GET /api/v1/models returns every model
with a supported_parameters list and needs no API key. Read that, cache it for
the process, and union it with the existing map-based answer.

The union is deliberate rather than letting live data win outright.
supported_parameters under-reports reasoning_effort: deepseek/deepseek-r1 is
flagged supports_reasoning in the bundled map and is unambiguously a reasoning
model, yet OpenRouter advertises only reasoning and include_reasoning for it,
treating the OpenAI-compatible spelling as an alias rather than listing it.
Reading that absence as a denial would drop a working parameter, which is the
failure this change exists to remove. So the lookup can admit a knob the map
does not know about but never withholds one the map allows, and the worst case
is exactly what shipped before.

Fails soft throughout: any error, timeout, non-200 or malformed payload yields
no opinion and the caller falls back. A failed fetch is cached for the TTL, so
an offline deployment costs one attempt per hour rather than one per request,
and a lock keeps concurrent first requests from stampeding the endpoint.
LITELLM_OPENROUTER_CAPABILITY_FETCH=0 restores the previous behaviour.

Verified against the live endpoint through a Router with the transport mocked:
reasoning_effort now reaches the wire for moonshotai/kimi-k3 and z-ai/glm-5.2
(absent from the map, previously dropped), stays absent for
poolside/laguna-s-2.1 (which advertises reasoning but not reasoning_effort, and
is in neither source), and reverts to absent for all three with the fetch
disabled.

A conftest disables the lookup for the rest of the OpenRouter unit tests so
they stay deterministic and offline; the directory runs in 1.8s with the
network blocked.
@jwbron

jwbron commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

Deployment note for the fleet this was built against, measured after the PR was opened.

Sending reasoning_effort: high reduces reasoning depth on both models where the knob now lands. Measured directly against OpenRouter with the flat key, max_tokens: 16000, n=4, mean reasoning tokens:

Model no parameter reasoning_effort: high reasoning_effort: low
moonshotai/kimi-k3 3130 340 49
z-ai/glm-5.2 1689 1090 788

On kimi-k3 the distributions do not overlap (lowest baseline sample 2240, highest high sample 1000). high is a cap well below the model's default, not a ceiling.

Nothing here changes the correctness argument: config and wire disagreeing with no signal is a defect whichever way the knob moves generation. But anyone adopting this should audit their config before deploying, because a reasoning_effort that was previously inert becomes live, and on these models that is a ~9x cut. See jwbron/egg#3624 for the sequencing.

# Stamped even on failure, so an unreachable endpoint costs one attempt
# per TTL rather than one per request.
_CACHE = _fetch()
_CACHE_STAMP = time.monotonic()
litellm requires >=3.10, so dict[...]/set[...]/X | None are native. Keeps
this module byte-identical to the copy egg's build-time patcher installs
(config/litellm/openrouter_capabilities.py), whose linter requires the
modern spelling; divergence between the two would be silent drift.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants