feat: add LAR-1 semantic routing strategy - #31295
Conversation
Greptile SummaryThis PR adds a new
Confidence Score: 5/5This PR is safe to merge; the routing logic, init wiring, and update_settings re-link path all work correctly for normal usage. The new routing strategy correctly uses set_custom_routing_strategy to override the instance-level async_get_available_deployment, so the class-level fallback guard never fires for LAR-1 requests. Both the init and update_settings paths call apply_lar1_routing_strategy, and the routing_strategy_args re-link flag correctly propagates threshold changes made independently of a strategy switch. No data-path bugs or auth issues were found. litellm/router_strategy/lar1_routing.py — minor import path and OTEL span propagation gaps worth addressing before this strategy sees heavy production use.
|
| Filename | Overview |
|---|---|
| litellm/router_strategy/lar1_routing.py | New routing strategy; logic is sound, but imports CustomRoutingStrategyBase from litellm.router (a re-exporter) instead of litellm.types.router, and async_get_healthy_deployments is called without propagating parent_otel_span. |
| litellm/router.py | LAR-1 is correctly wired at both init and update_settings; routing_strategy_args re-link logic handles the independent-args-update case properly. The existing async_get_available_deployment guard (lines 11338-11351) doesn't whitelist "lar1", but this is moot in practice because set_custom_routing_strategy shadows the class method with an instance attribute. |
| litellm/types/lar1.py | Clean Pydantic model with sensible defaults and ge/le validation on confidence; no issues. |
| tests/test_litellm/router_strategy/test_lar1_routing.py | 29 tests covering all key paths; no real network calls (fake API keys, internal health checks only); mocks used where needed for edge cases. |
| examples/lar1_ollama_config.yaml | Demo config; correctly maps all four LAR-1 tiers to Ollama models under a shared model_name alias. |
Reviews (3): Last reviewed commit: "feat: add LAR-1 semantic routing strateg..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
54e22d1 to
f476256
Compare
f476256 to
5d9f242
Compare
|
@greptileai please re-review Addressed the prior 3/5 feedback in commit b662cfe:
PR description updated with routing rules, config example, and proof-of-fix. 29 tests in |
Optional router strategy that picks a deployment tier from request_kwargs.metadata.lar1 (confidence, evidence, time). Deployments are tagged with model_info.type (cloud-smart, cloud-fast, local, deep). Thresholds are configurable via routing_strategy_args. Includes 30 unit tests and an Ollama example config. Co-authored-by: Cursor <cursoragent@cursor.com>
bfd1bee to
b0e2508
Compare
|
Ready for maintainer review. Checklist
Scope: additive If helpful, recent Per contributing guide, also available on LiteLLM Slack #pr-review if that is faster. |
|
@greptileai please re-review after squash to single commit b0e2508 (no logic changes, history cleanup only) |
| kwargs.get("routing_strategy_args"), | ||
| ) | ||
| else: | ||
| self.routing_strategy_init( |
There was a problem hiding this comment.
Medium: Disabling LAR-1 leaves its selector active
apply_lar1_routing_strategy() shadows both deployment-selection methods on the router instance, but this branch does not remove those attributes when switching away from LAR-1. An authenticated caller can therefore continue forcing cloud-smart or deep deployments with crafted metadata.lar1 after an operator changes the strategy to simple-shuffle or another policy. Restore the class methods before initializing the replacement strategy, or integrate LAR-1 into the normal strategy-selector dispatch rather than monkey-patching the router.
| confidence_threshold_high: 0.7 | ||
|
|
||
| general_settings: | ||
| master_key: sk-lar1-demo |
There was a problem hiding this comment.
Medium: Known master key in executable example
Anyone who can reach a proxy started from this example can authenticate with the repository-known master key and access administrative routes and configured models. Require the operator to provide a secret instead.
| master_key: sk-lar1-demo | |
| master_key: os.environ/LITELLM_MASTER_KEY |
PR overviewThis pull request adds the LAR-1 semantic routing strategy to the LiteLLM router, including logic for selecting between deployment modes such as cloud-smart and deep. It also includes an example Ollama configuration for using the new routing strategy. There are two open security concerns. The router can leave LAR-1 selection hooks active after switching to another routing policy, allowing an authenticated caller to keep steering requests to specific deployment classes. The included executable example also contains a repository-known master key, which could grant administrative access if an operator runs it as-is on a reachable proxy. No issues have been addressed yet, so the PR still carries moderate security risk. Open issues (2)
Fixed/addressed: 0 · PR risk: 6/10 |
|
Both issues are addressed in cloudiaspecula#1: Stale selector after strategy switch — Known master key in example — Replaced hardcoded PR: cloudiaspecula#1 |
Relevant issues
N/A (new feature)
Summary
Adds optional LAR-1 semantic routing. Instead of routing by latency or cost, the router picks a deployment from agent metadata in
request_kwargs.metadata.lar1(confidence,evidence,time). Each deployment is tagged withmodel_info.type:cloud-smart,cloud-fast,local, ordeep. All LAR-1 tiers must share the samemodel_namealias (seeexamples/lar1_ollama_config.yaml). Routing is local classification only; no extra LLM calls.LAR-1 complements
complexity_router: that router scores request text; LAR-1 scores agent state from the caller. References: SSRN 6981858, LAR-1 RFC v0.9Enable in proxy config:
Or in code:
apply_lar1_routing_strategy(router, routing_strategy_args)/Router(routing_strategy="lar1", ...).Routing rules (default thresholds)
evidencecontainsUNVERIFIEDcloud-smarttimeisMEMcloud-fastconfidence< 0.3cloud-smartconfidence< 0.5cloud-fastconfidence< 0.7localconfidence>= 0.7deepOverride thresholds via
routing_strategy_args.confidence_threshold_low|medium|high(single source:DEFAULT_THRESHOLDSinlar1_routing.py).Implementation notes
LAR1RoutingStrategyusesasync_get_healthy_deployments(cooldown/health checks)LAR1Metadatavalidation on the hot path; invalid metadata falls back to defaults with a warningget_available_deployment()raisesNotImplementedErrorRouter.update_settings(routing_strategy="lar1", ...)androuting_strategy_argsupdates re-wire the strategy viaapply_lar1_routing_strategylitellm_lar1-routing, base retargeted tolitellm_oss_stagingfor fork CI)litellm_lar1-routingfor review (b0e25089ad)Files
litellm/router_strategy/lar1_routing.py— strategy, thresholds, metadata parsinglitellm/types/lar1.py—LAR1Metadata+ enums (LAR1EvidenceincludesCONFIRMED)litellm/router.py—lar1in valid strategies; init +update_settingswiringtests/test_litellm/router_strategy/test_lar1_routing.py— 30 unit testsexamples/lar1_ollama_config.yaml— local Ollama proof-of-fix configPre-Submission checklist
make test-unit(GitHub Actions green on latest commit)@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer review (5/5)Test plan
pytest tests/test_litellm/router_strategy/test_lar1_routing.py -v— 30 passed locallylitellm_oss_stagingbase (fork PR)Coverage includes: confidence bands, UNVERIFIED/MEM overrides, custom thresholds, invalid metadata, healthy-deployment edge cases,
update_settingslar1 wiring, fallback logging,Router(routing_strategy="lar1")init.Screenshots / Proof of Fix
Ollama must be running. From repo root:
Low confidence (
0.2->cloud-smart->ollama/qwen3.5:9b):High confidence (
0.8->deep->ollama/lfm2.5-thinking:latest):Log proof:
grep '\[LAR-1\]' lar1_proxy.logExpected:
Type
New Feature