The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(complexity_router): enable session_affinity by default (#33500)
devin-ai-integration[bot]bf3a05878175
diff --git a/litellm/router_strategy/complexity_router/config.py b/litellm/router_strategy/complexity_router/config.pyindex b7ffa2866f2..1f984798970 100644--- a/litellm/router_strategy/complexity_router/config.py+++ b/litellm/router_strategy/complexity_router/config.py@@ -363,10 +363,13 @@ class ComplexityRouterConfig(BaseModel):# Session affinity: pin the first turn's routed model for the rest of the sessionsession_affinity: bool = Field(- default=False,+ default=True,description=("When True and a session_id is resolvable on the request, pin the model chosen on the "- "session's first turn and reuse it for every later turn, skipping re-classification."+ "session's first turn and reuse it for every later turn, skipping re-classification. "+ "On by default so multi-turn sessions stay on one model, preserving provider prompt "+ "caches and avoiding cross-model conversation-history errors. Set False to reclassify "+ "every turn."),)session_affinity_ttl_seconds: int = Field(diff --git a/tests/test_litellm/router_strategy/test_complexity_router.py b/tests/test_litellm/router_strategy/test_complexity_router.pyindex 3404b55f0db..12b2c9abefb 100644--- a/tests/test_litellm/router_strategy/test_complexity_router.py+++ b/tests/test_litellm/router_strategy/test_complexity_router.py@@ -2499,7 +2499,7 @@ class TestRoutingDecisionCauseLogging:class TestSessionAffinity:- """Test the opt-in session_affinity sticky-routing behavior."""+ """Test the session_affinity sticky-routing behavior (on by default)."""REASONING_MESSAGE = [{@@ -2513,14 +2513,19 @@ class TestSessionAffinity:def session_affinity_config(self, basic_config) -> Dict:return {**basic_config, "session_affinity": True}+ @pytest.fixture+ def session_affinity_disabled_config(self, basic_config) -> Dict:+ return {**basic_config, "session_affinity": False}+@staticmethoddef _request_kwargs(session_id: str) -> Dict:return {"metadata": {"session_id": session_id}}@pytest.mark.asyncio- async def test_disabled_by_default_reclassifies_every_turn(self, mock_router_instance, basic_config):- """Regression: session_affinity defaults to False, so a shared session_id must- not pin the model -- each turn is still classified independently."""+ async def test_enabled_by_default_pins_model(self, mock_router_instance, basic_config):+ """Regression: session_affinity defaults to True, so a shared session_id pins the+ first turn's model and later turns reuse it instead of reclassifying."""+ assert "session_affinity" not in basic_configmock_router_instance.cache = DualCache()router = ComplexityRouter(model_name="test-router",@@ -2535,6 +2540,28 @@ class TestSessionAffinity:model="test-model", request_kwargs=request_kwargs, messages=self.SIMPLE_MESSAGE)assert first.model == "o1-preview"+ assert second.model == "o1-preview"++ @pytest.mark.asyncio+ async def test_can_be_disabled_reclassifies_every_turn(+ self, mock_router_instance, session_affinity_disabled_config+ ):+ """Regression: session_affinity=False must still reclassify every turn even when a+ shared session_id is present, so the opt-out keeps working."""+ mock_router_instance.cache = DualCache()+ router = ComplexityRouter(+ model_name="test-router",+ litellm_router_instance=mock_router_instance,+ complexity_router_config=session_affinity_disabled_config,+ )+ request_kwargs = self._request_kwargs("session-1")+ first = await router.async_pre_routing_hook(+ model="test-model", request_kwargs=request_kwargs, messages=self.REASONING_MESSAGE+ )+ second = await router.async_pre_routing_hook(+ model="test-model", request_kwargs=request_kwargs, messages=self.SIMPLE_MESSAGE+ )+ assert first.model == "o1-preview"assert second.model == "gpt-4o-mini"@pytest.mark.asyncio