Sub topic 3
Short-term memory: the conversation buffer
This chapter assumes you know basic Python and have called a chat-style LLM API. It also assumes you know the idea of an agent as a model running in a loop.
What the buffer is
A model call is stateless, so it remembers nothing from the previous call. Short-term memory is the code you write around the model to fix that. It keeps the list of messages exchanged so far and sends the whole list with every call. That list is the conversation buffer.
Each message has a role and some content:
system: instructions that set the agent's behavior.user: what the person said.assistant: what the model replied.tool: results from tool calls, in agents that use tools.
The buffer lives in your program, not in the model. The model "remembers" a turn only because that turn is in the prompt you send.
A minimal buffer
The example below uses a fake model so it runs without an API key. Like a real model, it sees only the messages passed to it.
class ConversationBuffer:
def __init__(self, system_prompt):
self.messages = [{"role": "system", "content": system_prompt}]
def add(self, role, content):
self.messages.append({"role": role, "content": content})
def as_prompt(self):
return list(self.messages)
def fake_llm(messages):
"""Stand-in for a chat API: it only knows what is in `messages`."""
prefix = "My name is "
for m in messages:
if m["role"] == "user" and m["content"].startswith(prefix):
return "Your name is " + m["content"][len(prefix):].rstrip(".") + "."
return "I don't know your name."
def chat(buffer, user_input):
buffer.add("user", user_input)
reply = fake_llm(buffer.as_prompt())
buffer.add("assistant", reply)
return reply
buffer = ConversationBuffer("You are a helpful assistant.")
print(chat(buffer, "My name is Priya."))
print(chat(buffer, "What is my name?"))
fresh = ConversationBuffer("You are a helpful assistant.")
print(chat(fresh, "What is my name?"))This prints:
Your name is Priya.
Your name is Priya.
I don't know your name.The second question works because the first turn is still in the buffer. The fresh buffer has no such turn, so the model cannot answer.
Why the buffer needs limits
The buffer grows by at least two messages per turn, and tool results can be large. Three costs grow with it:
- Context limit. The prompt must fit in the model's context window. Past that limit, the call fails or the provider cuts content.
- Cost and latency. You pay for the full prompt on every call, and longer prompts respond more slowly.
- Quality. Long prompts full of old, irrelevant turns can distract the model.
The next chapter shows how to keep the buffer inside a token budget.
Common errors
- Forgetting to store the reply. If you append the user message but not the assistant reply, the model loses its own side of the conversation.
- Sharing one buffer across users. A module-level list shared by all requests mixes conversations. Create one buffer per session.
- Mutating what you send.
as_promptreturns a copy so that trimming a prompt does not erase the stored history. - Dropping the system message. It carries the agent's instructions, so any later trimming must keep it.
- Treating the buffer as durable. It disappears when the process ends. Facts that must survive across sessions belong in long-term memory.
Exercise
A user tells your agent their name in message 1. In message 6, the agent no longer knows it, although your code appends every message to the buffer. Name two causes you would check first.
Show answer
First, check whether the prompt sent to the model actually contains message 1, because trimming or a bug in building the prompt may have dropped it. Second, check whether you are using the same buffer for every turn, because a new buffer per request would lose all earlier messages. Also confirm the assistant replies are stored.