From 435d6f30b533f47f2b14ccfc5311533a7d3ed459 Mon Sep 17 00:00:00 2001 From: kshitij <82637225+kshitijk4poor@users.noreply.github.com> Date: Sun, 16 Aug 2026 13:28:21 +0530 Subject: [PATCH] fix(caching): match the litellm provider id token-wise too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Self-review follow-up. The previous commit fixed substring matching on the HOST but left the provider-id side as a bare substring, so a user-named provider like `custom:notlitellm` or `mylitellmthing` still matched and was handed Anthropic markers — the same bug class, half-fixed. Both signals now match `litellm` as a whole delimited token via a shared helper. Real spellings (`litellm`, `custom:litellm`, `litellm-router`, and the already-lowercased `LiteLLM`) still match; lookalikes no longer do. Tests: 71 passed. Adds lookalike-provider and real-spelling guards; both new guards mutation-checked. Differential matrix over 2688 configs vs origin/main: 60 changes, every one a Claude model on a genuine LiteLLM route getting the envelope layout, zero pre-existing routes altered. --- agent/agent_runtime_helpers.py | 28 ++++++++++--------- .../test_anthropic_prompt_cache_policy.py | 28 +++++++++++++++++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py index 3564c32c3e..be39e3ad47 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 2e6f7889bb..d76d70e362 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"] )