litellm/tests/unit/router_strategy
quietjules 8e8cdc6ece
fix(router): count pre-call context-window tokens with each deployment's tokenizer (#49764)
* fix(router): count pre-call context-window tokens with each deployment's tokenizer

With pre-call checks on, every prompt was counted with gpt-3.5-turbo's tokenizer, so a prompt the provider accepts could get a local context-window 400. Each deployment now counts with its own model's tokenizer when litellm has one, and otherwise with an o200k_base stand-in that refuses locally only past 2x the window. A group with a deployment that fits still routes to it.

* test(router): assert the context-window error reports the deployment's own count

* fix(router): keep context window fallbacks, tag and region routing, and token counts right in pre-call checks

Exclude a stand-in overflow when a context window fallback resolves or a fitting deployment survives
every other filter, count with an own tokenizer only for OpenAI models, share one stand-in count,
restore the base exclusion for a non-string allowed_model_region, and keep an OpenAI model's own
encoding when a caller passes select_tokenizer's default

* fix(router): count each distinct tokenizer once and steer only to an untried context window fallback

Count a prompt once per distinct encoding and message framing instead of once per OpenAI model name, so an
OpenAI-only group with several models runs one count. A context window fallback now keeps an uncertain overflow
off a deployment only when its chain still holds an untried target other than the failing group. Complexity router
admission counts through the router's bounded async counter.

* fix(router): enforce embedding windows and operator caps exactly, and memoize stand-in counts

* fix(router): enforce every operator-set max_input_tokens exactly

An operator's model_info.max_input_tokens is documented as a hard limit, so the
pre-call check now treats it as exact for every model. Only a window read from
the cost map keeps the 2x stand-in margin, including one a saved /model/info
response copied into model_info.

* fix(router): keep the 2x band for operator windows the cost map does not exceed, and stop retrying a provider-refused estimate

An operator max_input_tokens is exact again only when it is below the cost
map's window, so a local model whose operator limit is the only window known
keeps the margin. After a provider answers a context window 400, the retry
loop no longer resends the prompt to a deployment admitted on an estimate and
returns the provider's error instead.

* fix(router): send held-back estimates when the context window fallback cannot take them

A deployment held back for a context window fallback now gets the request when
that fallback is too small or skipped by the access, budget or rate-limit checks.

A provider's context window 400 now stops every later estimate in the request,
through retries, generic fallbacks and order tiers, so no fallback hop resends it.

Ollama windows are exact like embeddings, since Ollama drops older messages past
its window and still answers 200

* test(router-core): cover a stream retry skipping a stand-in overflow when a context window fallback applies

* fix(router-core): keep a provider window refusal through stream fallbacks and pick vision deployments before fitting ones

A stream that fails before content, or a take-over after a refused retry, now carries the provider's context window refusal into its fallback, so a stand-in overflow there is refused locally instead of being sent

An image request now drops text-only deployments before settling estimated overflows, so a fitting text-only deployment no longer wins over a vision deployment whose fit is only estimated

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-25 03:04:19 +00:00
..
adaptive_router fix(anthropic): log the safeguards classifier and compaction summary calls in the parent request's session (#53405) 2026-11-24 01:31:04 -08:00
complexity_router fix(router): count pre-call context-window tokens with each deployment's tokenizer (#49764) 2026-11-25 03:04:19 +00:00
switchyard_router fix(anthropic): log the safeguards classifier and compaction summary calls in the parent request's session (#53405) 2026-11-24 01:31:04 -08:00
__init__.py test: add __init__.py to intermediate tests/unit packages 2026-09-20 12:55:27 +00:00
test_auto_router.py refactor(router): route the router family through RoutingContext and close the outside surface (#49118) 2026-10-30 19:18:54 -07:00
test_auto_router_model_naming.py feat(router): infer custom classifier tiers from model mappings (#48987) 2026-10-29 11:30:23 -07:00
test_auto_router_tuning_baseline.py refactor(router): fence the router surface and document the router-core boundary (#48839) 2026-10-29 00:01:52 +00:00
test_base_routing_strategy.py refactor(router): count the whole router_usage family on the native store (#50862) 2026-11-13 03:22:28 +00:00
test_baseline_request.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_budget_limiter.py fix(router): preserve routing tags after guardrail metadata enrichment (#50488) 2026-11-16 11:34:48 -08:00
test_budget_limiter_hotpath.py test(router_strategy): keep foreign asyncio task noise out of the budget limiter caplog asserts (#43375) 2026-10-29 06:22:06 -07:00
test_complexity_router.py fix(llms): treat chat-latest and gpt-chat-latest as gpt-5 family models (#51151) 2026-11-24 04:02:51 -08:00
test_complexity_tier_predictor.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_fuse_presets.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_lar1_routing.py refactor(router): route the router family through RoutingContext and close the outside surface (#49118) 2026-10-30 19:18:54 -07:00
test_least_busy.py fix(proxy): reserve least-busy picks on Vertex pass-through calls (#53588) 2026-11-23 04:21:20 +00:00
test_litellm_encoder.py fix(router): restore four base behaviors the RoutingContext refactor changed (#49395) 2026-10-31 22:58:34 +00:00
test_llm_v2.py feat(redis): serve the router's session pins from a native store (#50870) 2026-11-13 03:22:28 +00:00
test_lowest_cost.py refactor(rust_bridge): route every Rust-to-Python call through host modules (#50822) 2026-11-11 01:57:54 +00:00
test_lowest_latency.py feat(tts)!: stream audio from the provider as it arrives on /v1/audio/speech (#46222) 2026-11-17 15:03:12 +00:00
test_lowest_tpm_rpm.py refactor(router): count the whole router_usage family on the native store (#50862) 2026-11-13 03:22:28 +00:00
test_lowest_tpm_rpm_v2.py fix(router)!: name the request size when usage-based routing refuses an over-tpm request (#51838) 2026-11-14 16:16:21 -05:00
test_quality_router.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_router_routing_groups.py refactor(router): count the whole router_usage family on the native store (#50862) 2026-11-13 03:22:28 +00:00
test_router_routing_plugins.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_router_tag_regex_routing.py refactor(router): route the router family through RoutingContext and close the outside surface (#49118) 2026-10-30 19:18:54 -07:00
test_router_tag_routing.py fix(router): preserve routing tags after guardrail metadata enrichment (#50488) 2026-11-16 11:34:48 -08:00
test_savings_baseline.py fix(router): register a model_id Bedrock deployment after an ARN deployment for the same profile (#53461) 2026-11-22 22:27:56 +00:00
test_simple_shuffle.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_stall_detector.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00