Lesson 2 of 5 Human Oversight, Caching, Scaling and Middleware

Reducing Cost and Latency with Prompt, Semantic and Response Caching

This lesson distinguishes prompt, semantic and response caching, which reuse the repeated work of multi-agent systems to cut cost and latency, and the risks of each.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how prompt caching reuses a shared prefix.
  • Describe semantic caching and the risk of serving wrong answers.
  • Cache deterministic operations with response caching.

Prompt caching

Prompt caching is performed by the model service. When a request begins with the same token sequence as a recent request, the service reuses the computation for that prefix, which reduces latency and bills cached input tokens at a discount. For Azure OpenAI models, caching applies to prompts of at least 1,024 tokens, in increments after that, and requires an identical prefix; a single changed token early in the prompt prevents reuse. The response usage reports cached tokens. Designing for prompt caching means placing instructions, tool definitions and fixed examples first and variable content last, and keeping them byte-for-byte stable.

The following diagram compares a cache-friendly prompt with a cache-hostile one.

Designing for prompt caching. When a request begins with the same token sequence as a recent one, the model service reuses the computation for that prefix and bills those input tokens at a discount. For Azure OpenAI models the prefix must be at least 1,024 tokens and identical. Instructions, tool definitions and fixed examples go first and stay byte-for-byte stable; variable content, such as the date or the user's question, goes last. A single changed token early in the prompt prevents reuse.
Stable content first, variable content last.
True or false

Prompt caching

Exam skills 2.4.3

Check how prompt caching works for Azure OpenAI models.

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

Semantic caching

Semantic caching stores a response together with the embedding of the request. When a new request's embedding is within a similarity threshold of a cached one, the cached response is returned without calling the model. The API Management AI gateway provides semantic cache lookup and store policies backed by Azure Managed Redis, with an embeddings deployment to vectorize requests. The threshold trades hit rate against the risk of returning an answer to a subtly different question. Semantic caching suits frequently asked, user-independent questions.

The following diagram shows a semantic cache lookup.

Semantic caching in the API Management AI gateway. Each request is embedded, and if a cached request's embedding is within the similarity threshold, the cached response is returned without calling the model. Otherwise the model answers and the response is stored with the request's embedding in Azure Managed Redis. The threshold trades hit rate against the risk of answering a subtly different question; semantic caching suits frequently asked, user-independent questions.
Similar questions reuse one answer.
Scenario questions

Semantic cache decisions

Exam skills 2.4.3

Apply semantic caching to agent scenarios.

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

Response caching

Response caching stores exact results of deterministic operations keyed by their inputs: retrieval queries, tool calls such as currency rates or product details, and embeddings of unchanged text. It uses a TTL that matches how quickly the data changes. The following table compares the three types.

CacheWhat is reusedWhereMain risk
Prompt cachingComputation for an identical prompt prefixModel serviceNone to correctness; only works with stable prefixes
Semantic cachingAnswers to similar requestsAPI Management with Redis, or customWrong answer for a different question; data leakage across users
Response cachingExact tool or query resultsApplication cache, RedisStale data beyond TTL

Caches must respect security boundaries. Answers that depend on the user's permissions, identity or tenant must be keyed by that scope or excluded from shared caches, otherwise one user can receive another's data.

Sort into groups

Which kind of cache?

Exam skills 2.4.3

Choose the cache type for each reuse opportunity.

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

Reducing cost of an FAQ agent

An HR agent answers 50,000 questions a month. About 60% are variants of 30 common policy questions whose answers are the same for everyone. The agent's 4,000-token instructions are identical on every call, but the current date is inserted at the top.

Moving the date to the end of the prompt makes the 4,000-token prefix stable, so prompt caching applies to every call. A semantic cache in API Management serves the common policy questions without model calls. Questions about a person's own leave balance must bypass the shared cache, because their answers depend on the user.

Check yourself

Why must permission-dependent answers never come from a shared cache?

Show answer

One user could receive an answer built from data that only another user may see.

Exam Tips

Remember for the exam

  • Prompt caching needs an identical prefix of at least 1,024 tokens; put variable content last.
  • Semantic caching suits user-independent questions; tune the similarity threshold.
  • Never serve permission-dependent answers from a shared cache.