Skip to content

The agent that grows with you

fix(openrouter): give auxiliary calls the sticky routing key

teknium11c0884516c1a
diff --git a/plugins/model-providers/openrouter/__init__.py b/plugins/model-providers/openrouter/__init__.py
index 11c814591ec..3abf291e144 100644
--- a/plugins/model-providers/openrouter/__init__.py
+++ b/plugins/model-providers/openrouter/__init__.py
@@ -3,6 +3,7 @@
import logging
from typing import Any
+from agent.portal_tags import get_conversation_context
from providers import register_provider
from providers.base import ProviderProfile
@@ -77,8 +78,26 @@ class OpenRouterProfile(ProviderProfile):
self, *, session_id: str | None = None, **context: Any
) -> dict[str, Any]:
body: dict[str, Any] = {}
- if session_id:
- body["session_id"] = session_id
+ # Top-level session_id → OpenRouter's sticky routing key. Per their
+ # prompt-caching docs it is used directly as the routing key instead of
+ # hashing the opening messages, and it activates stickiness on the
+ # first successful request rather than only after a cache hit.
+ #
+ # Resolve it from the ambient conversation contextvar first, explicit
+ # argument as fallback. The gap this closes is the auxiliary call sites
+ # — compression, title generation, vision, web_extract, session_search,
+ # MoA slots — which funnel through ``agent.auxiliary_client``. That
+ # module has no session handle and passes no ``session_id``, so those
+ # calls sent NO sticky key at all and each routed independently of the
+ # conversation it belonged to (#70820).
+ #
+ # Mirrors the Nous Portal profile, which resolves the same way
+ # (f2f4df064d). The ambient value is the session-lineage ROOT, so it
+ # also stays stable for installs that opt out of the default
+ # ``compression.in_place: true`` and across delegate-subagent trees.
+ sticky_key = get_conversation_context() or session_id
+ if sticky_key:
+ body["session_id"] = sticky_key
prefs = context.get("provider_preferences")
if prefs:
body["provider"] = prefs
@@ -159,8 +178,13 @@ class OpenRouterProfile(ProviderProfile):
else:
extra_body["reasoning"] = {"enabled": True, "effort": "medium"}
- if session_id and model and model.startswith(("x-ai/grok-", "xai/grok-")):
- extra_headers["x-grok-conv-id"] = session_id
+ # Same resolution as build_extra_body: xAI's prompt cache is pinned per
+ # backend server via this header, and aux calls pass no session_id, so
+ # reading the ambient conversation keeps compression/vision/MoA traffic
+ # on the same Grok backend as the conversation it belongs to.
+ grok_conv_id = get_conversation_context() or session_id
+ if grok_conv_id and model and model.startswith(("x-ai/grok-", "xai/grok-")):
+ extra_headers["x-grok-conv-id"] = grok_conv_id
if extra_headers:
top_level["extra_headers"] = extra_headers
diff --git a/tests/providers/test_provider_profiles.py b/tests/providers/test_provider_profiles.py
index 0e95623a37e..9df895af8dd 100644
--- a/tests/providers/test_provider_profiles.py
+++ b/tests/providers/test_provider_profiles.py
@@ -112,6 +112,53 @@ class TestOpenRouterProfile:
body = p.build_extra_body()
assert body == {}
+ def test_aux_call_inherits_ambient_conversation_as_sticky_key(self):
+ """Auxiliary calls pass no session_id but must still route stickily.
+
+ Compression, titles, vision, web_extract, session_search and MoA slots
+ funnel through ``agent.auxiliary_client``, which has no session handle.
+ Before the ambient resolution they sent NO sticky key at all and each
+ routed independently of its conversation (#70820).
+ """
+ from agent.portal_tags import (
+ reset_conversation_context,
+ set_conversation_context,
+ )
+
+ p = get_provider_profile("openrouter")
+ token = set_conversation_context("root-conversation")
+ try:
+ assert p.build_extra_body()["session_id"] == "root-conversation"
+ # An explicitly-passed segment id never beats the lineage root.
+ body = p.build_extra_body(session_id="segment-after-rotation")
+ assert body["session_id"] == "root-conversation"
+ finally:
+ reset_conversation_context(token)
+
+ def test_grok_cache_header_inherits_ambient_conversation(self):
+ """The xAI cache-affinity header resolves the same way as the body key."""
+ from agent.portal_tags import (
+ reset_conversation_context,
+ set_conversation_context,
+ )
+
+ p = get_provider_profile("openrouter")
+ token = set_conversation_context("root-conversation")
+ try:
+ _, top_level = p.build_api_kwargs_extras(
+ supports_reasoning=False, model="x-ai/grok-4"
+ )
+ headers = top_level.get("extra_headers", {})
+ assert headers["x-grok-conv-id"] == "root-conversation"
+
+ # Still model-gated: non-Grok models get no affinity header.
+ _, other = p.build_api_kwargs_extras(
+ supports_reasoning=False, model="anthropic/claude-sonnet-4.6"
+ )
+ assert "x-grok-conv-id" not in other.get("extra_headers", {})
+ finally:
+ reset_conversation_context(token)
+
def test_pareto_min_coding_score_emitted_for_pareto_model(self):
"""min_coding_score → plugins block when model is openrouter/pareto-code."""
p = get_provider_profile("openrouter")