DEV Community

Krutika Shah
Krutika Shah

Posted on Edited on

AI Agent Memory: Short-Term vs Long-Term Memory Explained

AI agents can call tools, search databases, plan tasks, and work across multiple steps.

But without memory, every interaction risks starting from zero.

That does not mean an agent should remember everything.

A useful agent needs to know what information belongs to the current task, what should survive into future conversations, and what should eventually disappear.

The simplest distinction is:

Short-term memory
= What matters during this conversation or task?

Long-term memory
= What should remain useful across future sessions?
Enter fullscreen mode Exit fullscreen mode

There is another concept developers often mix up with both:

Context window
= What the model can see right now.
Enter fullscreen mode Exit fullscreen mode

That difference matters.

A model can have a huge context window and still have no durable memory. Likewise, an application can store thousands of memories while exposing only three relevant ones to the model.

Modern agent frameworks reflect this separation. LangChain, for example, treats short-term memory as thread-scoped agent state while long-term memory persists across conversations and can be recalled from separate stores.

Key Takeaway: Agent memory isn't about remembering everything. It's about making the right past information available when it becomes useful again.

What Is AI Agent Memory?

AI agent memory is the mechanism an application uses to retain, organize, retrieve, update, and sometimes forget information from previous interactions or actions.

Think about a coding agent.

You tell it:

Our backend uses FastAPI.
We use PostgreSQL.
Never modify production migrations directly.
Enter fullscreen mode Exit fullscreen mode

Five minutes later, those details may still exist in the current conversation.

Easy.

But what happens next Tuesday?

If the agent starts a fresh session and has no external memory system, those details may no longer be available.

A memory-enabled architecture looks more like this:

User Request
     ↓
Retrieve Relevant Memory
     ↓
Build Context
     ↓
LLM / Agent
     ↓
Perform Action
     ↓
Decide What to Remember
     ↓
Update Memory
Enter fullscreen mode Exit fullscreen mode

The important point is that memory usually lives in the system surrounding the model, not magically inside each model inference.

Research on autonomous LLM agents increasingly frames memory as a process involving selective persistence and recall rather than simple storage. One 2026 survey describes the core lifecycle as a write–manage–read loop connected to agent perception and action.

Context Window vs Agent Memory

This is where beginners usually get tripped up.

A context window and memory are related, but they are not interchangeable.

Concept Context Window Agent Memory
Main purpose Information available to the model now Information preserved for reuse
Lifetime Current model context Can survive future interactions
Location Model input Usually application state or external storage
Capacity Limited by model Depends on storage design
Access Already present Usually selected or retrieved
Main question What can the model see? What should we preserve?

A useful mental model:

Context is what the model sees. Memory helps determine what the model should see next.

This is why memory connects directly to context engineering in modern AI systems.

Imagine your database stores:

User language preference: TypeScript
Preferred cloud: AWS
Project database: PostgreSQL
Favorite pizza: Margherita
Last login: Tuesday at 14:32
Enter fullscreen mode Exit fullscreen mode

If the user asks:

Why is my Prisma migration failing?

You probably want the PostgreSQL and TypeScript information.

Their pizza preference?

Not so much.

The memory system may store information broadly. Context engineering determines which part deserves to enter the model's working context.

What Is Short-Term Memory?

Short-term memory maintains continuity during an ongoing conversation, workflow, or task.

LangChain currently describes it as thread-scoped memory managed as part of agent state. That state can include conversation history and additional application-specific information required while the thread continues.

A support agent might temporarily track:

Customer: Sarah
Order: #4821
Problem: damaged item
Refund requested: yes
Refund status: pending
Enter fullscreen mode Exit fullscreen mode

A coding agent might track:

Current issue:
Authentication tests failing

Files inspected:
auth.ts
middleware.ts
auth.test.ts

Latest finding:
Refresh token expiration mismatch
Enter fullscreen mode Exit fullscreen mode

That information is useful right now.

It doesn't necessarily deserve permanent storage.

Short-Term Memory Is More Than Chat History

A common simplification is:

short-term memory = previous messages
Enter fullscreen mode Exit fullscreen mode

Conversation history is one form of short-term memory, but an agent may also maintain:

  • current task state,
  • intermediate tool results,
  • temporary files,
  • workflow progress,
  • active constraints,
  • structured variables,
  • unresolved actions.

This is why state and context are useful concepts to keep separate.

The application may store substantial thread state while sending only the relevant portion into the next model call.

What Happens When Short-Term Memory Gets Too Large?

Suppose an agent works for several hours.

It performs 50 tool calls.

Reads 20 files.

Receives multiple errors.

Generates intermediate plans.

The obvious implementation is:

