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

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.
This commit is contained in:
kshitij
2026-08-16 13:28:21 +05:30
parent 1b0e953b47
commit 435d6f30b5
2 changed files with 43 additions and 13 deletions
+15 -13
View File
@@ -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(
@@ -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"]
)