Skip to content

The agent that grows with you

refactor(cache): match api.x.ai by hostname and share the Grok slug prefixes

kshitijk4poore174f36ae241
diff --git a/agent/prompt_cache_scope.py b/agent/prompt_cache_scope.py
index ce3b9a3ae66..cd8e44370ae 100644
--- a/agent/prompt_cache_scope.py
+++ b/agent/prompt_cache_scope.py
@@ -13,6 +13,8 @@ import hashlib
import logging
from typing import Any, Optional
+from utils import base_url_hostname
+
logger = logging.getLogger(__name__)
_MEMO_ATTR = "_prompt_cache_scope_memo"
@@ -173,6 +175,9 @@ def resolve_prompt_cache_scope(agent: Any) -> str:
# colons are never mistaken for a fork scope.
FORK_SCOPE_SEPARATOR = "::"
+# Aggregator slugs that front xAI Grok (OpenRouter and friends).
+GROK_AGGREGATOR_MODEL_PREFIXES = ("x-ai/grok-", "xai/grok-")
+
def is_slot_keyed_cache_route(provider: Any, model: Any, base_url: Any = "") -> bool:
"""True when the cache key selects ONE server-side slot per conversation.
@@ -186,9 +191,9 @@ def is_slot_keyed_cache_route(provider: Any, model: Any, base_url: Any = "") ->
"""
if str(provider or "").strip().lower() in {"xai", "xai-oauth"}:
return True
- if "api.x.ai" in str(base_url or "").lower():
+ if base_url_hostname(str(base_url or "")) == "api.x.ai":
return True
- return str(model or "").strip().lower().startswith(("x-ai/grok-", "xai/grok-"))
+ return str(model or "").strip().lower().startswith(GROK_AGGREGATOR_MODEL_PREFIXES)
def is_fork_cache_scope(scope: Any) -> bool:
diff --git a/plugins/model-providers/openrouter/__init__.py b/plugins/model-providers/openrouter/__init__.py
index 09716938cfa..ae969bc07ce 100644
--- a/plugins/model-providers/openrouter/__init__.py
+++ b/plugins/model-providers/openrouter/__init__.py
@@ -4,7 +4,7 @@ import logging
from typing import Any
from agent.portal_tags import get_affinity_scope, get_conversation_context
-from agent.prompt_cache_scope import is_fork_cache_scope
+from agent.prompt_cache_scope import GROK_AGGREGATOR_MODEL_PREFIXES, is_fork_cache_scope
from agent.transports.codex import _cache_scope_from_session_id
from providers import register_provider
from providers.base import ProviderProfile
@@ -195,7 +195,7 @@ class OpenRouterProfile(ProviderProfile):
# the parent's server slot, so honour the fork-derived scope (agent/prompt_cache_scope.py).
if is_fork_cache_scope(context.get("cache_scope_id")):
grok_conv_id = context["cache_scope_id"]
- if grok_conv_id and model and model.startswith(("x-ai/grok-", "xai/grok-")):
+ if grok_conv_id and model and model.startswith(GROK_AGGREGATOR_MODEL_PREFIXES):
top_level["extra_headers"] = {"x-grok-conv-id": grok_conv_id}
return extra_body, top_level