Keep everything
      ↓
Send everything again
      ↓
Repeat
Enter fullscreen mode Exit fullscreen mode

That gets expensive quickly.

More importantly, large histories can contain stale or irrelevant information that distracts the model. Current LangChain guidance recommends strategies such as trimming, deleting, or summarizing message history when conversations grow too long.

OpenAI similarly uses compaction for long-running agent loops: key state can be preserved while less useful context is removed as the active context window fills.

Typical strategies include:

Trimming

Keep only the most recent messages.

Messages 1–100
      ↓
Keep 80–100
Enter fullscreen mode Exit fullscreen mode

Summarization

Compress older interactions.

70 previous messages
        ↓
Project summary
        +
Recent messages
Enter fullscreen mode Exit fullscreen mode

Structured State

Instead of relying on raw conversation history:

{
  "current_task": "fix authentication test",
  "suspected_file": "auth.ts",
  "tests_failed": 3
}
Enter fullscreen mode Exit fullscreen mode

Short-term memory should preserve continuity, not every token ever produced.

Key Takeaway: The context window is the model's current workspace. Short-term memory helps maintain the state needed to keep that work coherent.

What Is Long-Term Memory?

Long-term memory preserves useful information across separate conversations or sessions.

LangChain's current implementation, for example, stores long-term memories independently of individual conversation threads and can persist them as structured JSON documents organized by namespace and key.

Imagine this interaction:

Session 1

User:
Our API services are written in TypeScript
and we use PostgreSQL.
Enter fullscreen mode Exit fullscreen mode

The system decides these are stable project facts.

Long-Term Memory

project.language = TypeScript
project.database = PostgreSQL
Enter fullscreen mode Exit fullscreen mode

Two weeks later:

Session 14

User:
Can you suggest an ORM for this service?
Enter fullscreen mode Exit fullscreen mode

The application retrieves:

Language: TypeScript
Database: PostgreSQL
Enter fullscreen mode Exit fullscreen mode

and inserts those memories into the current context.

Now the model can give a more relevant answer without requiring the user to repeat themselves.

That is durable memory.

Three Useful Types of Long-Term Memory

Developers often think about memory only in terms of duration.

Another useful distinction is what kind of information is being remembered.

Current agent-memory frameworks commonly borrow three categories from cognitive science: semantic, episodic, and procedural memory.

1. Semantic Memory: Facts

Facts about users, projects, or the environment.

Project uses PostgreSQL.
User prefers TypeScript.
Production runs on AWS.
Enter fullscreen mode Exit fullscreen mode

These are things the agent should know.

2. Episodic Memory: Experiences

Information about what happened previously.

Last deployment failed because the database
migration executed before the backup completed.
Enter fullscreen mode Exit fullscreen mode

These memories help the agent use previous outcomes when solving similar problems.

3. Procedural Memory: Rules and Procedures

Information describing how something should be done.

Deployment process:
1. Run integration tests
2. Create database backup
3. Apply migrations
4. Deploy application
5. Verify health checks
Enter fullscreen mode Exit fullscreen mode

These are instructions, policies, or learned procedures the agent can reuse.

For a beginner implementation, you don't need a separate database for every memory type.

The taxonomy simply helps answer:

What exactly are we asking the agent to remember?

How Agent Memory Actually Works

Storage is only one step.

A useful memory system needs a lifecycle.

1. Observe

The agent receives new information.

"We migrated this project from MongoDB to PostgreSQL."
Enter fullscreen mode Exit fullscreen mode

2. Decide What Matters

Should this survive future sessions?

A stable database migration probably should.

A temporary message such as:

"I'm grabbing coffee; I'll be back in 10 minutes."
Enter fullscreen mode Exit fullscreen mode

probably shouldn't.

3. Write

Convert useful information into a memory.

{
  "project_database": "PostgreSQL"
}
Enter fullscreen mode Exit fullscreen mode

4. Store

Persist it somewhere appropriate.

5. Retrieve

During a future request, search for memories relevant to the current task.

6. Inject Into Context

Only selected memories are given to the model.

7. Update or Forget

Suppose the user later says:

We migrated from PostgreSQL to CockroachDB.
Enter fullscreen mode Exit fullscreen mode

The old memory should not continue competing with the new one forever.

This final step gets ignored surprisingly often.

A memory system needs to handle:

  • updates,
  • contradictions,
  • expiration,
  • consolidation,
  • deletion.

Otherwise memory eventually becomes historical clutter.

Where Is Long-Term Memory Stored?

There isn't one universal answer.

You might use:

Agent
 │
 ├── SQL database
 ├── Key-value store
 ├── Vector database
 ├── Graph database
 ├── Files
 └── Structured profile store
