Repository navigation
fix(anthropic): apply tool_config cache-control points on chat and /v1/messages - #47394
Conversation
…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.
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
Merging this PR will not alter performance
|
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.
|
@litellm-stampbot |
litellm-stampbot: risk highNot approved at Rules (
Cursor Bugbot: Medium Risk at Pushes do not re-run this check. Comment |
|
bugbot run |
…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
|
@litellm-stampbot |
|
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 d2399c8. Configure here.
…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>
…1/messages (BerriAI#47394) Backport of BerriAI#47394 to stable/1.103.x Cherry-picked from 93b79d2 (main)
…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)
…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)
…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)
…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)
…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).
…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).
TLDR
Problem this solves:
tool_configentry incache_control_injection_pointswas sent to Anthropic as iscache_control_injection_points: Extra inputs are not permittedon/v1/chat/completions/v1/messagesdropped the point silently, so tools went out with no breakpointextra_bodyand answered 400Unrecognized request argument suppliedHow it solves it:
controlon the last tool and never forwards the key/v1/messageshandler does the same for itstoolslistcache_control_injection_pointsis kept out of the OpenAI SDKextra_body, OpenAI has no tool mark to turn it into/v1/messagesbridge casesThis absorbs the tool marking from #42751 by @tan-pressingly-net (credited as co-author), adds the
/v1/messagespath and moves the helper tocommon_utilsso both routes share itUser 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"model": "claude-sonnet-4-6"and twotoolsAnthropicException - cache_control_injection_points: Extra inputs are not permittedusage.cache_creation_input_tokensis 0 on every callAfter: the same config caches the tool definitions on both routes
toolsusage.cache_creation_input_tokensis 5555, the second identical call showscache_read_input_tokens: 5555cache_read_input_tokens: 5555as wellRelevant issues
Supersedes #42751
Linear ticket
Resolves LIT-9880
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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 moreScreenshots / Proof of Fix
Live proxy at
localhost:4001(Before, main) andlocalhost:4000(After, this branch), both started withpython 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 withINTEGRATION_WORKERS=2; CircleCI first runs it after mergeShared setup
chat.jsoncarries two OpenAI-shaped function tools with 11 KB descriptions (about 5.5k tokens, above Anthropic's cache minimum) and one user message;messages.jsonis the same request in Anthropic shapeBefore (2e427c6)
/v1/chat/completions on OpenAI (
openai/gpt-4.1, sametool_configpoint, nocontrol)curl -s localhost:4001/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @chat_openai.json/v1/chat/completions
curl -s localhost:4001/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @chat.json/v1/messages
curl -s localhost:4001/v1/messages -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @messages.jsonAfter (b4831b6)
/v1/chat/completions on OpenAI (
openai/gpt-4.1, sametool_configpoint, nocontrol)curl -s localhost:4000/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @chat_openai.json/v1/chat/completions
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/v1/messages
curl -s localhost:4000/v1/messages -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d @messages.jsonType
🐛 Bug Fix
Caveats (if any)
Low
/v1/messagesto a model without a native Anthropic path hands the point tolitellm.completionorlitellm.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 nocache_control_injection_pointsand Bedrock Nova gets thecachePointontoolConfigtool_configpoint marks one tool; extratool_configpoints are ignoredLink 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_pointswithlocation: tool_configis applied correctly instead of leaking to upstream APIs.Anthropic chat and native
/v1/messagesnow run shared logic (tools_marked_for_a_tool_config_point): stripcache_control_injection_points, put the point’scache_controlon the last cacheable tool (skip tool-search types), leave an existing client tool mark alone, and record gateway injection viaAnthropicCacheControlHook. The same chat transform path covers Bedrock Invoke / Vertex / Azure Anthropic.OpenAI SDK routing treats
cache_control_injection_pointsas LiteLLM-only (LITELLM_ONLY_PARAMS) so it is not copied intoextra_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.