Caching
Cache tool results to avoid redundant executions with ToolCache.
Cacheable Tools
Mark a tool as cacheable to deduplicate identical calls:
const lookupTool = defineTool({
name: 'lookup',
description: 'Look up product info',
input: z.object({ sku: z.string() }),
output: z.object({ name: z.string(), price: z.number() }),
execute: async ({ sku }) => {
// This only runs once per unique SKU
return await database.findProduct(sku);
},
cacheable: true,
});When cacheable: true, AgentForge caches the result keyed on the serialized input. If the LLM calls the same tool with the same input again, the cached result is returned without re-executing.
ToolCache
The ToolCache class manages caching:
import { ToolCache } from '@ahzan-agentforge/core';
const cache = new ToolCache();
// Manual usage (framework handles this automatically)
const key = cache.key('lookup', { sku: 'ABC123' });
cache.set(key, { name: 'Widget', price: 9.99 });
const result = cache.get(key); // { name: 'Widget', price: 9.99 }When to Use Caching
Use caching for tools that:
- Make expensive external API calls
- Query databases with deterministic results
- Perform computations with the same inputs
Don't cache tools that:
- Have side effects (sending emails, creating records)
- Return time-sensitive data
- Depend on external state that changes between calls
Next Steps
- Compensating Actions — rollback support
- Serialization — result serialization