How AI Agents Remember: A Simple Guide to Memory, Vector Search, and RAG A guide on AI agent memory explains that large language models retain nothing between requests, so the surrounding agent must manage a limited context window — roughly 4,000 to 8,000 tokens for older models, up to 272,000 tokens for GPT-5 and Claude 3.7 Sonnet, and up to 1 million tokens for Gemini 2.5. The guide recommends a rolling context window that keeps the newest messages and drops the oldest when space runs out, and includes a LangGraph/LangChain code example using Ollama's llama3.2 that summarizes once state holds more than 6 messages while keeping the last 2. It notes the approach is easy to build but discards older information even when important. An AI chatbot may seem to remember earlier messages, but the language model LLM does not remember them on its own. It uses the information sent to it with each request. The agent — the system around the model — keeps track of useful information. It sends the model the details it needs for each task. Here’s why this matters: 1. The model has limited space. It can read only a certain amount of text at once. This limit is called the context window. Long conversations, files, and search results can fill it up. Older information may then be left out, even if it is important. 2. More text can cost more and take longer. Sending the whole conversation each time adds extra work. Sending only useful details can save time and money. 3. Too much unrelated information can hurt answers. Important details can get lost in the clutter. Clear, focused input helps the model answer better. 4. Long tasks need progress tracking. The agent needs to know what is finished, what is next, and what has been decided. Otherwise, it may repeat work or ignore earlier decisions. 5. The model may lack needed information. Private data and recent information may not be part of its training. Search tools can find these details when needed. 6. Users need control over memory. Memory management helps decide what to save, forget, or avoid storing. It can also let users view and delete saved information. Two simple ways to manage memory are a rolling context window and keyword search. A rolling window keeps recent messages. Keyword search finds saved information by matching words. What is a context window? It is the space available for the information sent to the model in one request. Think of it as short-term memory for that request. What is context length? It is the maximum amount of text the model can handle at once, measured in tokens. What is a token? A token is a small piece of text. In English, it is roughly three-quarters of a word, or about four characters. · 1,000 tokens ≈ 750 words How big are context windows? · Older models: about 4,000 tokens ~3,000 words, ~12 pages or 8,000 tokens ~6,000 words, ~24 pages · GPT-5 and Claude 3.7 Sonnet: up to 272,000 tokens · Gemini 2.5: up to 1 million tokens Why it matters The context window has limited space. Include what the model needs and leave out unrelated details. The simplest approach Send the current question and the conversation so far. When there is not enough space, keep the newest messages. Rolling context window · Send the conversation history that fits in the window with each request. · When the window fills up, remove the oldest messages to make room for new ones. Pros · Easy to build · Simple · Works for many use cases Cons · Older information is removed even when it is important. · Long questions and answers can fill the window quickly. python import argparseimport osimport refrom langchain core.messages import AIMessage, AnyMessage, HumanMessage, RemoveMessage, SystemMessage, from langchain core.runnables import RunnableLambdafrom langgraph.checkpoint.memory import InMemorySaverfrom langgraph.graph import END, START, MessagesState, StateGraphMAX MESSAGES = 6 summarize once state holds more than this many messagesKEEP LAST = 2 how many recent messages survive a summarization passOLLAMA MODEL = "llama3.2" also try gpt-oss:20b, qwen:4b, deepseek-r1:1.5bOLLAMA URL = os.environ.get "OLLAMA HOST", "http://localhost:11434" class State MessagesState : summary: strdef strip thinking msg: AIMessage - AIMessage: """Reasoning models deepseek-r1, ... may inline