Skip to content

The agent that grows with you

fix(caching): match the litellm provider id token-wise too

kshitij435d6f30b533
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index 3564c32c3e6..be39e3ad473 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -2150,23 +2150,25 @@ def _is_litellm_route(provider_lower: str, base_url: str) -> bool:
Provider naming varies per install (``litellm``, ``custom:litellm``, or a
bare ``custom`` alias pointed at a LiteLLM host), so both signals are
- checked. The host side matches ``litellm`` as a whole dot- or
- hyphen-delimited label token rather than a raw substring:
- ``base_url_hostname``'s own docstring names substring host matching as the
- false-positive class to avoid, and a plain ``"litellm" in hostname`` grants
- Anthropic markers to unrelated hosts like ``notlitellm.example.com``.
+ checked. Both match ``litellm`` as a whole delimited token rather than a
+ raw substring: ``base_url_hostname``'s own docstring names substring host
+ matching as the false-positive class to avoid, and a plain
+ ``"litellm" in ...`` grants Anthropic markers to unrelated routes like
+ ``notlitellm.example.com`` or a provider named ``custom:notlitellm``.
A ``litellm`` *path* segment never qualifies — only the host does.
"""
- if "litellm" in provider_lower:
+ if _has_litellm_token(provider_lower, ":-_/"):
return True
- hostname = base_url_hostname(base_url)
- if not hostname:
+ return _has_litellm_token(base_url_hostname(base_url), ".-")
+
+
+def _has_litellm_token(value: str, delimiters: str) -> bool:
+ """True when ``value`` contains ``litellm`` as a whole delimited token."""
+ if not value:
return False
- return any(
- "litellm" == token
- for label in hostname.split(".")
- for token in label.split("-")
- )
+ for delimiter in delimiters:
+ value = value.replace(delimiter, " ")
+ return "litellm" in value.split()
def anthropic_prompt_cache_policy(
diff --git a/tests/run_agent/test_anthropic_prompt_cache_policy.py b/tests/run_agent/test_anthropic_prompt_cache_policy.py
index 2e6f7889bba..d76d70e3623 100644
--- a/tests/run_agent/test_anthropic_prompt_cache_policy.py
+++ b/tests/run_agent/test_anthropic_prompt_cache_policy.py
@@ -657,6 +657,34 @@ class TestLiteLLMOpenAIWire:
)
assert agent._anthropic_prompt_cache_policy() == (False, False)
+ @pytest.mark.parametrize(
+ "provider", ["custom:notlitellm", "notlitellm", "mylitellmthing"]
+ )
+ def test_litellm_lookalike_provider_names_do_not_cache(self, provider):
+ # The provider signal is token-wise for the same reason as the host:
+ # a user-named provider that merely contains "litellm" is not a
+ # LiteLLM route and must not be handed Anthropic markers.
+ agent = _make_agent(
+ provider=provider,
+ base_url="https://gateway.attacker.example/v1",
+ api_mode="chat_completions",
+ model="claude-opus-4.8",
+ )
+ assert agent._anthropic_prompt_cache_policy() == (False, False)
+
+ @pytest.mark.parametrize(
+ "provider", ["litellm", "custom:litellm", "litellm-router", "LiteLLM"]
+ )
+ def test_litellm_provider_spellings_still_cache(self, provider):
+ # ...while every real spelling of a LiteLLM provider id still matches.
+ agent = _make_agent(
+ provider=provider,
+ base_url="https://gateway.internal.example/v1",
+ api_mode="chat_completions",
+ model="claude-opus-4.8",
+ )
+ assert agent._anthropic_prompt_cache_policy() == (True, False)
+
@pytest.mark.parametrize(
"api_mode", ["codex_responses", "bedrock_converse", "codex_app_server"]
)