liveBook · Manning

Caching

As explained in Building LLM Applications with DSPy

Caching is the practice of storing a language model provider's response so repeated identical requests can be served without querying the provider.

Caching stores a language model’s response so repeated identical requests can be served quickly without querying the provider again, saving time and money. In DSPy, caching is enabled by default; disable it when checking response consistency or execution time. section 2.4.4

Listing 2.10 Disabling the DSPy cache mechanism
dspy.configure_cache(  #1
    enable_disk_cache=False,
    enable_memory_cache=False
)
lm = dspy.LM('openai/gpt-4o', cache=True, temperature=0.99)  #2
dspy.configure(lm=lm)
prog = dspy.Predict("question -> answer")
print(prog(question="Compose a 5-line poem about space"))  #3
print(prog(question="Compose a 5-line poem about space"))  #4

DSPy caching requires both global caching and the LM’s cache setting to be enabled. This example disables both globally, then calls the same program twice. section 2.4.4

How Building LLM Applications with DSPy covers it

How deep it goes
Describes response reuse and LM-level and global controls without detailing cache internals.
Examples
Shows DSPy generating a poem twice from the same question after global caching is disabled.
What you can do after
Apply caching controls in DSPy by setting cache options on an LM and globally.
How it connects
Connects caching to language-model calls, prompt consistency, and execution-time testing.

Also explained in

Compare how 38 books explain caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.