AI Agents · How-to
How to Give Your AI Agent Memory
Agents are stateless — they only remember what you resend. Add memory with in-context history plus a JSON file for durable facts.
You'll need: A working agent loop.
Steps
- Keep the full message history in-context for the current task (bounded by the context window).
- For durable facts, add a save_memory tool that writes to a JSON file on disk.
- Load relevant memories back into context at the start of each run.
- For large document sets, move to a vector store (RAG) instead of stuffing everything in.
- Log every iteration so you can see what the agent actually remembered and did.
Watch outDon't let memory grow unbounded — summarize or retrieve selectively, or you'll blow the context window on long tasks.
Go deeper: Deploy Your First AI Agent
The AI guide covers the memory ladder: in-context, external file, vector store, and KV — and when to use each.
Get the guide