Skip to content

The agent that grows with you

feat(transport): imply prompt_cache_key capability for api.openai.com

kshitij78e2987e202e
diff --git a/agent/transports/chat_completions.py b/agent/transports/chat_completions.py
index 645d868d928..2572038126b 100644
--- a/agent/transports/chat_completions.py
+++ b/agent/transports/chat_completions.py
@@ -163,6 +163,24 @@ def _is_gemini_openai_compat_base_url(base_url: Any) -> bool:
return normalized.endswith("/openai")
+def _is_openai_api_base_url(base_url: Any) -> bool:
+ """True only for api.openai.com itself (exact host).
+
+ OpenAI documents ``prompt_cache_key`` as a first-class body field and
+ GPT-5.6+ docs recommend it for reliable cache routing, so the flag is
+ implied for the real endpoint. Deliberately NOT a substring match:
+ Azure OpenAI and strict OpenAI-compat endpoints may reject unknown
+ fields and must stay opt-in via ``supports_prompt_cache_key``.
+ """
+ try:
+ from urllib.parse import urlparse
+
+ host = (urlparse(str(base_url or "").strip()).hostname or "").lower()
+ except Exception:
+ return False
+ return host == "api.openai.com"
+
+
def _model_consumes_thought_signature(model: Any) -> bool:
"""True when the outgoing model is a Gemini family model that requires
``extra_content`` (thought_signature) to be replayed on tool calls.
@@ -564,7 +582,8 @@ class ChatCompletionsTransport(ProviderTransport):
api_kwargs,
messages=sanitized,
tools=api_kwargs.get("tools"),
- supports_prompt_cache_key=bool(params.get("supports_prompt_cache_key")),
+ supports_prompt_cache_key=bool(params.get("supports_prompt_cache_key"))
+ or _is_openai_api_base_url(params.get("base_url")),
)
return api_kwargs
diff --git a/tests/agent/transports/test_chat_completions.py b/tests/agent/transports/test_chat_completions.py
index c3f4c3ab0b1..6f034fda5f5 100644
--- a/tests/agent/transports/test_chat_completions.py
+++ b/tests/agent/transports/test_chat_completions.py
@@ -647,6 +647,37 @@ class TestPromptCacheKeyCapability:
assert body["prompt_cache_key"] == kwargs["prompt_cache_key"]
+ def test_openai_api_base_url_implies_capability(self, transport):
+ """api.openai.com gets the key WITHOUT an explicit flag (exact host)."""
+ kwargs = transport.build_kwargs(
+ model="gpt-cache-model",
+ messages=self._messages(),
+ tools=self._tools(),
+ session_id="cron_job_2026-07-15T10:07:00Z",
+ base_url="https://api.openai.com/v1",
+ )
+
+ assert kwargs["prompt_cache_key"].startswith("pck_")
+
+ @pytest.mark.parametrize(
+ "base_url",
+ [
+ "https://myproxy.example.com/api.openai.com/v1", # host embedded in path
+ "https://api.openai.com.evil.example/v1", # prefix-spoofed host
+ "https://eastus.api.cognitive.microsoft.com/openai/v1", # Azure
+ ],
+ )
+ def test_non_openai_hosts_do_not_imply_capability(self, transport, base_url):
+ kwargs = transport.build_kwargs(
+ model="strict-model",
+ messages=self._messages(),
+ tools=self._tools(),
+ session_id="cron_job_2026-07-15T10:08:00Z",
+ base_url=base_url,
+ )
+
+ assert "prompt_cache_key" not in kwargs
+
@pytest.mark.parametrize("provider", [None, "anthropic", "custom"])
def test_default_off_never_leaks_unknown_body_field(self, transport, provider):
from providers import get_provider_profile