Skip to content
mentoor

Topic 4 of 5 · 5 min read

Caching tool and retrieval results in agents

Goal: Decide which tool and retrieval results in an agent run may be cached, with a lifetime for each.

In an agent, cache the results of tools that read slow-changing data, never the ones that read live state or change something, and give each cached result a lifetime that matches how fast its source changes. The model call is often not the biggest cost in a long agent run; repeated tool calls are.

Check yourself · 1 of 3

Which agent tool call must never be served from a cache?

Choose one answer

Why it matters

An agent loops: think, call a tool, read the result, think again. It often repeats the same call, such as fetching the same file, running the same search or embedding the same text, within one run or across runs. Each repeat costs time, tool fees and tokens when the result is pasted back into the prompt.

How to decide, tool by tool

For each tool ask three questions:

  1. Does it have side effects? Sending an email, creating a ticket, running a payment or writing a file must never be cached or replayed. Cache only reads.
  2. How fast does its source change? Pick a lifetime from the answer: a pricing page edited monthly can live for hours; a stock quote or an inventory count for seconds or not at all.
  3. Does the result depend on who asked? If the tool applies the caller's permissions, put the user or tenant in the key, as with response caches.

Typical choices:

Tool or step Cache? Lifetime
Embedding of a stored document chunk Yes Until the chunk or embedding model changes
Web search for a public query Yes Minutes to hours
Read a file at a given commit Yes Forever, key on the commit
Read a file in a working tree being edited No, or key on file hash -
Database query for current balance No -
Send email, create order Never -

Keys and scope

Key a tool result on the tool name, a version of the tool's code or schema, the normalised arguments and the scope. Two runs for different tenants must not share a retrieval result that was filtered by permissions.

What goes wrong

  • Replaying a side effect. An agent retries a "create invoice" call and a cache returns the earlier success, so the agent believes it happened again. Or the opposite: a retry runs twice. Use idempotency keys for writes, not caches.
  • Stale reads inside a run. The agent edits a file, then reads it back from cache and sees the old text.
  • Cached empty results. A search that failed or returned nothing during an outage is stored, and the agent keeps concluding there is no answer.

Examples

A tool cache policy

read_file:      { cache: true,  ttl: "key on content hash" }
web_search:     { cache: true,  ttl: "30m", scope: "global" }
embed_chunk:    { cache: true,  ttl: "until embedding model changes" }
get_balance:    { cache: false }
send_email:     { cache: false, idempotency_key: "required" }

Each line answers the three questions: side effects, speed of change and scope.

Stale read after a write

An agent reads config.yaml (cached), edits it, then reads it again to verify. The cache returns the old text and the agent reports the edit failed. Fix: any write to a path invalidates that path's entries, or the key includes the file's hash.

Key points

  • Cache reads only; never cache or replay tools that send, create, pay or write.
  • Set a lifetime per tool from how fast its source changes, from forever for a commit-pinned file to none for live balances.
  • Key tool results on tool version, normalised arguments and the caller's scope.
  • Within a run, invalidate a cached read after the agent writes to the same resource.
  • Do not cache failures or empty results caused by outages.

Common mistakes

  • Caching a tool because it is slow without asking whether it has side effects.
  • Using one lifetime for every tool.
  • Leaving the user or tenant out of keys for permission-filtered retrieval.
  • Caching empty or error results from a temporary outage.

Check yourself · 2 of 3

An agent edits a file, then reads it back from a tool cache and sees the old content. What is the right fix?

Choose one answer

Check yourself · 3 of 3

A retrieval tool filters documents by the caller's permissions. What must the cache key include?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic