Repository navigation
fix(anthropic): only inject cache_control when the request carries none - #33886
Conversation
Greptile SummaryThis PR fixes a bug where
Confidence Score: 5/5Safe to merge — the change is scoped to the Anthropic cache-control injection hook, the stand-down logic is correctly guarded at both entry points, and the judged stamp reliably prevents re-judgment on all known re-entry paths. The stand-down predicate is evaluated with the right parameters at each entry point: No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/integrations/anthropic_cache_control_hook.py | Adds _should_stand_down / _stamped_as_judged helpers and wires them into both the /chat/completions seeding path and the /v1/messages injection path; also extends _request_has_cache_control to handle OpenAI-shaped tool definitions. Logic is correct and well-guarded. |
| litellm/types/integrations/anthropic_cache_control_hook.py | Adds NotRequired[bool] _litellm_judged field to both TypedDicts so the internal stamp is type-safe; import of NotRequired from typing_extensions is correct. |
| tests/test_litellm/integrations/test_anthropic_cache_control_hook.py | Adds TestConfiguredInjectionPointsStandDown class with 10 new unit tests covering all stand-down scenarios; updates existing test assertion to include the new judged stamp (correctly reflects changed behavior, not a regression mask); all tests use mocks with no real network calls. |
Reviews (3): Last reviewed commit: "fix(anthropic): carry the stand-down jud..." | Re-trigger Greptile
|
bugbot run |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit e064857. Configure here.
Relevant issues
cache_control_injection_pointsnow stands down entirely when the request already carries clientcache_controlanywhere (messages, system, or tools)enable_anthropic_prompt_cachingflag already promiseLinear ticket
Resolves LIT-4582
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Live proxy on
127.0.0.1:4152, real Anthropic API, modelanthropic/claude-sonnet-5configured with an injection point, exactly the v1.92.0 rollout shape:The client request carries 4 breakpoints of its own:
cache_controlon 3 content blocks plus 1 on a tool definition (marked_chat.json;marked_v1.jsonis the same request in Anthropic shape). The tool breakpoint is invisible to the injection cap, so unfixed litellm injects a 5th onto the system promptBefore, at 366ec6f (branch point, unfixed):
After, at 0268d01 (this PR), the same two requests succeed and the client's own 4 breakpoints reach Anthropic untouched (the cache the first call writes is read back by the second):
A request with no client
cache_controlstill gets the configured injection, so the feature keeps working for the traffic it was built for (same output at both SHAs):All three requests were re-run at e064857 (the judged-stamp revision) with identical results: both marked requests 200 with the client's four breakpoints intact, the unmarked request cached by injection (
cache_creation_input_tokens: 3363on a cold cache)Type
🐛 Bug Fix
Changes
cache_control_injection_pointsnow stand down entirely when the request already carries clientcache_controlanywhere: message level, content block, system block, or tool definitiontool["function"]["cache_control"]), which the Anthropic chat transform accepts as a client breakpoint but the predicate missed; this hardens theenable_anthropic_prompt_cachingpath toomaybe_seed_default_injection_pointsfor /chat/completions,maybe_inject_cache_controlfor /v1/messages); non-message points written back for provider transforms carry an internal judged stamp so no later pass re-judges themcache_controlkeep getting the configured points, and the existing per-point overwrite guard and four-block cap still apply among litellm's own pointsThe docs already promise this contract ("Never double-injects. If the request already carries its own
cache_control, LiteLLM stands down and the client's checkpoints win", https://docs.litellm.ai/docs/tutorials/prompt_caching); the code only honored it on the newerenable_anthropic_prompt_cachingpath, not on the config-driven path this ticket is aboutTwo things a reviewer will ask about. Full stand-down was chosen over teaching the four-block cap to count tool breakpoints because counting only avoids the 400; partial injection would still reshape the caching layout of a client that is managing its own breakpoints, which is the other half of the complaint. The judged stamp exists because the injection machinery re-runs on the same request through several paths (
acompletionre-enterscompletion, the async /v1/messages entry re-dispatches into the sync handler, and interceptor sub-calls re-enteranthropic_messageswith the outer request's kwargs); by then the messages carry litellm's own injected marks, so a re-judgment would misread them as client breakpoints and drop the written-backtool_configremainder the Bedrock transform consumes. Carrying the judgment inside the written-back points makes every such re-entry, present and future, skip it by construction; an earlier revision gated this with per-entryis_first_passflags, which review correctly showed cannot cover re-entry paths that reuse kwargs wholesaleFinal Attestation
Note
Medium Risk
Changes behavior on the Anthropic prompt-caching path for proxy and native messages traffic, but narrows injection to match documented contract and reduces provider 400 risk; re-entry stamping is localized to injection-point handling.
Overview
Config-driven
cache_control_injection_pointsnow honor the same stand-down rule as auto-caching: if the request already has clientcache_controlon messages, system blocks, or tools, LiteLLM skips injection instead of adding more breakpoints (fixing Anthropic 400s when clients cache tools plus content).Stand-down runs once per request via
_should_stand_downon/chat/completionsseeding (maybe_seed_default_injection_points) and/v1/messages(maybe_inject_cache_control). Non-message points written back for provider transforms are copied with_litellm_judgedso later re-entries (async/sync dispatch, interceptor sub-calls) do not treat LiteLLM’s own marks as client breakpoints.Tool detection now treats OpenAI-shaped tools (
function.cache_control) as client breakpoints, matching what the Anthropic chat transform already accepts.Reviewed by Cursor Bugbot for commit e064857. Bugbot is set up for automated code reviews on this repo. Configure here.