AI Agent Memory Architectures: Short-Term, Episodic, and Vector State in Production

AI Agent Memory Architectures: Short-Term, Episodic, and Vector State in Production

(Updated: ) ๐Ÿ“– 4 min read

An autonomous agent without persistent memory suffers from permanent amnesia: every interaction restarts the universe from scratch, forcing users to repeatedly re-explain their preferences, tech stacks, and organizational constraints.

Conversely, the naive developer workaroundโ€”indefinitely appending every past message and tool execution into the prompt contextโ€”triggers Context Exhaustion. Within a few dozen turns, token consumption balloons exponentially, latency triples, and the agent drowns in obsolete historical tool payloads, losing track of its primary objective.

Building production-ready autonomous systems requires a Hierarchical Memory Architecture: an engineered pipeline that separates ephemeral scratchpad execution from distilled episodic facts and permanent semantic knowledge.

This guide provides a comprehensive blueprint for architecting multi-tiered memory systems in Python.


The Four Tiers of Agent Memory

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                              THE AGENT MEMORY HIERARCHY                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1. Working Memory (Active Execution Context)                                           โ”‚
โ”‚    - Volatile, in-prompt scratchpad for the current reasoning step                     โ”‚
โ”‚    - Current turn messages, immediate tool call inputs/outputs, active execution plan  โ”‚
โ”‚    - Lifespan: Single task trajectory (Cleared upon task completion)                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2. Short-Term Episodic Buffer (Sliding Window + Summary)                               โ”‚
โ”‚    - Rolling conversational history across recent sessions                             โ”‚
โ”‚    - Asynchronously compacted into progressive summaries to save tokens                โ”‚
โ”‚    - Lifespan: Active session / Current day                                            โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 3. Long-Term Semantic Memory (Distilled Entity Facts)                                  โ”‚
โ”‚    - Structured Subject-Predicate-Object triples with bi-temporal timestamps           โ”‚
โ”‚    - Extracted asynchronously via background LLM jobs (preferences, constraints)       โ”‚
โ”‚    - Lifespan: Permanent (Indexed in Relational DB + Vector Embeddings)                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 4. Procedural Memory (Learned Tool Patterns & Rules)                                   โ”‚
โ”‚    - System prompt instructions, few-shot trajectory exemplars, and learned skills      โ”‚
โ”‚    - Guides *how* the agent executes tasks based on past successes and failures        โ”‚
โ”‚    - Lifespan: Static codebase or fine-tuned weights                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

1. Asynchronous Memory Compaction Pipeline

Never store raw conversation transcripts in your long-term database. Instead, process completed sessions through an asynchronous Compaction & Fact Extraction Pipeline:

User Finishes Session
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Background Celery / Temporal Worker                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ LLM Distillation Extractor                             โ”‚
โ”‚ Filters conversational pleasantries ("Hello", "Thanks")โ”‚
โ”‚ Extracts permanent user preferences and facts          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Bi-Temporal Conflict Resolution Engine                 โ”‚
โ”‚ Resolves contradictions against existing memories      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Store in PostgreSQL + pgvector                         โ”‚
โ”‚ (Embeddings generated for semantic retrieval)          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. Structured Bi-Temporal Fact Schema

To prevent hallucinated memories and resolve updates over time (e.g., when a user changes jobs or migrates cloud providers), we structure memories as typed facts with temporal validity:

from datetime import datetime, timezone
from typing import Optional, List, Literal
from pydantic import BaseModel, Field

class MemoryFact(BaseModel):
    entity: str = Field(..., description="The subject of the memory (e.g. 'User', 'Project-Alpha', 'PostgresDB')")
    attribute: str = Field(..., description="The property being described (e.g. 'preferred_cloud', 'primary_language')")
    value: str = Field(..., description="The factual value (e.g. 'AWS us-east-1', 'TypeScript')")
    category: Literal["preference", "technical_constraint", "business_rule", "personal_info"]
    confidence_score: float = Field(..., ge=0.0, le=1.0)
    created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    valid_until: Optional[datetime] = Field(None, description="Set when a newer fact supersedes this fact")

class SessionExtractionPayload(BaseModel):
    session_summary: str = Field(..., description="A concise 2-sentence summary of the completed interaction")
    extracted_facts: List[MemoryFact]
    superseded_fact_ids: List[str] = Field(
        default_factory=list,
        description="IDs of previous memory facts that are contradicted by new information"
    )

3. Python Implementation: The Memory Extraction Engine

Here is the extraction worker using Gemini 2.5 Flash and Pydantic:

import os
import json
from google import genai
from google.genai import types

client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))

