Skip to content

fix(anthropic): apply tool_config cache-control points on chat and /v1/messages - #47394

Merged
klu-berri merged 4 commits into
mainfrom
litellm_anthropic_tool_config_cache_point
Oct 19, 2026
Merged

klu-berri merged 4 commits into
mainfrom
litellm_anthropic_tool_config_cache_point

Conversation

@klu-berri

@klu-berri klu-berri commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • A tool_config entry in cache_control_injection_points was sent to Anthropic as is
  • Anthropic answered 400 cache_control_injection_points: Extra inputs are not permitted on /v1/chat/completions
  • Native /v1/messages dropped the point silently, so tools went out with no breakpoint
  • OpenAI got the same point in extra_body and answered 400 Unrecognized request argument supplied

How it solves it:

  • The Anthropic chat transform puts the point's control on the last tool and never forwards the key
  • The native /v1/messages handler does the same for its tools list
  • Tool-search tools are skipped and a client marked tool keeps its own breakpoint
  • Bedrock Invoke, Vertex and Azure AI Anthropic share the chat transform, so they get the same fix
  • cache_control_injection_points is kept out of the OpenAI SDK extra_body, OpenAI has no tool mark to turn it into
  • One integration test file drives a live proxy against scripted Anthropic, Bedrock, OpenAI and Gemini upstreams, four direct cases plus three /v1/messages bridge cases

This absorbs the tool marking from #42751 by @tan-pressingly-net (credited as co-author), adds the /v1/messages path and moves the helper to common_utils so both routes share it

User Flow

Before: a developer who configured cache_control_injection_points: [{location: tool_config}] on an Anthropic deployment gets a 400 on every chat request with tools

  1. They send POST https://litellm-domain/v1/chat/completions with "model": "claude-sonnet-4-6" and two tools
  2. The response is 400 AnthropicException - cache_control_injection_points: Extra inputs are not permitted
  3. They send the same tools to POST https://litellm-domain/v1/messages, get 200, but usage.cache_creation_input_tokens is 0 on every call

After: the same config caches the tool definitions on both routes

  1. They send the same POST https://litellm-domain/v1/chat/completions with two tools
  2. The response is 200 and usage.cache_creation_input_tokens is 5555, the second identical call shows cache_read_input_tokens: 5555
  3. POST https://litellm-domain/v1/messages with the same tools returns cache_read_input_tokens: 5555 as well

Relevant issues

Supersedes #42751

Linear ticket

Resolves LIT-9880

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/unit/<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
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

Live proxy at localhost:4001 (Before, main) and localhost:4000 (After, this branch), both started with python litellm/proxy/proxy_cli.py --config config.yaml --port <port> and real Anthropic spend. The new unit tests fail on the Before commit and pass on the After commit. tests/integration/providers/test_tool_config_cache_point_wire.py (7 cases) passes locally with INTEGRATION_WORKERS=2; CircleCI first runs it after merge

Shared setup

model_list:
  - model_name: claude-sonnet-4-6
    litellm_params:
      model: anthropic/claude-sonnet-4-6
      api_key: os.environ/ANTHROPIC_API_KEY
      cache_control_injection_points:
        - location: tool_config
          control:
            type: ephemeral
            ttl: 5m

