# Response Caching and Semantic Caching

> Avoid paying twice for the same answer with exact, semantic and intermediate-result caches, while keeping answers fresh and private. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/ai-engineer/llm-caching
Last updated: 2026-11-28

Avoid paying twice for the same answer with exact, semantic and intermediate-result caches, while keeping answers fresh and private.

Intermediate · about 3 hours · difficulty 3/5

Learn first: [Prompt Caching and Batch Processing](https://www.internhack.in/roadmaps/tracks/ai-engineer/prompt-caching-batching), [Text Embeddings](https://www.internhack.in/roadmaps/tracks/ai-engineer/embeddings)

Provider prompt caching speeds up repeated prefixes, but your own **application caches** can skip model calls entirely.

### Layers of caching

- **Exact-match cache**: hash the normalized request (model, prompt version, messages, parameters) and store the response in Redis or a database. Perfect for deterministic tasks such as classification or extraction of the same document.
- **Semantic cache**: embed the user query and return a cached answer when a previous query is similar enough. Useful for FAQ-style traffic, risky for anything personal or time-sensitive.
- **Intermediate caches**: cache embeddings, retrieved results, document summaries and tool results, which are often more reusable than final answers.

### Getting it right

- **Keys must include everything that changes the answer**: prompt version, model, user or tenant id when content is personalized, and relevant document versions.
- **Invalidation**: set time-to-live values and clear entries when source documents change.
- **Thresholds** for semantic caches need tuning with evals; too loose and users get answers to a different question.
- **Privacy**: never serve one user's cached personal answer to another user.

### Measuring value

Track hit rate, cost saved and any quality impact. A cache that saves 5% of calls but causes wrong answers is a net loss.

### Good enough to move on

You can add an exact-match cache with correct keys and TTLs to an AI endpoint, explain when a semantic cache would be safe, and report its hit rate and savings.

## Key concepts

- **Exact-match response cache**
- **Semantic caching**
- **Caching embeddings and retrieval**
- **Cache keys and versioning**
- **TTLs and invalidation**
- **Tenant isolation**

## Mini project

Add a Redis cache to a document summarization endpoint keyed on document hash, model and prompt version, invalidate on document change, and log hit rate and cost saved over a day of simulated traffic.

## Check yourself

What must be part of the cache key for a support bot that answers questions about each user's own orders?

## Free resources

- [Redis documentation](https://redis.io/docs/latest/) (Redis)
- [Anthropic docs: prompt caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) (Anthropic)

Previous: [Latency Optimization](https://www.internhack.in/roadmaps/tracks/ai-engineer/latency-optimization) · Next: [Model Routing, Gateways and Fallbacks](https://www.internhack.in/roadmaps/tracks/ai-engineer/model-routing-fallbacks)

## More in LLMOps and Production Engineering

- [Deploying LLM-Powered Services](https://www.internhack.in/roadmaps/tracks/ai-engineer/deploying-ai-services)
- [Latency Optimization](https://www.internhack.in/roadmaps/tracks/ai-engineer/latency-optimization)
- [Response Caching and Semantic Caching](https://www.internhack.in/roadmaps/tracks/ai-engineer/llm-caching)
- [Model Routing, Gateways and Fallbacks](https://www.internhack.in/roadmaps/tracks/ai-engineer/model-routing-fallbacks)
- [Serving Open-Weight Models at Scale](https://www.internhack.in/roadmaps/tracks/ai-engineer/serving-open-models)
- [GPU and Hardware Basics for AI Engineers](https://www.internhack.in/roadmaps/tracks/ai-engineer/gpu-basics)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [AI Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/ai-engineer)
