liveBook · Manning

GPTCache

As explained in Building Reliable AI Systems

GPTCache is a library that intercepts requests to GPT-based endpoints and caches exact or semantically similar queries.

GPTCache wraps requests to GPT-based endpoints and reuses answers for exact or semantically similar prompts. A sufficiently similar cached request returns its stored answer; a miss calls the model and caches the result. This can reduce LLM calls, cost, and response time for repeated questions. section 9.2.3

import gptcache                                       #1
from gptcache import cache                            #2
from gptcache.embedding import Onnx as DefaultEmbedding   #3
from gptcache.adapter import openai as gptcache_openai  #4

# 1. Initialize GPTCache
cache.init_embeddings(DefaultEmbedding())             #5
cache.set_similarity_threshold(0.75)                  #6

# 2. Wrap LLM calls
def get_cached_answer_gptcache(prompt):              #7
    response = gptcache_openai.ChatCompletion.create(
        model="gpt-5.4-mini",             #8
        messages=[{"role": "user", "content": prompt}]  #9
    )
    return response["choices"][0]["message"]["content"]   #10

# 3. Use this in your code
answer = get_cached_answer_gptcache("When do you open?")   #11
print(answer)                                         #12

The Python example initializes ONNX embeddings, sets a similarity threshold of 0.75, and routes a ChatCompletion request through GPTCache’s OpenAI adapter. section 9.2.3

Choose an embedding function, similarity threshold, and local or remote storage. A close match can serve an existing answer; a poor match should reach the model or prompt for clarification. A cache hit saves a call, but similarity alone does not establish that the stored answer is appropriate. section 9.2.3

How Building Reliable AI Systems covers it

How deep it goes
Shows initialization, threshold configuration, request wrapping, and hit-or-miss behavior without developing cache internals.
Examples
Uses store-hours questions and a Python ChatCompletion wrapper to illustrate a semantically similar lookup.
What it assumes
Assumes familiarity with Python and chat API calls while showing the cache configuration explicitly.
What you can do after
Apply it via GPTCache to cache GPT-based requests using configured embeddings and a similarity threshold.
How it connects
Connects GPTCache to semantic caching, embeddings, similarity thresholds, and persistent storage.

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.