liveBook · Manning

Semantic caching

As explained in Building Reliable AI Systems

A caching technique that reuses results for queries with similar meanings, rather than requiring exact textual matches.

Semantic caching reuses an LLM response when a new query has a similar meaning to a cached query. Unlike an exact-string cache, it can match differently worded questions. For a busy customer-support service, that can avoid repeated model calls, reducing latency and cost. section 9.2.3

Store-hours questions

“What are your hours?” and “Tell me your operating hours” can retrieve the same stored answer despite their different wording. An exact-string lookup would treat them as separate queries. section 9.2.3

A cache-backed request follows four steps:

  • Convert the query to a vector embedding and search stored query embeddings for a similar match. section 9.2.3
  • If a match passes the configured similarity threshold, return its stored response. section 9.2.3
  • Otherwise, call the LLM and store the new query with its response for later reuse. section 9.2.3
def handle_query(query):              #1
    cached_response = get_cached_response(query)   #2
    if cached_response:
        return cached_response
    response = call_gpt_api(query)     #3
    vector_store.add_texts([query], metadatas=[{"response": response}])   #4
    return response

def call_gpt_api(query):        #5
 #6

print(handle_query("What’s your refund policy?"))   #7

The request handler shows the cache-first path: look up a response, call the model on a miss, then save that response with the query. section 9.2.3

One implementation stores responses as metadata alongside queries in FAISS and uses embeddings for retrieval. Another wraps model calls with GPTCache, an ONNX embedding model, and a 0.75 similarity threshold. The threshold controls which near-matches are reused; similarity alone does not establish that a cached answer is appropriate for every variation of a question. section 9.2.3

In an e-commerce service, caching sits alongside request batching and model routing to handle traffic spikes without spending on repeated FAQ answers. The illustrated project’s mock-response mode bypasses the GPTCache-backed call, while its streaming path uses a separate model call; test the live, non-streaming path when evaluating cache behavior. section 9.2.5

How Building Reliable AI Systems covers it

How deep it goes
Works through embedding storage, similarity lookup, thresholded reuse, and cache-miss handling, with FAISS and GPTCache implementation sketches.
Examples
Uses differently phrased store-hours questions and an e-commerce support service with repeated refund queries.
What it assumes
Assumes familiarity with Python and LLM API calls while introducing the embedding-and-lookup workflow.
What you can do after
Apply it via FAISS or GPTCache to return cached responses on similar-query hits and call the LLM on misses.
How it connects
Connects vector embeddings and vector databases to exact-string caching, request batching, and multimodel fallback.

Also explained in

Compare how 2 books explain semantic caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Reliable AI Systems. The section links point to the parts of the book it draws on.