Agent memory is information preserved so an agent can use earlier facts, actions, and results while working toward a goal.
Why is it needed?
An agent may perform several steps:
1. Fetch failed orders
2. Find the common error
3. Inspect service logs
4. Create a report
Without memory or state, it may forget what it already checked, repeat tools, or contradict an earlier result.
Short-term memory
Short-term memory contains information needed for the current task.
Examples:
- current goal
- recent messages
- completed steps
- tool results
- temporary plan
- remaining work
{
"goal": "Investigate failed orders",
"completed": ["Fetched 50 failed orders"],
"finding": "38 failures contain INVENTORY_TIMEOUT",
"nextStep": "Inspect inventory logs"
}
It usually expires when the task or session ends.
Long-term memory
Long-term memory stores useful information that may help in future tasks.
Examples:
- user preferences
- stable project facts
- previously approved decisions
- summaries of completed work
{
"userId": 25,
"projectDatabase": "PostgreSQL",
"preferredReportFormat": "short Markdown summary"
}
Long-term memory belongs in external storage such as a database. The model itself is not the permanent storage.
Short-term vs long-term
| Short-term memory | Long-term memory |
| Used for the current task | Used across future tasks |
| Plans and recent tool results | Stable facts and preferences |
| Usually temporary | Stored persistently |
| Often placed directly in context | Retrieved only when relevant |
Memory and context window are different
- Memory storage: information saved outside the model
- Context window: information included in the current model request
The application retrieves relevant memory and places it inside the context window.
Database memory → retrieve useful facts → current context → model
What should be remembered?
Before storing a memory, ask:
- Will it be useful later?
- Is it stable or likely to change?
- Is the user allowed to store it?
- Is it sensitive?
- When should it expire?
Do not turn every message or model guess into permanent truth.
Common problems
Incorrect memory
The agent may infer something incorrectly and save it as a fact. Validate important memories or let the user correct them.
Irrelevant memory
Too many unrelated memories distract the model and waste tokens. Retrieve only what helps the current goal.
Cross-user leakage
Memory must be isolated by user or tenant. One user's facts must never appear in another user's context.
Outdated memory
Facts can change. Store timestamps and update or expire old information.
Practical strategy
- Keep structured task state for the current run.
- Summarize long histories when necessary.
- Store only useful and permitted long-term facts.
- Retrieve memories based on the current goal.
- Allow correction and deletion.
- Never store secrets unnecessarily.
Short-term memory tracks the current job. Long-term memory preserves useful facts for future jobs. Both are managed by the application, not magically remembered by the model.