mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* feat(router): steer flexible teams to a permitted alternate model when their preferred model is saturated
Teams opt in with metadata.model_preferences: [{"model": "luna", "alternates": ["opus"]}].
The dynamic rate limiter v3 pre-call hook reads the saturation cache for the preferred model
and, once it is at or above saturation_threshold, rewrites the request to the first alternate
the team may call that is below the threshold. The response carries
x-litellm-model-steered-from with the preferred model. Rate limits and spend are charged to
the model that served. Teams without model_preferences are untouched.
* fix(dynamic_rate_limiter_v3): type the hook request dicts so the steering rewrite passes the lint gates
* fix(dynamic_rate_limiter_v3): authorize alternates against the key and its model budget, send the steering header on streams
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
* fix(dynamic_rate_limiter_v3): enforce member and project allowlists on alternates, skip the steering header for non Latin-1 names
* fix(dynamic_rate_limiter_v3): check every budget layer for an alternate, fall back to the preferred model when the alternate refuses, merge the served model's guardrails
* fix(dynamic_rate_limiter_v3): never steer agent keys, type the steering unit tests
* fix(dynamic_rate_limiter_v3): hold alternates to customer grants, access group budgets, guardrails and the zero-cost waiver
An alternate now passes every check a budget fallback passes. Customer model grants are enforced, the access groups that grant the alternate are budget checked and stamped on the token and request metadata so spend charges them, a zero-cost preferred model that waived the aggregate budget checks is never steered to a priced alternate, and an alternate whose deployments add model-level guardrails the request did not run is skipped.
* fix(dynamic_rate_limiter_v3): stamp the alternate's groups on every bucket and skip alternates the request cannot safely take
The served model's access groups now land on every metadata bucket and the auth copy inside it even when the preferred model had none, so group spend is charged. An alternate is skipped when a budget reservation already holds a different group, when it does not serve the request's endpoint, when unpriced models are blocked and it has no price, or when its guardrails differ from the request's by name or settings. A failing alternate check or alternate admission leaves the request on the preferred model instead of failing it.
* fix(dynamic_rate_limiter_v3): keep a reserved budget off pricier alternates
While the request holds a budget reservation sized for the preferred model, an alternate that costs more per token is skipped, so fail-closed budget enforcement never admits a call the reservation does not cover.
* fix(dynamic_rate_limiter_v3): an unpriced alternate never takes a reserved budget
While a budget reservation is held, an alternate with no input or output price is skipped rather than treated as free.
---------
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
144 lines
5.7 KiB
Python
144 lines
5.7 KiB
Python
import json
|
|
import uuid
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
from typing import Final
|
|
|
|
import httpx
|
|
import pytest
|
|
import yaml
|
|
from integration._support.client import Gateway, Scenario, eventually, gateway_from_environment
|
|
from integration._support.process import owned_proxy
|
|
from integration._support.wire import Reply, Request, wire_server
|
|
|
|
OPENAI_MODEL: Final = "gpt-5.6"
|
|
PREFERRED_RPM: Final = 2
|
|
ALTERNATE_RPM: Final = 1000
|
|
SATURATION_THRESHOLD: Final = 0.5
|
|
STEERED_FROM_HEADER: Final = "x-litellm-model-steered-from"
|
|
|
|
|
|
def _reply() -> bytes:
|
|
return json.dumps(
|
|
{
|
|
"id": f"chatcmpl_{uuid.uuid4().hex}",
|
|
"object": "chat.completion",
|
|
"created": 1700000000,
|
|
"model": OPENAI_MODEL,
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "steered"}, "finish_reason": "stop"}],
|
|
"usage": {"prompt_tokens": 20, "completion_tokens": 10, "total_tokens": 30},
|
|
}
|
|
).encode()
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def steering_proxy(tmp_path_factory: pytest.TempPathFactory) -> Iterator[tuple[Gateway, str]]:
|
|
directory: Final = tmp_path_factory.mktemp("model-preference-steering")
|
|
base: Final = yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text())
|
|
configuration: Final = {
|
|
**base,
|
|
"litellm_settings": {
|
|
**base["litellm_settings"],
|
|
"callbacks": ["dynamic_rate_limiter_v3"],
|
|
"priority_reservation_settings": {
|
|
"saturation_threshold": SATURATION_THRESHOLD,
|
|
"saturation_check_cache_ttl": 1,
|
|
},
|
|
},
|
|
}
|
|
path: Final = directory / "model_preference_steering.yaml"
|
|
path.write_text(yaml.safe_dump(configuration))
|
|
|
|
def respond(request: Request) -> Reply:
|
|
assert request.method == "POST" and request.target == "/v1/chat/completions", request.target
|
|
return Reply(body=_reply())
|
|
|
|
with (
|
|
gateway_from_environment() as rig,
|
|
wire_server(respond) as wire,
|
|
owned_proxy(rig, directory, {}, config=path) as owned,
|
|
):
|
|
yield owned, wire.url
|
|
|
|
|
|
def _deployment(owned: Gateway, scenario: Scenario, upstream: str, rpm: int) -> tuple[str, str]:
|
|
name: Final = scenario.model(
|
|
model=f"openai/{OPENAI_MODEL}", api_base=f"{upstream}/v1", api_key="synthetic-key", rpm=rpm
|
|
)
|
|
rows: Final = owned.rows('SELECT model_id FROM "LiteLLM_ProxyModelTable" WHERE model_name = %s', (name,))
|
|
return name, str(rows[0]["model_id"])
|
|
|
|
|
|
def _chat(owned: Gateway, key: str, model: str) -> httpx.Response:
|
|
return owned.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": model, "messages": [{"role": "user", "content": f"model preference steering {uuid.uuid4().hex}"}]},
|
|
key=key,
|
|
)
|
|
|
|
|
|
def _spend_row(owned: Gateway, response: httpx.Response) -> list[dict[str, object]]:
|
|
return owned.rows(
|
|
'SELECT model_group FROM "LiteLLM_SpendLogs" WHERE request_id = %s', (str(response.json()["id"]),)
|
|
)
|
|
|
|
|
|
def test_flexible_team_is_steered_to_its_permitted_alternate_once_the_preferred_model_saturates(
|
|
steering_proxy: tuple[Gateway, str],
|
|
) -> None:
|
|
owned, upstream = steering_proxy
|
|
with owned.scenario() as scenario:
|
|
preferred, preferred_id = _deployment(owned, scenario, upstream, PREFERRED_RPM)
|
|
alternate, alternate_id = _deployment(owned, scenario, upstream, ALTERNATE_RPM)
|
|
rigid_key: Final = scenario.key(team_id=scenario.team(models=[preferred, alternate]))
|
|
flexible_key: Final = scenario.key(
|
|
team_id=scenario.team(
|
|
models=[preferred, alternate],
|
|
metadata={"model_preferences": [{"model": preferred, "alternates": [alternate]}]},
|
|
)
|
|
)
|
|
|
|
warmup: Final = _chat(owned, rigid_key, preferred)
|
|
assert warmup.status_code == 200, warmup.text
|
|
assert warmup.headers["x-litellm-model-id"] == preferred_id
|
|
assert STEERED_FROM_HEADER not in warmup.headers
|
|
|
|
steered: Final = eventually(
|
|
lambda: _chat(owned, flexible_key, preferred),
|
|
lambda response: STEERED_FROM_HEADER in response.headers,
|
|
seconds=15,
|
|
return_last_on_timeout=True,
|
|
)
|
|
assert steered.status_code == 200, steered.text
|
|
assert steered.headers[STEERED_FROM_HEADER] == preferred
|
|
assert steered.headers["x-litellm-model-id"] == alternate_id
|
|
|
|
rigid: Final = _chat(owned, rigid_key, preferred)
|
|
assert rigid.status_code == 200, rigid.text
|
|
assert rigid.headers["x-litellm-model-id"] == preferred_id
|
|
assert STEERED_FROM_HEADER not in rigid.headers
|
|
|
|
charged: Final = eventually(lambda: _spend_row(owned, steered), lambda rows: len(rows) == 1, seconds=30)
|
|
assert charged[0]["model_group"] == alternate
|
|
|
|
|
|
def test_alternate_the_team_may_not_call_is_never_used(steering_proxy: tuple[Gateway, str]) -> None:
|
|
owned, upstream = steering_proxy
|
|
with owned.scenario() as scenario:
|
|
preferred, _ = _deployment(owned, scenario, upstream, PREFERRED_RPM)
|
|
forbidden, _ = _deployment(owned, scenario, upstream, ALTERNATE_RPM)
|
|
key: Final = scenario.key(
|
|
team_id=scenario.team(
|
|
models=[preferred],
|
|
metadata={"model_preferences": [{"model": preferred, "alternates": [forbidden]}]},
|
|
)
|
|
)
|
|
|
|
first: Final = _chat(owned, key, preferred)
|
|
assert first.status_code == 200, first.text
|
|
second: Final = eventually(
|
|
lambda: _chat(owned, key, preferred), lambda response: response.status_code != 200, seconds=15
|
|
)
|
|
assert second.status_code == 429, second.text
|
|
assert STEERED_FROM_HEADER not in second.headers
|