๐ง Memory Handling
Memory handling is how an AI agent stores, retrieves, and manages information across a conversation or task. Since LLMs don't have true memory, we simulate it through careful prompt design โ telling the AI what to remember, how to summarize past context, and when to reference earlier information.
Think of it like taking notes during a long meeting. Without notes, you forget key details. With good notes, you can recall anything when needed.
๐ฏ Why This Mattersโ
LLMs have a limited context window (the amount of text they can process at once). As conversations grow longer or tasks span many steps, important information can get pushed out. Memory handling helps you:
- Keep critical information accessible throughout long interactions
- Prevent the AI from forgetting earlier instructions or decisions
- Summarize past context to fit within the context window
- Build agents that maintain consistent behavior over many exchanges
๐ The Concept in Detailโ
Types of Agent Memoryโ
| Memory Type | What It Stores | Lifespan | Example |
|---|---|---|---|
| Working Memory | Current task context | One interaction | "I'm currently analyzing file X" |
| Short-Term Memory | Recent conversation history | Current session | Last 10 messages |
| Long-Term Memory | Persistent facts and preferences | Across sessions | "User prefers Python over Java" |
| Episodic Memory | Past experiences and outcomes | Across sessions | "Last time we tried X, it failed because Y" |
Memory Management Strategiesโ
1. Key Information Extraction Teach the AI to pull out and store the most important facts:
After each user message, extract and update your memory:
- KEY FACTS: [Important facts mentioned]
- USER PREFERENCES: [Any stated preferences]
- DECISIONS MADE: [Choices that were finalized]
- OPEN QUESTIONS: [Things still unresolved]
2. Memory Summarization When context gets long, compress older information:
Summarize the conversation so far in 3-5 bullet points,
keeping only the most critical information. Drop small talk
and repeated information.
3. Memory Sections Organize memory into clearly labeled sections for easy retrieval.
๐ก Prompt Examplesโ
Working Memory Promptโ
You are a project management agent working on a software project.
Maintain a MEMORY BLOCK at the end of each response:
=== MEMORY ===
Project: [Project name]
Current Phase: [What phase we're in]
Completed Tasks: [List of done tasks]
In Progress: [Current tasks]
Pending Decisions: [Things needing user input]
Key Constraints: [Budget, timeline, tech stack]
==============
Always check your memory block before answering. Update it after
every interaction. If information conflicts with your memory, ask
for clarification.
Conversation Summarization Promptโ
You are a long-running assistant helping me write a novel.
Every 5 messages, create a SUMMARY UPDATE:
STORY SO FAR:
- Plot: [1-2 sentence summary]
- Characters: [Name - key trait - current situation]
- Setting: [Where the story currently takes place]
- Unresolved threads: [Plot points still open]
- Style notes: [Tone, voice, POV preferences]
Use this summary to stay consistent. If I reference something
from earlier, check the summary first. If it's not there,
let me know you may have lost that context.
โ Bad Exampleโ
Let's continue working on the marketing plan we discussed earlier.
What were we talking about before?
Why it fails: The AI has no memory structure to reference. It either guesses what was discussed or admits it doesn't know. There's no system for tracking past context.
โ Improved Exampleโ
You are my marketing strategy assistant. You maintain a persistent
memory throughout our conversation.
CURRENT MEMORY STATE:
- Company: TechFlow (B2B SaaS, project management tool)
- Target audience: Mid-size tech companies, 50-500 employees
- Budget: $50,000 for Q1 campaign
- Decided channels: LinkedIn ads, content marketing, webinars
- Pending: Email campaign strategy, influencer partnerships
- Last session: We finalized the LinkedIn ad copy (3 variants)
MEMORY RULES:
1. Before answering, review the memory state above
2. After each response, output an UPDATED MEMORY section
3. If new information contradicts existing memory, flag it
4. If I refer to something not in memory, ask me to clarify
5. Keep memory concise โ max 10 bullet points
Now, let's work on the email campaign strategy. Suggest 3
approaches that align with our target audience and budget.
Why it works: The memory state is explicitly provided, rules for updating memory are clear, and the AI knows to check memory before responding and update it after.
๐งช Try It Yourselfโ
๐งช Try It Yourself
Edit the prompt and click Run to see the AI response.
๐๏ธ Practice Challengeโ
Design a memory system for an AI coding tutor that tracks:
- The student's skill level (beginner/intermediate/advanced)
- Topics already covered
- Common mistakes the student makes
- The student's learning pace
- Preferred programming language
Write the prompt with a memory block format and rules for when to update each field.
๐ Real-World Scenarioโ
Scenario: You're using an AI agent as a personal health coach over several weeks. The agent needs to remember:
- Your dietary restrictions and allergies
- Exercise routines and progress
- Weekly weight and measurement trends
- Foods you said you dislike
- Your fitness goals and timeline
Without memory handling, the agent would suggest foods you're allergic to or exercises you've already said you can't do. With a structured memory prompt, it maintains a consistent, personalized experience across every interaction.
๐ค Interview Questionโ
Q: How do you handle memory in AI agents when LLMs have limited context windows?
A: Since LLMs have no persistent memory, we simulate it using three strategies: First, structured memory blocks โ explicit sections in the prompt that store key facts, decisions, and preferences, updated after each interaction. Second, summarization โ periodically condensing long conversation history into concise summaries to fit within the context window. Third, key information extraction โ identifying and preserving only the most critical information while dropping redundant details. For production systems, external storage (databases, vector stores) can supplement in-prompt memory for truly long-term recall.
๐ Summaryโ
- LLMs don't have real memory โ you must design memory into your prompts
- Use memory blocks with labeled sections (facts, decisions, preferences, pending items)
- Summarize long conversations periodically to fit within context limits
- Set memory rules โ when to update, what to keep, what to drop
- Different memory types serve different purposes: working, short-term, long-term, episodic
- Always instruct the AI to check memory before responding and update memory after responding
- Good memory handling makes AI agents feel consistent and personalized