Lesson 4 of 4 Cost Optimization for GenAI

Caching Prompts and Responses to Reduce Cost

This lesson covers caching at several levels, prompt prefixes inside Bedrock, complete responses for identical or similar requests and content at the edge, and the trade-offs of each.

Learning outcomes

After completing this lesson, you will be able to:

  • Use prompt caching in Amazon Bedrock.
  • Cache exact matches with request hashing.
  • Use semantic and edge caching.

Prompt caching in Amazon Bedrock

Prompt caching stores the model's processed state for a prompt prefix. The application marks the end of the reusable prefix, such as a long system prompt, tool definitions or a document, with a cachePoint block in the Converse request. The first request writes the cache; later requests with an identical prefix within the cache's time to live (a few minutes, refreshed on each hit) read it, and cached input tokens are billed at a substantially reduced rate and processed faster, lowering latency. Supported models, minimum prefix lengths and the number of checkpoints vary by model. The response's usage shows cache read and write tokens.

r = brt.converse(
    modelId="us.amazon.nova-pro-v1:0",
    system=[{"text": LONG_POLICY_MANUAL}, {"cachePoint": {"type": "default"}}],   # reusable prefix ends here
    messages=[{"role": "user", "content": [{"text": question}]}],
)
print(r["usage"].get("cacheReadInputTokens"), r["usage"].get("cacheWriteInputTokens"))

Prompt caching helps only when the prefix is identical, so variable content (the user's question, retrieved chunks that change per request) must come after the cache point.

The following diagram shows two requests that share a cached prefix.

How prompt caching works. A cachePoint block marks the end of a reusable prefix, such as a long manual, tool definitions or a system prompt. The first request writes the cache; later requests with an identical prefix within the time to live read it at a reduced rate and with lower latency. Variable content, such as the question, comes after the cache point.
Supported models, minimum prefix lengths and checkpoints vary by model.
Scenario questions

When prompt caching pays off

Exam skills 4.1.4 · 4.2.1

Decide when prompt caching helps and how to confirm that it works.

Log in to try this activity Interactive activities are free with an account.

Exact-match caching with request hashing

Exact-match caching stores complete responses keyed by a deterministic hash of the normalised request: model ID, prompt version, inference parameters and the normalised input (trimmed, lowercased where safe). A request whose hash is found returns the stored response without invoking the model. Amazon ElastiCache (Valkey or Redis OSS) gives sub-millisecond lookups; DynamoDB with TTL is a durable alternative. Result fingerprinting hashes outputs to detect duplicates and to deduplicate stored results. Exact caching suits deterministic tasks (temperature 0) with repeated inputs, such as classifying identical product titles.

Select all that apply

Building the cache key

Exam skills 4.1.4

Choose what belongs in the hash key of an exact-match response cache.

Log in to try this activity Interactive activities are free with an account.

Semantic caching and edge caching

Semantic caching handles paraphrases: the question is embedded, a vector search finds previously answered questions above a similarity threshold, and the stored answer is returned. It can be built with vector search in ElastiCache or OpenSearch. The threshold trades hit rate against the risk of returning an answer to a subtly different question, so it is tuned on real queries, and personalised or time-sensitive answers are excluded.

Edge caching with Amazon CloudFront serves non-personalized generated content, such as product descriptions or FAQ answers exposed through GET requests, from edge locations close to users; Lambda@Edge or CloudFront Functions can normalise cache keys. All caches need invalidation tied to data freshness: expire entries when source documents, prompts or models change. The following table compares the strategies.

StrategyReusesBest for
Prompt cachingProcessed prompt prefix inside BedrockLong, repeated system prompts, tools or documents
Exact-match cache (hash key)Complete responses for identical requestsDeterministic, repetitive requests
Semantic cacheResponses for similar questionsFAQ-like traffic with paraphrases
Edge cache (CloudFront)Responses at edge locationsNon-personalized content for many users
Pre-computationAnswers generated in advancePredictable queries

The following diagram shows two caches, a semantic cache and prompt caching, working together for a product assistant.

Two caches for a product assistant. A semantic cache answers paraphrases of the 200 common questions, about 40% of traffic, without calling the model. The remaining requests use prompt caching with a cache point after the 8,000-token manual excerpt that every request includes, which lowers their cost and latency.
Personalised or time-sensitive answers are excluded from the semantic cache.
Sort into groups

Which cache fits?

Exam skills 4.1.4

Choose the caching strategy that fits each traffic pattern.

Log in to try this activity Interactive activities are free with an account.
Worked example

Repeated questions

A public product assistant receives thousands of daily questions; analysis shows that 40% are paraphrases of 200 common questions, and every request includes the same 8,000-token product manual excerpt.

A semantic cache answers paraphrased common questions without invoking the model, and prompt caching with a cache point after the manual excerpt lowers the cost and latency of the remaining calls. Cache entries expire when the manual is updated.

Check yourself

With prompt caching, where should variable content go relative to the cachePoint?

Show answer

After it. The identical prefix before the cachePoint is cached, and anything that changes must follow it.

Exam Tips

Remember for the exam

  • Long, identical prompt prefix: prompt caching with cachePoint; variable content after it.
  • Identical requests: exact cache keyed by a hash of model, prompt version, parameters and input.
  • Paraphrased questions: semantic cache with a tuned similarity threshold.
  • Invalidate caches when sources, prompts or models change.