Skip to content

fix: honor cache_control on system blocks in Anthropic -> Responses bridge - #41426

Closed
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
litellm_anthropic_responses_cache_control_alias
Closed

devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
litellm_anthropic_responses_cache_control_alias

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Claude Code's cache_control system blocks were flattened into Responses instructions
  • Responses-mode deployments (bedrock_mantle GPT-5.6) then read zero cached tokens each turn

How it solves it:

  • The /v1/messages -> Responses bridge treats cache_control as an explicit breakpoint
  • System prompt stays a developer message in input with prompt_cache_breakpoint

User Flow

Before: a developer points Claude Code at the proxy for an OpenAI GPT-5.6 deployment served over the Responses API and pays cache-write rates with no cache reads

  1. They send POST http://localhost:4000/v1/messages with a system array whose last block carries cache_control: {"type": "ephemeral"} and a short user turn
  2. The response usage shows cache_creation_input_tokens > 0 and cache_read_input_tokens: 0
  3. They send the identical request again a few seconds later
  4. The response again shows cache_read_input_tokens: 0, so every turn is billed as a fresh cache write

After: the same requests hit the cache on the warm call

  1. They send the same POST http://localhost:4000/v1/messages with the cache_control system block
  2. The response shows cache_creation_input_tokens > 0 and cache_read_input_tokens: 0
  3. They send the identical request again a few seconds later
  4. The response now shows cache_read_input_tokens > 0 covering the system prompt

Relevant issues

Fixes #41424

Affected release

Linear ticket

Resolves LIT-7865

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to 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

This session's network policy blocks every LLM provider host and the npm registry, so no live proxy call and no Claude Code run was possible here. The proof below is the reporter's offline reproduction from #41424, run at the merge base and at the tip. A live before/after leg driven from Claude Code against a real Responses deployment still needs to be run before this merges

Shared setup: repro.py is the script from the issue body (it builds a /v1/messages request whose system block carries either cache_control or prompt_cache_breakpoint, runs it through the bridge, and prints whether the system prompt left as instructions or as a developer message)

Before (9cd7873)

  1. git checkout 9cd787386 && uv run --no-sync python repro.py
  2. Observed:
['cache_control'] -> instructions
['prompt_cache_breakpoint'] -> developer message

After (5fafe03)

  1. git checkout litellm_anthropic_responses_cache_control_alias && uv run --no-sync python repro.py
  2. Observed:
['cache_control'] -> developer message
['prompt_cache_breakpoint'] -> developer message

Type

🐛 Bug Fix

Caveats (if any)

High

  • The reporter's bedrock_mantle deployment does not reach this bridge at all
    • /v1/messages only takes the Anthropic -> Responses bridge for custom_llm_provider == "openai"
    • bedrock_mantle (and every other provider) goes Anthropic -> chat completions -> Responses
    • on that route the outbound input keeps the system message but the Responses config strips cache_control, so cache reads are still zero after this PR
    • closing that route needs fix(responses): keep cache breakpoints on blocks the bridge stringifies #40032 (keeps prompt_cache_breakpoint through the chat -> Responses bridge) plus a cache_control alias in that bridge; tracked on LIT-7108
  • No live proof yet: provider hosts are blocked from this environment, see the proof section

Low

  • A block carrying both markers keeps its prompt_cache_breakpoint value untouched
  • Anthropic ttl on cache_control is not forwarded; Responses breakpoints have no ttl field

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/cc0d2cb2dd364964bca02e05ea4401a9
Open in Devin Desktop: https://app.devin.ai/desktop/session/cc0d2cb2dd364964bca02e05ea4401a9?variant=devin


Note

Low Risk
Scoped to Anthropic→Responses request translation for system blocks; behavior change only when cache_control is present, with tests covering precedence and edge cases.

Overview
Treats Anthropic cache_control on system text blocks like prompt_cache_breakpoint in the /v1/messages → Responses API bridge, so cached system prompts are not flattened into instructions (which broke prompt cache reads for clients such as Claude Code).

A new _system_block_cache_breakpoint helper prefers an existing prompt_cache_breakpoint and otherwise maps any cache_control marker to {"mode": "explicit"}. Top-level system arrays with either marker still become a leading developer message with prompt_cache_breakpoint on the right input_text parts; mid-turn system messages get the same mapping. cache_control is not forwarded on the outbound payload.

Tests cover cache_control → developer message, explicit breakpoint precedence, and mid-turn system blocks.

Reviewed by Cursor Bugbot for commit 5fafe03. Bugbot is set up for automated code reviews on this repo. Configure here.

…ridge

Fixes #41424

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 16, 2026 14:40
@codspeed

codspeed Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_anthropic_responses_cache_control_alias (5fafe03) with main (9cd7873)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR preserves Anthropic system-block cache markers when bridging requests to the Responses API. Marked system prompts now become leading developer messages with explicit prompt-cache breakpoints, while existing explicit breakpoints retain precedence. Focused tests cover system and mid-turn conversion behavior

Confidence Score: 5/5

The PR appears safe to merge, with the intended cache marker preserved through the existing Responses developer-message path

No actionable failure remains. The marker is emitted as the expected plain dictionary, explicit breakpoints retain precedence, and unmarked system prompts keep their existing instructions behavior

Important Files Changed
Filename Overview
litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py Maps Anthropic cache_control markers to explicit Responses cache breakpoints and selects developer-message routing for marked system prompts
tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_transformation.py Adds focused regressions for system markers, explicit-breakpoint precedence, and mid-turn system conversion

Reviews (1): Last reviewed commit: "fix: honor cache_control on system block..." | Re-trigger Greptile

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 5fafe03. Configure here.

@mateo-berri

Copy link
Copy Markdown
Contributor

Closing: OpenAI already caches the flattened instructions prefix implicitly (warm cached=3532/3535, gpt-5.6-terra), and gpt-4.1 rejects prompt_cache_breakpoint with a 400, regressing older openai/ deployments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Anthropic /v1/messages → Responses bridge drops cache_control, so Claude Code gets zero prompt-cache reads on bedrock_mantle gpt-5.6

2 participants