Lesson 3 of 6·12 min read

Memory and State Management

An agent without memory is like an employee who forgets everything every morning. Memory systems give agents the ability to learn from past interactions, maintain context across sessions, and improve over time.

The Four Memory Types

1. Short-Term Memory (Conversation Context)

The current conversation — messages from the running session. Limited by the LLM's context window.

Implementation:

  • Message history in the prompt
  • Sliding window (last N messages)
  • LLM-summarized older messages

2. Long-Term Memory (Persistent Knowledge)

Information stored across sessions: user preferences, learned facts, project status.

Implementation:

  • Key-value store (Redis, Supabase)
  • Structured storage (PostgreSQL)
  • Automatic extraction of relevant facts after each session

3. Episodic Memory (Experiences)

Memories of past situations and their outcomes. "Last time, strategy X worked."

Implementation:

  • Vector database with past agent runs
  • Similarity search over previous situations
  • Learning from feedback (successful vs. failed actions)

4. Vector Memory (Semantic Knowledge)

Similar to RAG — the agent has access to a knowledge base searched via embeddings.

Implementation:

  • Vector database (pgvector, Pinecone)
  • Automatic ingestion of new information
  • Contextual retrieval based on current task

State Management Patterns

Conversation State

interface AgentState {
  messages: Message[]          // Chat history
  currentGoal: string          // Active goal
  plan: Step[]                 // Planned steps
  completedSteps: Step[]       // Completed steps
  context: Record<string, unknown>  // Collected context
}

Checkpointing

Save agent state after every step. On errors, you can roll back to the last valid state — instead of starting from scratch.

State Machines

For structured workflows (e.g., onboarding agent): Defined states with allowed transitions.

Choosing a Memory Strategy

Use CaseRecommended MemoryExample
Simple ChatbotShort-term onlyFAQ bot
Personal AssistantShort + long-termCalendar agent
Research AgentShort + vector + episodicMarket analysis agent
Workflow AgentShort + state machineOnboarding agent

Practical tip: Start with short-term memory. Add long-term memory when users interact across multiple sessions. Episodic memory is only worthwhile when the agent solves complex, recurring tasks.