Tool Caching
Agentium supports result caching for tools. When a tool is called with the same arguments within the TTL window, the cached result is returned instantly — no re-execution.Quick Start
getWeather("Mumbai") twice within 60 seconds, the second call returns the cached result without hitting the API.
Configuration
Add acache property to any tool definition:
number
required
Time-to-live in milliseconds. Cached results expire after this duration. Example:
60_000 for 1 minute, 300_000 for 5 minutes.How It Works
- When a tool is called, the
ToolExecutorgenerates a cache key from the tool name and a stable serialization of the arguments. - If a valid (non-expired) cache entry exists for that key, the result is returned immediately.
- If no cache entry exists, the tool executes normally and the result is stored with an expiry timestamp.
Cache Key Strategy
Cache keys are generated astoolName:stableStringify(args) — arguments are sorted by key to ensure consistent hashing regardless of property order.
Clearing the Cache
Tool result caches are scoped to eachToolExecutor instance, which is recreated per request when using toolRouter. For agents without a tool router, the cache persists across runs on the same agent instance.
Cache entries expire automatically based on ttl. To manually clear all cached results, create a fresh agent or call setTools() to rebuild the tool executor.
When to Use
Good Candidates
- External API calls (weather, exchange rates)
- Database lookups with stable results
- Expensive computations
- Rate-limited APIs
Poor Candidates
- Tools with side effects (send email, write file)
- Real-time data that changes every second
- Tools where arguments include timestamps
ToolCacheConfig Type
Logging
WhenlogLevel is "info" or higher, cached tool results are prefixed with [cached] in the logs, making it easy to verify caching behavior during development.