---
title: "Cache whole answers for repeated inputs · Agent Wranglers"
url: https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/answer-cache
updated: 2026-10-20
licence: "All rights reserved"
---

# Cache whole answers for repeated inputs

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Optional
- Chapter: [Latency, cost and fallbacks](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks)
- Rule: Cache the full answer for features that give one right answer per input, such as classification or extraction. Key the cache on the prompt version, the model version and a hash of the input.
- How: Only cache answers that do not depend on who asks or on data that changes. Include every input that shapes the answer in the key, so a new prompt or model never serves an old answer. Set a time limit on each entry, such as 7 days. Never cache answers built from one user's private data for another user.
- Check: A test calls the feature twice with the same input. The provider is called once. After the prompt version changes, the provider is called again.

Example:

```ts
const key = createHash('sha256').update(JSON.stringify([prompt.id, prompt.version, config.model, input])).digest('hex');
const cached = await cache.get(key);
if (cached) return JSON.parse(cached);
const answer = await ai.generate({ promptId: prompt.id, vars: { input }, schema: Category, userId });
await cache.set(key, JSON.stringify(answer), { ttlSeconds: 7 * 24 * 3600 });
```

```python
key = hashlib.sha256(json.dumps([prompt.id, prompt.version, settings.model, text]).encode()).hexdigest()
if (cached := cache.get(key)) is not None:
    return Category.model_validate_json(cached)
answer = ai.generate(prompt.id, {"input": text}, Category, user_id)
cache.set(key, answer.model_dump_json(), ex=7 * 24 * 3600)
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers ai features playbook: https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/answer-cache`.

- Previous: [Put the stable part of the prompt first](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/prompt-caching)
- Next: [Price every call and cap the spend](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/cost-per-call)
- [Latency, cost and fallbacks](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks)