chat.json carries two OpenAI-shaped function tools with 11 KB descriptions (about 5.5k tokens, above Anthropic's cache minimum) and one user message; messages.json is the same request in Anthropic shape

Before (2e427c6)

/v1/chat/completions on OpenAI (openai/gpt-4.1, same tool_config point, no control)

  1. curl -s localhost:4001/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @chat_openai.json
  2. Observed
{"error": {"message": "litellm.BadRequestError: OpenAIException - Unrecognized request argument supplied: cache_control_injection_points ...", "type": "invalid_request_error", "code": "400"}}

/v1/chat/completions

  1. curl -s localhost:4001/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @chat.json
  2. Observed
{"error": {"message": "litellm.BadRequestError: AnthropicException - {\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"cache_control_injection_points: Extra inputs are not permitted\"},\"request_id\":\"req_011CgCBZRpScZAF7KAj16cge\"} ...", "type": "invalid_request_error", "code": "400"}}

/v1/messages

  1. curl -s localhost:4001/v1/messages -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @messages.json
  2. Observed usage (200, nothing cached)
"usage": {"input_tokens": 5884, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0, "output_tokens": 11}

After (b4831b6)

/v1/chat/completions on OpenAI (openai/gpt-4.1, same tool_config point, no control)

  1. curl -s localhost:4000/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @chat_openai.json
  2. Observed usage (200, OpenAI caches on its own, no mark to send)
"usage": {"prompt_tokens": 4858, "completion_tokens": 8, "prompt_tokens_details": {"cached_tokens": 0}}

/v1/chat/completions

  1. for n in 1 2; do curl -s localhost:4000/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @chat.json; done
  2. Observed usage, request 1 (200, cache written)
"usage": {"prompt_tokens": 5884, "completion_tokens": 11, "cache_creation_input_tokens": 5555, "cache_read_input_tokens": 0, "prompt_tokens_details": {"cached_tokens": 0, "cache_creation_tokens": 5555, "cache_creation_token_details": {"ephemeral_5m_input_tokens": 5555, "ephemeral_1h_input_tokens": 0}}}
  1. Observed usage, request 2 (200, cache read)
"usage": {"prompt_tokens": 5884, "completion_tokens": 11, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 5555, "prompt_tokens_details": {"cached_tokens": 5555, "cache_creation_tokens": 0}}

/v1/messages

  1. curl -s localhost:4000/v1/messages -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @messages.json
  2. Observed usage (200, reads the prefix the chat route just wrote)
"usage": {"input_tokens": 329, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 5555, "output_tokens": 11}

Type

🐛 Bug Fix

Caveats (if any)

Low

  • /v1/messages to a model without a native Anthropic path hands the point to litellm.completion or litellm.responses, the same as a direct call, so no code change there. The bridge cases in the integration file lock that in, the OpenAI Responses and Gemini bodies carry no cache_control_injection_points and Bedrock Nova gets the cachePoint on toolConfig
  • The Anthropic chat and /v1/messages proofs above were captured at 68479e7, the OpenAI proof at the tip, the Anthropic code did not change in between
  • One tool_config point marks one tool; extra tool_config points are ignored

Link to Devin session: https://app.devin.ai/sessions/6d3fba1a729e474ea96d5ffd5b7d3a78
Open in Devin Desktop: https://app.devin.ai/desktop/session/6d3fba1a729e474ea96d5ffd5b7d3a78?variant=devin
Requested by: @kerrylu-berri


Note

Medium Risk
Touches request shaping on high-traffic Anthropic chat and /v1/messages paths and prompt-caching behavior, but scope is narrow and heavily tested.

Overview
Fixes LIT-9880 so deployment cache_control_injection_points with location: tool_config is applied correctly instead of leaking to upstream APIs.

Anthropic chat and native /v1/messages now run shared logic (tools_marked_for_a_tool_config_point): strip cache_control_injection_points, put the point’s cache_control on the last cacheable tool (skip tool-search types), leave an existing client tool mark alone, and record gateway injection via AnthropicCacheControlHook. The same chat transform path covers Bedrock Invoke / Vertex / Azure Anthropic.

OpenAI SDK routing treats cache_control_injection_points as LiteLLM-only (LITELLM_ONLY_PARAMS) so it is not copied into extra_body.

Also generalizes list validation (validated_object_list) and adds unit plus gateway integration tests for Anthropic, Bedrock, OpenAI, and Gemini wire shapes.

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

devin-ai-integration Bot and others added 2 commits October 19, 2026 19:51
…1/messages

A cache_control_injection_points entry with location tool_config was spread into
the Anthropic request body on /v1/chat/completions, which Anthropic rejected with
400 Extra inputs are not permitted, and the native /v1/messages path dropped the
point silently. Both paths now put the point's control on the last tool Anthropic
caches and never forward the key.

Co-authored-by: Tan Nguyen <tan@pressingly.net>
Keeps the lint gates flat: the helper takes untyped tools and points and
narrows them with the existing object validators, the chat transform builds
the body with the marked tools instead of writing into optional_params, and
the handler reads a typed view of kwargs.
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@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.

@codspeed

codspeed Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 32 untouched benchmarks


Comparing litellm_anthropic_tool_config_cache_point (d2399c8) with main (f4347d4)

Open in CodSpeed

@codecov

codecov Bot commented Oct 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…egration test

The cache hook hands a tool_config point on for the provider step, and for
providers that call through the OpenAI SDK every non-OpenAI param rides in
extra_body, so OpenAI answered 400 Unrecognized request argument supplied.
The new integration file drives a live proxy against scripted Anthropic,
Bedrock Converse and OpenAI upstreams for /v1/chat/completions and
/v1/messages.
@klu-berri

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@kerry-berri

kerry-berri commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

litellm-stampbot: risk high

Not approved at d2399c8. risk is high, a human reviewer is needed.

Rules (stampbot/risk_rules.yml):

  • low: no rule flagged it

Cursor Bugbot: Medium Risk at d2399c8. Touches request shaping on high-traffic Anthropic chat and /v1/messages paths and prompt-caching behavior, but scope is narrow and heavily tested. Bugbot run requested, finished in 2 min.
Bugbot Low is low, Medium and High are high. The judge model was not called.

Pushes do not re-run this check. Comment @litellm-stampbot again to re-check the new head.

@kerry-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.

Stale Bugbot comment from a previous run.

@kerry-berri kerry-berri added the risk:high Sensitive, broad, or high-risk change label Oct 19, 2026
…ages

Three new cases on /v1/messages for models without a native Anthropic
messages path: OpenAI through the Responses bridge and Gemini through the
chat bridge never see cache_control_injection_points in the body, and
Bedrock Nova through the chat bridge gets the cachePoint on toolConfig
@devin-ai-integration

Copy link
Copy Markdown
Contributor

@litellm-stampbot new head d2399c8 adds three bridged /v1/messages integration cases, no production code changed since b4831b6

@klu-berri

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@kerry-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 d2399c8. Configure here.

@klu-berri
klu-berri enabled auto-merge (squash) October 19, 2026 23:24
@klu-berri
klu-berri merged commit 93b79d2 into main Oct 19, 2026
92 of 93 checks passed
@klu-berri
klu-berri deleted the litellm_anthropic_tool_config_cache_point branch October 19, 2026 23:26
klu-berri added a commit that referenced this pull request Oct 20, 2026
…s to the downstream proxy (#47570)

PR #47394 withheld cache_control_injection_points from the OpenAI SDK extra_body for
every provider on that path, which also covers litellm_proxy. The downstream LiteLLM
proxy applies the point itself, so litellm_proxy keeps forwarding it while OpenAI and the
other OpenAI-compatible providers still never receive it

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
pamanseau pushed a commit to KubeTEE-AI/litellm that referenced this pull request Oct 25, 2026
…1/messages (BerriAI#47394)

Backport of BerriAI#47394 to stable/1.103.x

Cherry-picked from 93b79d2 (main)
Cartofante pushed a commit to CartoDB/litellm that referenced this pull request Oct 26, 2026
…1/messages (BerriAI#47394)

* fix(anthropic): apply tool_config cache-control points on chat and /v1/messages

A cache_control_injection_points entry with location tool_config was spread into
the Anthropic request body on /v1/chat/completions, which Anthropic rejected with
400 Extra inputs are not permitted, and the native /v1/messages path dropped the
point silently. Both paths now put the point's control on the last tool Anthropic
caches and never forward the key.

Co-authored-by: Tan Nguyen <tan@pressingly.net>

* refactor(anthropic): validate tool_config inputs instead of casting them

Keeps the lint gates flat: the helper takes untyped tools and points and
narrows them with the existing object validators, the chat transform builds
the body with the marked tools instead of writing into optional_params, and
the handler reads a typed view of kwargs.

* fix(openai): keep tool_config cache points out of extra_body, add integration test

The cache hook hands a tool_config point on for the provider step, and for
providers that call through the OpenAI SDK every non-OpenAI param rides in
extra_body, so OpenAI answered 400 Unrecognized request argument supplied.
The new integration file drives a live proxy against scripted Anthropic,
Bedrock Converse and OpenAI upstreams for /v1/chat/completions and
/v1/messages.

* test(integration): cover tool_config cache points on bridged /v1/messages

Three new cases on /v1/messages for models without a native Anthropic
messages path: OpenAI through the Responses bridge and Gemini through the
chat bridge never see cache_control_injection_points in the body, and
Bedrock Nova through the chat bridge gets the cachePoint on toolConfig

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Tan Nguyen <tan@pressingly.net>

Backport of BerriAI#47394 to stable/1.106.x
Cherry-picked from 93b79d2 (main)
Cartofante pushed a commit to CartoDB/litellm that referenced this pull request Oct 26, 2026
…s to the downstream proxy (BerriAI#47570)

PR BerriAI#47394 withheld cache_control_injection_points from the OpenAI SDK extra_body for
every provider on that path, which also covers litellm_proxy. The downstream LiteLLM
proxy applies the point itself, so litellm_proxy keeps forwarding it while OpenAI and the
other OpenAI-compatible providers still never receive it

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>

Backport of BerriAI#47570 to stable/1.106.x
Cherry-picked from 04e9ed4 (main)
yuneng-berri pushed a commit that referenced this pull request Oct 28, 2026
…1/messages (#47394)

* fix(anthropic): apply tool_config cache-control points on chat and /v1/messages

A cache_control_injection_points entry with location tool_config was spread into
the Anthropic request body on /v1/chat/completions, which Anthropic rejected with
400 Extra inputs are not permitted, and the native /v1/messages path dropped the
point silently. Both paths now put the point's control on the last tool Anthropic
caches and never forward the key.

Co-authored-by: Tan Nguyen <tan@pressingly.net>

* refactor(anthropic): validate tool_config inputs instead of casting them

Keeps the lint gates flat: the helper takes untyped tools and points and
narrows them with the existing object validators, the chat transform builds
the body with the marked tools instead of writing into optional_params, and
the handler reads a typed view of kwargs.

* fix(openai): keep tool_config cache points out of extra_body, add integration test

The cache hook hands a tool_config point on for the provider step, and for
providers that call through the OpenAI SDK every non-OpenAI param rides in
extra_body, so OpenAI answered 400 Unrecognized request argument supplied.
The new integration file drives a live proxy against scripted Anthropic,
Bedrock Converse and OpenAI upstreams for /v1/chat/completions and
/v1/messages.

* test(integration): cover tool_config cache points on bridged /v1/messages

Three new cases on /v1/messages for models without a native Anthropic
messages path: OpenAI through the Responses bridge and Gemini through the
chat bridge never see cache_control_injection_points in the body, and
Bedrock Nova through the chat bridge gets the cachePoint on toolConfig

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Tan Nguyen <tan@pressingly.net>
(cherry picked from commit 93b79d2)
yuneng-berri pushed a commit that referenced this pull request Oct 28, 2026
…s to the downstream proxy (#47570)

PR #47394 withheld cache_control_injection_points from the OpenAI SDK extra_body for
every provider on that path, which also covers litellm_proxy. The downstream LiteLLM
proxy applies the point itself, so litellm_proxy keeps forwarding it while OpenAI and the
other OpenAI-compatible providers still never receive it

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 04e9ed4)
haowang1013 pushed a commit to directivegames/litellm that referenced this pull request Nov 3, 2026
…1/messages (BerriAI#47394)

* fix(anthropic): apply tool_config cache-control points on chat and /v1/messages

A cache_control_injection_points entry with location tool_config was spread into
the Anthropic request body on /v1/chat/completions, which Anthropic rejected with
400 Extra inputs are not permitted, and the native /v1/messages path dropped the
point silently. Both paths now put the point's control on the last tool Anthropic
caches and never forward the key.

Co-authored-by: Tan Nguyen <tan@pressingly.net>

* refactor(anthropic): validate tool_config inputs instead of casting them

Keeps the lint gates flat: the helper takes untyped tools and points and
narrows them with the existing object validators, the chat transform builds
the body with the marked tools instead of writing into optional_params, and
the handler reads a typed view of kwargs.

* fix(openai): keep tool_config cache points out of extra_body, add integration test

The cache hook hands a tool_config point on for the provider step, and for
providers that call through the OpenAI SDK every non-OpenAI param rides in
extra_body, so OpenAI answered 400 Unrecognized request argument supplied.
The new integration file drives a live proxy against scripted Anthropic,
Bedrock Converse and OpenAI upstreams for /v1/chat/completions and
/v1/messages.

* test(integration): cover tool_config cache points on bridged /v1/messages

Three new cases on /v1/messages for models without a native Anthropic
messages path: OpenAI through the Responses bridge and Gemini through the
chat bridge never see cache_control_injection_points in the body, and
Bedrock Nova through the chat bridge gets the cachePoint on toolConfig

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Tan Nguyen <tan@pressingly.net>

Backport of BerriAI#47394 to stable/1.105.x.
Cherry-picked from 93b79d2 (main).
haowang1013 pushed a commit to directivegames/litellm that referenced this pull request Nov 3, 2026
…s to the downstream proxy (BerriAI#47570)

PR BerriAI#47394 withheld cache_control_injection_points from the OpenAI SDK extra_body for
every provider on that path, which also covers litellm_proxy. The downstream LiteLLM
proxy applies the point itself, so litellm_proxy keeps forwarding it while OpenAI and the
other OpenAI-compatible providers still never receive it

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>

Backport of BerriAI#47570 to stable/1.105.x.
Cherry-picked from 04e9ed4 (main).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:high Sensitive, broad, or high-risk change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants