AWS Bedrock Prompt Caching Solves AI Agent Costs and Latency Simultaneously

AWS has unveiled a prompt caching pattern for using OpenAI models on Amazon Bedrock. It addresses the 'Groundhog Day' problem, where agents must resend the entire context at every turn, through a combination of explicit and implicit caching. Static tools and guidelines are handled with explicit caching, while the growing conversation history is managed with implicit caching, reducing both costs and latency.

The 'Groundhog Day' Problem of AI Agents

In the movie <Groundhog Day>, Bill Murray repeats the same February 2nd for 34 years. Since the achievements of the previous day do not carry over, he must start anew every day. Currently, AI agents find themselves in a similar situation. Every time an agent progresses to the next step (tool call, reading results, making the next decision), the model must be resent the entire story up to that point. Tool definitions, guidelines, and all previous tool calls and results are input tokens for every request. Since the model cannot remember its internal state between requests, by the 20th turn, it has to read turns 1 through 19 again, and developers pay that token cost each time.

How Prompt Caching Works: Prefill and KV Cache

The first operation the model performs when reading a prompt is prefill. During the prefill stage, all input tokens are processed to generate key-value (KV) pairs, which become the model's working state. This process must be completed before the first output token is produced. The prompt caching service saves this KV state and, if subsequent requests start with the same input tokens, it loads the saved state to skip the prefill operation. It then processes only the newly added parts.

Combination Strategy of Explicit and Implicit Caching

Bedrock offers two caching modes that can be used simultaneously.

CategoryExplicit Caching (Explicit)Implicit Caching (Implicit)
Cache Boundary SpecificationDeveloper directly marks with `prompt_cache_breakpoint`Bedrock automatically determines based on conversation growth
Main UseTools and guidelines shared between sessions and sub-agentsIncreasing conversation history within the same session
Reuse ScopeCan be shared across multiple sessions with `prompt_cache_key`Limited to the current conversation session

Explicit caching is suitable for static content (tool lists, developer guidelines). A reusable prefix must be 1,024 tokens or more to be eligible for caching. Implicit caching automatically sets the cache boundary after the most recent eligible message as the conversation lengthens. By combining the two methods, a 'common starting point' can be fixed with explicit caching, while the 'growing history' is managed automatically by implicit caching.

Static Content Caching: Reusing Tools and Guidelines

The following code pattern is an example of fixing tools and guidelines with explicit caching.

```python

# Place variable elements (date, username, request ID) after the explicit cache boundary

messages = [

{

Verified figures

Our take

Bedrock's dual caching strategy structurally resolves the bottleneck of repetitive context transmission that has been a cost issue for agent operations. The pattern of hierarchically applying explicit and implicit caching is likely to be standardized in other clouds in the future.

Open questions

Source

Back to catalog