Skip to content

feat(cache): add stable prefix caching strategy for improved hit rates (#2271)#2272

Open
dinindunz wants to merge 2 commits intostrands-agents:mainfrom
dinindunz:agent-tasks/2271
Open

feat(cache): add stable prefix caching strategy for improved hit rates (#2271)#2272
dinindunz wants to merge 2 commits intostrands-agents:mainfrom
dinindunz:agent-tasks/2271

Conversation

@dinindunz
Copy link
Copy Markdown
Contributor

@dinindunz dinindunz commented May 10, 2026

Description

The current CacheConfig(strategy="auto") in BedrockModel only injects a single cache point on the last user message (the "moving tail"). There is no stable/anchor cache point placed on the first user message to cover the system prompt + initial user prompt + tool descriptions.

This means that whenever the moving tail gets invalidated (e.g., due to context pruning, summarisation, or conversation reset), the entire prompt cache is lost and must be rebuilt from scratch. There is no fallback cache prefix to absorb the cost of the stable portions of the prompt.

Proposed Solution

Modify _inject_cache_point() to inject two cache points instead of one:

Stable prefix: A cache point appended to the first user message's content. This anchors the cache over the system prompt + first user message + tool descriptions - content that rarely changes across turns.

Moving tail: A cache point appended to the last user message's content (current behavior). This advances with the conversation to maximise cache hit rate on recent context.

Usage

from strands.models.bedrock import BedrockModel
from strands.models.model import CacheConfig

model = BedrockModel(
    model_id="us.anthropic.claude-sonnet-4-6-20250514-v1:0",
    cache_config=CacheConfig(strategy="auto", anchor_first_message=True),
)

Related Issues

#2271

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

strands-agents#2271)

Introduces `anchor_first_message` to `CacheConfig` to inject a stable cache point on the first user message. This stable prefix acts as a fallback when the moving tail cache point (on the last user message) is invalidated by context pruning or summarization, enhancing cache resilience and hit rates.
…sage (strands-agents#2271)

Removes the exclusion that prevented the moving tail cache point from being added to the last user message if it was the same as the first user message and `anchor_first_message` was active. This ensures both caching strategies are applied concurrently, improving cache resilience in single-turn conversations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant