Skip to content

feat(bedrock): support cache_control_injection_points for tool_config location#24076

Merged
Chesars merged 2 commits intoBerriAI:litellm_oss_staging_03_19_2026from
Chesars:feat/cache-control-tool-config-21969
Mar 21, 2026
Merged

feat(bedrock): support cache_control_injection_points for tool_config location#24076
Chesars merged 2 commits intoBerriAI:litellm_oss_staging_03_19_2026from
Chesars:feat/cache-control-tool-config-21969

Conversation

@Chesars
Copy link
Copy Markdown
Collaborator

@Chesars Chesars commented Mar 19, 2026

Relevant issues

Fixes #21969

Pre-Submission checklist

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Type

🆕 New Feature

Changes

Add support for {"location": "tool_config"} in cache_control_injection_points, which appends a cachePoint block to the Bedrock Converse toolConfig.tools array. This enables prompt caching of tool definitions on Bedrock Claude models, reducing cost and latency for tool-heavy agentic workloads.

Also update the cache control hook to pass through non-message injection points to provider-specific handling instead of silently dropping them.

… location

Add support for {"location": "tool_config"} in cache_control_injection_points,
which appends a cachePoint block to the Bedrock Converse toolConfig.tools array.
This enables prompt caching of tool definitions on Bedrock Claude models.

Also update the cache control hook to pass through non-message injection points
to provider-specific handling instead of silently dropping them.

Fixes BerriAI#21969
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Mar 21, 2026 2:30am

Request Review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 19, 2026

Greptile Summary

This PR adds support for {"location": "tool_config"} in cache_control_injection_points, enabling Bedrock Converse prompt caching of tool definitions by appending a {"cachePoint": {"type": "default"}} block to toolConfig.tools. It also changes the AnthropicCacheControlHook to forward non-message injection points to downstream provider-specific handling, rather than silently dropping them.

Key changes:

  • litellm/types/integrations/anthropic_cache_control_hook.py: Adds CacheControlToolConfigInjectionPoint TypedDict and widens CacheControlInjectionPoint from a plain alias to a Union type.
  • litellm/integrations/anthropic_cache_control_hook.py: Separates injection-point processing — message locations are consumed by the hook, while all others are written back into non_default_params for provider-specific handling.
  • litellm/llms/bedrock/chat/converse_transformation.py: Inside _transform_request_helper (shared by both sync and async paths), pops cache_control_injection_points from additional_request_params before it can leak into additionalModelRequestFields, then appends the cachePoint block when a tool_config location is found and tools are present.
  • Three new unit tests validate the expected behaviour (cachePoint appended, no-op when no tools, no-op without a tool_config point).

Architectural note: Because the hook now forwards any non-message injection point unconditionally, providers other than Bedrock will receive cache_control_injection_points in additional_request_params; those providers have no handler for it today (flagged in a prior review thread).

Confidence Score: 4/5

  • Safe to merge for Bedrock users; minor risk that non-Bedrock providers receive unhandled injection points in their request bodies.
  • The Bedrock-side implementation is correct and well-tested: cache_control_injection_points is properly popped before it can leak into additionalModelRequestFields, both sync and async paths share _transform_request_helper so neither is missed, and all three new tests are mock-only unit tests consistent with the repo rule. The hook-side change is intentional and necessary to route the new location type to Bedrock. The one point of risk — that the hook now forwards unknown locations to every provider — was identified in a prior review thread and the author is aware of it. No regressions to existing message-level caching behaviour are introduced.
  • litellm/integrations/anthropic_cache_control_hook.py — the passthrough logic at line 76 will forward any unrecognised location values to all non-Bedrock providers without filtering.

Important Files Changed

Filename Overview
litellm/types/integrations/anthropic_cache_control_hook.py Adds CacheControlToolConfigInjectionPoint TypedDict for Bedrock tool_config caching and expands CacheControlInjectionPoint from a simple alias to a Union type. Clean and correct type definitions.
litellm/integrations/anthropic_cache_control_hook.py Splits injection-point processing so that non-message points are written back into non_default_params for downstream provider-specific handling. Previously they were silently dropped. The forwarding-to-all-providers concern was flagged in a prior review thread.
litellm/llms/bedrock/chat/converse_transformation.py Adds cache_control_injection_points handling in _transform_request_helper (shared by both sync and async paths). Correctly pops the key from additional_request_params before it can leak into additionalModelRequestFields, then appends {"cachePoint": {"type": "default"}} to bedrock_tools when a tool_config location is found. Unrecognised locations are silently discarded (flagged in a prior review thread).
tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py Three new unit tests cover: (1) cachePoint appended when tool_config injection point is present, (2) no toolConfig when no tools are provided, and (3) no cachePoint when injection points don't include tool_config. All tests call _transform_request directly with no real network calls, consistent with the repo's mock-only rule for this folder.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["User calls litellm.completion() with\ncache_control_injection_points"] --> B["AnthropicCacheControlHook\nget_chat_completion_prompt"]
    B --> C{For each injection point}
    C -->|location == 'message'| D["_process_message_injection\nApply cache_control to messages"]
    C -->|any other location| E["Add to remaining_points"]
    D --> F{More points?}
    F -->|yes| C
    F -->|no| G{remaining_points\nnon-empty?}
    E --> F
    G -->|yes| H["Write remaining_points back\ninto non_default_params\n(cache_control_injection_points)"]
    G -->|no| I["non_default_params unchanged"]
    H --> J["AmazonConverseConfig\n_transform_request_helper"]
    I --> J
    J --> K["_prepare_request_params\ncache_control_injection_points →\nadditional_request_params"]
    K --> L["Pop cache_control_injection_points\nfrom additional_request_params"]
    L --> M{len bedrock_tools > 0\nAND cache_injection_points?}
    M -->|yes| N{Any point with\nlocation == 'tool_config'?}
    M -->|no| O["Discard injection points\n(no tools to cache)"]
    N -->|yes| P["Append cachePoint block\nto bedrock_tools list\nbreak"]
    N -->|no| Q["Silently discard\nunrecognised locations"]
    P --> R["Build ToolConfigBlock\nwith cachePoint at end\nof tools array"]
    R --> S["Bedrock Converse API request\nwith toolConfig.tools including cachePoint"]
    O --> T["Bedrock Converse API request\nwithout toolConfig"]
Loading

Last reviewed commit: "fix(types): add Cach..."

Comment thread litellm/integrations/anthropic_cache_control_hook.py
Comment thread litellm/llms/bedrock/chat/converse_transformation.py
@Chesars Chesars changed the base branch from litellm_oss_staging_03_18_2026 to litellm_oss_staging_03_19_2026 March 21, 2026 02:20
@Chesars Chesars merged commit a3095f4 into BerriAI:litellm_oss_staging_03_19_2026 Mar 21, 2026
4 of 5 checks passed
@Chesars Chesars deleted the feat/cache-control-tool-config-21969 branch March 21, 2026 02:29
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.

1 participant