Concept Beginner · 3 min read

What is short term memory in AI agents

Quick answer
Short term memory in AI agents is a temporary storage of recent interactions or context that the agent uses to maintain coherent conversations and make decisions. It differs from long term memory by focusing only on immediate, short-lived information relevant to the current task or dialogue.
Short term memory in AI agents is a temporary context storage that holds recent information to support coherent interaction and decision-making.

How it works

Short term memory in AI agents acts like a scratchpad that holds recent inputs, outputs, and relevant context during an ongoing interaction. Imagine a detective taking quick notes during an interview to remember key facts before writing a full report later. This memory is volatile and resets or updates frequently as the conversation or task progresses, enabling the agent to respond consistently without needing to reprocess all prior data.

Concrete example

Here is a simple Python example simulating short term memory in an AI chat agent using a list to store recent messages:

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

class ShortTermMemoryAgent:
    def __init__(self, max_memory=3):
        self.memory = []
        self.max_memory = max_memory

    def remember(self, message):
        self.memory.append(message)
        if len(self.memory) > self.max_memory:
            self.memory.pop(0)  # Remove oldest memory

    def chat(self, user_input):
        self.remember(f"User: {user_input}")
        prompt = "\n".join(self.memory) + "\nAgent:"
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}]
        )
        answer = response.choices[0].message.content
        self.remember(f"Agent: {answer}")
        return answer

agent = ShortTermMemoryAgent()
print(agent.chat("Hello, who won the world series in 2020?"))
print(agent.chat("And who was the MVP?"))
output
User: Hello, who won the world series in 2020?
Agent: The Los Angeles Dodgers won the World Series in 2020.
User: And who was the MVP?
Agent: Corey Seager was the MVP of the 2020 World Series.

When to use it

Use short term memory in AI agents when you need to maintain context over a short conversation or task session, such as chatbots, virtual assistants, or interactive agents. It is not suitable for storing persistent knowledge or facts that must survive across sessions; that requires long term memory or external databases. Short term memory improves coherence and relevance in dynamic interactions.

Key terms

TermDefinition
Short term memoryTemporary storage of recent context or interactions in an AI agent.
Long term memoryPersistent storage of knowledge or data across sessions or tasks.
AI agentAn autonomous system that perceives and acts in an environment to achieve goals.
Context windowThe amount of recent text or data an LLM can consider at once.

Key Takeaways

  • Short term memory holds recent context to keep AI agent responses coherent and relevant.
  • It is volatile and limited in size, unlike long term memory which stores persistent knowledge.
  • Implement short term memory as a rolling buffer of recent interactions for chatbots and assistants.
Verified 2026-04 · gpt-4o
Verify ↗