Enter fullscreen mode Exit fullscreen mode

The storage system should match how the information will be retrieved.

Do You Need a Vector Database?

No.

This is one of the biggest misconceptions around agent memory.

If you know exactly what you need:

user.preferences["language"]
Enter fullscreen mode Exit fullscreen mode

a normal structured database may be simpler.

If you need semantic search:

Find memories related to previous
deployment failures.
Enter fullscreen mode Exit fullscreen mode

vector retrieval becomes more useful.

A vector database is therefore one memory retrieval mechanism.

It is not memory itself.

Memory vs RAG

Memory and Retrieval-Augmented Generation can look similar because both retrieve information and add it to model context.

Their purpose is usually different.

Agent Memory RAG
Often derives from past interactions Usually retrieves external knowledge
Creates continuity Creates grounding
May be user or agent specific Often retrieves shared domain knowledge
Evolves as interactions occur Knowledge may exist independently
Example: user preferences Example: company documentation

Consider:

"What database does this project use?"
Enter fullscreen mode Exit fullscreen mode

Memory might retrieve:

This user's project migrated to PostgreSQL.
Enter fullscreen mode Exit fullscreen mode

RAG might retrieve:

PostgreSQL migration documentation.
Enter fullscreen mode Exit fullscreen mode

Both pieces could enter the same context.

Different origin. Different purpose.

The Biggest Memory Mistake: Saving Everything

A naive architecture sometimes looks like this:

Every Message
     ↓
Embedding
     ↓
Vector Database
     ↓
Retrieve Top 20
     ↓
Prompt
Enter fullscreen mode Exit fullscreen mode

Easy to build.

Hard to maintain.

Soon you get:

  • duplicate memories,
  • outdated preferences,
  • conflicting facts,
  • irrelevant details,
  • unnecessary retrieval,
  • larger prompts,
  • higher latency.

The difficult part of memory engineering isn't storing information.

It's deciding what deserves to survive.

What Should an Agent Remember?

A simple filter helps.

Consider storing information when it is:

  • likely to matter again,
  • reasonably stable,
  • useful for future decisions,
  • difficult to reconstruct automatically,
  • specific enough to retrieve,
  • appropriate to retain.

Avoid automatically persisting:

  • temporary instructions,
  • casual chatter,
  • redundant facts,
  • easily recomputed values,
  • expired workflow state.

A good memory policy needs both remembering and forgetting.

Practical Example: Coding Agent Memory

Here's how I would separate memory for a coding assistant.

CODING AGENT

SHORT-TERM
├── Current bug
├── Current branch
├── Stack trace
├── Files being inspected
├── Recent edits
└── Latest test results


LONG-TERM
├── Tech stack
├── Architecture conventions
├── Preferred libraries
├── Deployment process
├── Historical technical decisions
└── Known project constraints
Enter fullscreen mode Exit fullscreen mode

Suppose today's task is:

Fix the authentication refresh-token bug.

The agent may retrieve long-term memories saying:

Authentication uses JWT.
Refresh tokens are stored server-side.
Tests use PostgreSQL containers.
Enter fullscreen mode Exit fullscreen mode

Then it combines them with short-term information:

Current failure:
refreshToken.test.ts line 84

Expected expiration:
7 days

Actual expiration:
24 hours
Enter fullscreen mode Exit fullscreen mode

The model receives the intersection of past knowledge and current task state.

That is where memory becomes useful.

In production systems, these memory decisions rarely exist in isolation. They usually sit alongside retrieval, model orchestration, tool integration, evaluation, and infrastructure—the same architectural concerns involved in broader AI and ML development when moving an agent from a prototype into a maintainable application.

It also connects naturally to other agent infrastructure. Tools may be exposed through MCP, agents may collaborate using patterns like those covered in MCP vs A2A, while memory determines which previous information should remain available across those interactions.

AI Agent Memory Cheat Sheet

Context window: What can the model see right now?

Short-term memory: What matters during this task or conversation?

Long-term memory: What should survive future sessions?

Semantic memory: What facts should the agent know?

Episodic memory: What previous experiences should it remember?

Procedural memory: What rules or processes should it reuse?

Memory retrieval: Which past information deserves to enter the current context?

The most useful memory system isn't the one that stores the most data.

It's the one that can reliably answer three questions:

What should I remember?

When should I retrieve it?

When should I forget it?
Enter fullscreen mode Exit fullscreen mode

That is the shift developers need to make.

Agent memory is not just persistence.

It is selective persistence plus selective recall.

Key Takeaway: Better agents don't remember everything. They remember less—and make the right memories useful at the right moment.

Top comments (0)