Having established the importance of memory for agents to maintain context, especially in ongoing dialogues, we now turn to a practical method for its implementation. The most direct technique for enabling short-term memory is to maintain a chronological record of the interaction history.
The simplest way to think about short-term memory is like keeping a transcript of the conversation. Each time the user provides input and the agent responds, this exchange is recorded. In most programming languages, a list (often called an array in other contexts) serves as an elementary data structure for this purpose. A data structure is simply a way of organizing and storing data. In this case, our list will store pairs, with each pair comprising the user's input and the agent's corresponding response.
For instance, if a user asks, "What is the weather like in London?" and the agent, after consulting its LLM, replies, "It's currently cloudy with a chance of rain," our memory list would store this interaction.
Here's how you might represent this in Python:
# A Python list to store conversation history
# Each element in the list is a dictionary representing one turn
conversation_history = []
# Example of the first turn
user_input_turn1 = "What is the weather like in London?"
agent_response_turn1 = "It's currently cloudy with a chance of rain."
# Add this exchange to our history list
conversation_history.append({
"user_query": user_input_turn1,
"agent_answer": agent_response_turn1
})
# Example of a second turn
user_input_turn2 = "What about Paris?"
agent_response_turn2 = "It's sunny in Paris."
# Add the second exchange
conversation_history.append({
"user_query": user_input_turn2,
"agent_answer": agent_response_turn2
})
# At this point, conversation_history would look like this:
# [
# {"user_query": "What is the weather like in London?", "agent_answer": "It's currently cloudy with a chance of rain."},
# {"user_query": "What about Paris?", "agent_answer": "It's sunny in Paris."}
# ]
In this Python snippet, conversation_history
is a list of dictionaries. Each dictionary neatly stores one user query and the agent's answer to it.
So, how does the agent make use of this conversation_history
? The core idea is to include this history as part of the information, the prompt, that is sent to the Large Language Model (LLM) for any subsequent interaction.
This process generally follows these steps:
conversation_history
. It then formats this history, along with the new user input, into a single, comprehensive prompt. It is important to clearly distinguish between user messages and agent messages in this formatted prompt so the LLM understands the conversational flow.
For example, using our weather scenario, if the user now asks "What about Paris?", the prompt sent to the LLM might be constructed like this:
User: What is the weather like in London?
Agent: It's currently cloudy with a chance of rain.
User: What about Paris?
Agent:
By providing the previous exchange, the LLM can infer that "What about Paris?" is also a question about the weather, maintaining the context of the conversation.conversation_history
list.This cycle, using memory to inform the LLM and then updating memory with the latest interaction, is fundamental to how agents maintain conversational context.
The following diagram illustrates how this list-based short-term memory integrates into the agent's operational loop.
The agent combines new user input with the existing conversation history to form a prompt for the LLM. The LLM's response and the initial user input (forming the current turn) are then added back to the history.
Let's trace this with a simple question-answering agent.
Initial State: conversation_history = []
(Our memory is empty)
Turn 1:
User: Who wrote the play 'Hamlet'?
Agent:
conversation_history
now contains:
[{"user_query": "Who wrote the play 'Hamlet'?", "agent_answer": "The play 'Hamlet' was written by William Shakespeare."}]
Turn 2:
User: Who wrote the play 'Hamlet'?
Agent: The play 'Hamlet' was written by William Shakespeare.
User: When was he born?
Agent:
conversation_history
is now:
[
{"user_query": "Who wrote the play 'Hamlet'?", "agent_answer": "The play 'Hamlet' was written by William Shakespeare."},
{"user_query": "When was he born?", "agent_answer": "William Shakespeare was born in April 1564."}
]
As you can see, this list-based memory allows the agent to understand that "he" in the second question refers to William Shakespeare, thanks to the context provided from the first turn.
The primary benefit of using a list for short-term memory is its simplicity. It's relatively easy to implement, understand, and debug. For many applications, especially those involving shorter conversations or interactions where only recent context is needed, this method proves quite effective. It allows the agent to produce coherent and contextually relevant responses without requiring complex database systems or sophisticated memory management techniques.
While this simple list-based memory is a great starting point, it's not without its limitations, especially as conversations grow very long or when more nuanced forms of memory are needed. We will discuss these boundaries and potential challenges in the next section.
Was this section helpful?
© 2025 ApX Machine Learning