liveBook · Manning

Caching

As explained in AI Agents in Action, Second Edition

Caching is the practice of storing reusable data or computation results so later access can avoid repeating retrieval or computation.

Caching stores results so repeated requests can return them without repeating the original retrieval or computation. For agent tools, caching is safest when calls are idempotent: the same inputs produce the same result, whether called once or repeatedly. A weather lookup is idempotent; sending an email is not. section 8.3.4

Common caching opportunities in agent systems include:

  • Cache idempotent tool-call results so retries and replays can return prior results without repeating side effects. section 8.3.4
  • Cache repeated research queries, but clear the cache regularly to reduce the risk of stale search results. section 11.4
  • Consider prompt, response, and embedding caches for repeated work; plan how each cache will be invalidated because stale entries can produce stale behavior. section 8.3.8
Listing 8.6 06_idempotent_key_example.py
from fastapi import FastAPI
from pydantic import BaseModel
import hashlib, json, asyncio

app = FastAPI(title="Idempotent-by-Inputs Tool Proxy")

class ToolIn(BaseModel):
    name: str
    args: dict

CACHE: dict[str, dict] = {}  # demo only

def cache_key_from_inputs(name: str, args: dict) -> str:  #1
    """
    Build a deterministic key directly from function inputs.
    Canonicalize JSON to ensure stable ordering of dict keys.
    """
    canon = json.dumps({"name": name, "args": args},
                       sort_keys=True, separators=(",", ":"), ensure_ascii=False)
    return hashlib.sha256(canon.encode("utf-8")).hexdigest()

@app.post("/tool")
async def call_tool(body: ToolIn):  #2
    key = cache_key_from_inputs(body.name, body.args)  #3

    if key in CACHE:
        return {"cached": True, "result": CACHE[key], "key": key}  #4

    # Fake tool execution (replace with real call)
    await asyncio.sleep(1.0)  #5
    result = {"ok": True, "tool": body.name, "echo": body.args}

    CACHE[key] = result  #6
    return {"cached": False, "result": result, "key": key}

See an example that builds a deterministic key from tool inputs, checks a cache for that key, and returns a cached result when available. section 8.3.4

Repeated research queries

A research agent can reuse cached results for queries it has already run, reducing repeated work. Regular cache clearing helps limit stale results. section 11.4

How AI Agents in Action, Second Edition covers it

How deep it goes
Explains idempotency, gives an input-keyed cache example, and flags invalidation; it does not detail cache infrastructure.
Examples
Contrasts weather lookups with email sending and demonstrates cached tool results and repeated research queries.
What you can do after
Apply caching to idempotent tool calls and repeated queries, and clear caches to limit stale results.
How it connects
Connects caching with idempotency, retries, event replay, prompt caching, response caching, and embedding caching.

Also explained in

Compare how 38 books explain caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.