An agent that looks up the same thing twice pays twice: twice the tokens, twice the time, and twice the load on the tool. A cache fixes that by storing the result of a call under its arguments, and returning the stored result when the same call comes again. CrewAI enables caching for tool usage by default, which shows the shape to copy: the cache key is the tool name plus its arguments, and the cached result is used only while it is still fresh. Set an expiry on each cached result, because a price or a status that was true an hour ago may not be true now.
What you need
- A tool the agent calls more than once with the same arguments, such as a price or status lookup
- A cache keyed by tool name plus arguments, with an expiry per result
- One task that repeats the same lookup, to test the cache on
Step 1: Key the cache by tool name plus arguments
Store each tool result under the tool’s name and the exact arguments it was called with. A cache keyed only by tool name will return the wrong result for a different order.
Step 2: Set an expiry on every cached result
Give each cached result a lifetime that fits the data: a price may last an hour, an order status only minutes. A cached result with no expiry is a stale result waiting to be served.
Step 3: Test by running the repeated lookup twice
Run the task that repeats the lookup and confirm the second call returns the cached result without calling the tool, and that a call after expiry fetches fresh data. If the second call still hits the tool, the cache key is wrong.
Worked example: synthetic tool cache check
The table below is a synthetic example. It shows the expected result of caching; no tool was called twice.
| Call | Cache hit? | Tool called? |
|---|---|---|
| lookup_order A123, first | No | Yes |
| lookup_order A123, second | Yes | No |
| lookup_order B456 | No | Yes |
| Expired entry refetched | Yes | Pass |
Expected result (UNTESTED). the repeated lookup returns the cached result without calling the tool, a different order fetches fresh, and expired entries refetch. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- A cached result can go stale, so the expiry must fit how fast the data changes.
- Caching a write result is dangerous: a cached write hides whether the write happened.
- A cache key that misses an argument will serve one order’s result for another.
- The cache saves tokens and time, not correctness: a wrong first result is cached wrong too.