EXTRACTION_SYSTEM_PROMPT = """
You are an expert cognitive memory manager for an enterprise AI agent.
Analyze the provided user transcript and extract enduring, actionable facts about the user, their technical environment, and operational constraints.

EXTRACTION GUIDELINES:
1. Ignore ephemeral banter, greetings, and temporary debugging errors.
2. Extract durable facts: tech stacks, infrastructure choices, stylistic preferences, and business invariants.
3. If the user explicitly changes a previously stated constraint (e.g., "We migrated from MySQL to Postgres"), identify the old belief for invalidation.
4. Output strictly structured JSON adhering to the provided schema.
"""

def extract_memories_from_transcript(transcript: str, existing_memories: List[dict]) -> SessionExtractionPayload:
    """Processes a completed conversation and extracts distilled facts."""
    
    prompt = (
        f"EXISTING STORED MEMORIES:\n{json.dumps(existing_memories, indent=2)}\n\n"
        f"COMPLETED SESSION TRANSCRIPT:\n{transcript}\n\n"
        "Extract new durable facts and identify any existing memories that must be marked invalid."
    )

    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=[EXTRACTION_SYSTEM_PROMPT, prompt],
        config=types.GenerateContentConfig(
            response_mime_type="application/json",
            response_schema=SessionExtractionPayload,
            temperature=0.0
        )
    )

    return SessionExtractionPayload.model_validate_json(response.text)

4. Production Memory Retrieval & Dynamic Injection

When the user initiates a new session, the agent does not load 50,000 tokens of past history. Instead, it queries the memory database for the top 5โ€“10 relevant facts using vector cosine similarity and injects them into the system prompt:

class AgentMemoryStore:
    def __init__(self, db_connection):
        self.db = db_connection

    def get_relevant_context(self, user_id: str, current_query: str) -> str:
        """
        Retrieves active, valid memories for this user.
        In production: computes embedding for current_query and executes pgvector similarity search.
        """
        # Simulated database query: SELECT * FROM memories WHERE user_id = ? AND valid_until IS NULL
        active_facts = [
            "- User strictly enforces Python 3.12+ with type hints.",
            "- Production database is PostgreSQL 16 hosted on AWS RDS.",
            "- Deployment pipeline uses Docker containers on AWS ECS Fargate.",
            "- User prefers brief, code-first answers without conversational pleasantries."
        ]
        
        return "\n".join(active_facts)

def build_hydrated_system_prompt(user_id: str, base_system_prompt: str, current_query: str, store: AgentMemoryStore) -> str:
    """Hydrates the agent system prompt with distilled, high-relevance memories."""
    memories = store.get_relevant_context(user_id, current_query)
    
    hydrated_prompt = f"""{base_system_prompt}

## RECALLED USER & SYSTEM CONTEXT:
The following persistent facts have been remembered from previous sessions with this user:
{memories}

Strictly follow these recalled constraints in all generated answers and code.
"""
    return hydrated_prompt

5. Architectural Evaluation: Memory Frameworks vs Custom SQL/Vector

Evaluation Dimension Mem0 Zep Custom PostgreSQL + pgvector
Hosting Model Cloud Managed / Open Source Cloud Managed / Open Source 100% Self-Hosted in your private VPC
Data Privacy & Compliance Third-party SaaS liability Third-party SaaS liability Zero data leakage (SOC2 / HIPAA safe)
Conflict Resolution Built-in graph matching Built-in temporal graph Customizable via deterministic SQL rules
Latency per Turn 120 ms โ€“ 250 ms 90 ms โ€“ 180 ms < 15 ms (Direct local database query)
Cost at Scale $0.01 per extraction Tiered subscription Fractional API cost (Gemini Flash extraction)

6. Enterprise Privacy & Compliance Rules (GDPR & CCPA)

When persisting AI agent memory across sessions, data privacy compliance requires explicit governance:

  1. Right to Inspect: Provide users with a dashboard where they can review all facts the agent has recorded about them (GET /api/v1/user/memories).
  2. Right to Forget: Enable one-click deletion of memories. Deleting a record must immediately remove it from both the relational database and the vector index (DELETE /api/v1/user/memories/{fact_id}).
  3. Automated Expiration (TTL): Ephemeral preferences (e.g. โ€œCurrently traveling in Londonโ€) should carry automated expiration timestamps to prevent obsolete constraints from poisoning prompts months later.
WEEKLY NEWSLETTER

Get Weekly AI Architect Cost & Strategy Updates

Join 14,000+ developers receiving weekly, data-driven cost-reduction blueprints and production-ready agent guidelines.

Professor XAI
Professor XAI ML Engineer passionate about advancing AI technologies and building intelligent systems.
comments powered by Disqus