Short-Term Memory Best Practices
Ten practices for keeping session state correct, cheap, and isolated while multi-turn agents stay inside the context window.
Use this list when you add chat memory, Redis sessions, scratchpads, or summarization to an agent.
How to Use This Checklist
- Work top to bottom; isolation and budgets before clever compression.
- Tick items that are true in production paths, not only local demos.
- Re-run after new channels, longer tool traces, or multi-worker deploys.
- Pair each gap with an owner and a verification test.
A - Scope and Isolation
- 1. Key every store by tenant, user, and session. Composite keys plus authorize-on-read/write beat UUID-only session lookup.
- 2. Split history, scratchpad, and tool cache. Three concerns, three lifecycles - even if one Redis document holds all three fields.
- 3. Prefer runtime-owned progress for facts. Mark steps done from tool success; let the model propose notes, not invent completion.
B - Context Budget
- 4. Reserve tokens for the reply and tools. Measure full input after RAG and tool schemas; never fill 100% of the window with history.
- 5. Protect policy, goal, and recent tail. Trim or summarize the middle first; keep system rules immutable.
- 6. Trim tool chains atomically. Drop complete call+result groups together so APIs do not reject broken transcripts.
C - Compression and Storage
- 7. Summarize on a threshold with labeled summaries. Use a cheap model, low temperature, and structured sections (goal, ids, decisions, open questions).
- 8. Cap payload size and set TTLs. Bound serialized sessions; expire abandoned chats; store huge blobs by reference.
- 9. Keep a cold log separate from the hot prompt view. Debug and compliance need more than what the model saw last turn.
D - Operations
- 10. Test memory like security and cost features. Multi-turn retention tests, cross-tenant deny tests, and cost-per-turn dashboards on history size.
Applying the Habits in Order
| Stage | Habits | Exit criterion |
|---|---|---|
| Design | 1-3 | Scoped keys; state diagram for history/pad/cache |
| Implement | 4-6 | Budget function + atomic trim in the turn path |
| Harden | 7-9 | Summarize + TTL + cold/hot split live |
| Operate | 10 | Automated isolation and retention tests in CI |
Quick Anti-Patterns
- Global in-memory
messages = []on a multi-tenant server. - Re-sending entire PDF tool outputs every turn.
- Summarizing without tags so summaries look like user claims.
- Tool cache keyed only by query string across all customers.
- Infinite Redis TTL "so we never lose chats."
- Trusting the model to remember after you dropped the only copy of a constraint.
Minimal Reference Snippet
# Pattern: scoped load → budget → model → save with TTL
# key = f"agent:stm:{tenant}:{user}:{session}"
# sess = load(key) or new_session()
# sess["messages"] = trim_or_summarize(sess["messages"], budget=...)
# resp = model.complete(project(sess)) # history + compact scratchpad
# append_turn(sess, user, resp); save(key, sess, ttl=SESSION_TTL)FAQs
Which three habits are non-negotiable?
Isolation keys (1), input budget with reserves (4), and tests for cross-tenant deny plus retention (10).
Do larger context models retire this checklist?
No. They reduce how often you summarize, not the need for isolation, TTLs, or cost control.
Should every agent use Redis?
Only when multiple workers, restarts, or shared sessions require it. Single-process prototypes can start in memory with the same keying discipline.
When is summarization better than truncation?
When early turns hold goals, constraints, or ids you still need. Truncate pure chit-chat.
How does this relate to long-term memory?
Short-term habits keep one session healthy. Promote durable facts deliberately; do not treat TTL session blobs as a CRM.
Where should scratchpad fields be documented?
Next to the agent skill or graph definition, versioned with the code that validates them.
What metric shows history is too fat?
Tokens of history per turn, p95 latency, and cost per successful task rising while task success stays flat.
Can frameworks replace these practices?
Frameworks supply checkpoints and helpers. You still own key scope, budgets, retention, and tests.
Related
Related: Short-Term Memory Basics
Related: Managing Conversation History Within a Context Budget
Related: Redis for Fast Short-Term Agent State
Related: Session Isolation
Related: Summarization Strategies for Long-Running Conversations
Stack versions: Pins from the category manifest (verify at build): OpenRouter (~315+ models, July 2026 pricing/fees); LangGraph 1.0+; CrewAI 1.14+; Microsoft Agent Framework 1.0; Vercel AI SDK 6; Pydantic AI (latest); LlamaIndex (latest); OpenAI Agents SDK (latest + MCP); MCP (Linux Foundation governance); A2A (HTTP+SSE+JSON-RPC 2.0); Solana
@solana/web3.js+@solana/spl-token.