DEV Community

Mustafa ERBAY
Mustafa ERBAY

Posted on • Originally published at mustafaerbay.com.tr

AI Agent Memory: Short-Term and Long-Term Memory Design

AI Agent Memory is a data structure that manages pieces of information generated and stored during interaction with a Large Language Model (LLM) across two distinct timeframes (short-term and long-term). Short-term memory holds the immediate query context, while long-term memory stores previous sessions and persistent knowledge accumulation. The transition and design decisions between these two layers play a critical role in system performance, scalability, and privacy. Popular AI frameworks like LlamaIndex and LangChain offer mechanisms for managing these memory types in agent systems.

How Does AI Agent Memory Work?

Short-term memory is a temporary data store initialized from scratch with each request, ensuring the model only remembers the current context and doesn't mix in responses from previous sessions. Long-term memory, on the other hand, permanently stores summaries of a given session, learned patterns, and references from external data sources. The data flow between these two layers allows the LLM to produce more coherent responses by combining both immediate context and past experiences.

The primary advantage of this architecture is controlling preferential memory consumption while simultaneously preventing information loss. When short-term memory is cleared, the model only responds to the new input in the next request; long-term memory still recalls summaries of previous sessions, ensuring consistency.

ℹ️ Key Concepts

Short-term memory: Data stored for at most a few seconds, containing immediate context.

Long-term memory: Persistent knowledge store, retained for days, weeks, or even months.

When is Short-Term Memory Design Preferred?

Short-term memory is a critical component in real-time chat applications requiring low latency and high response consistency. For example, a customer service chatbot needs to respond instantly to a user's query while only remembering the last few messages; otherwise, mixing previous conversations degrades response quality. In this scenario, a buffer based on collections.deque is preferred.

A typical Python example for short-term memory:

from collections import deque

# Holds a maximum of 10 messages; older messages are automatically discarded
short_term = deque(maxlen=10)

def add_message(msg):
    short_term.append(msg)

def get_context():
    return " ".join(short_term)
Enter fullscreen mode Exit fullscreen mode

This code snippet maintains the current context by calling the add_message function after each request; the get_context function returns a single string that can be added to the model input. The maxlen parameter of deque keeps memory usage constant, preventing RAM overflow.

Configuration Recommendations for Short-Term Memory

Parameter Recommended Value Description
maxlen 10‑20 (messages) Sufficient for most chat flows.
serialization msgpack Fast serialization, low CPU cost.

msgpack is an efficient binary serialization format that typically offers faster serialization and lower CPU costs compared to text-based formats like JSON. These settings minimize CPU and memory consumption, ensuring stable performance under high request volumes.

Under What Conditions is Long-Term Memory Design Necessary?

Long-term memory allows an AI agent to recall past sessions, learned patterns, and references from external data sources. In a manufacturing ERP system, an operator's planning and decisions from previous days could be offered as suggestions by the AI; in such a case, without long-term memory, the system would provide inconsistent recommendations. Long-term memory is indispensable for state persistence and information integrity.

Long-term memory is typically stored in a database (e.g., PostgreSQL) or a vector database (e.g., Milvus). For PostgreSQL, the pgvector extension allows vector embeddings to be stored and searched directly within the relational database. Vector-based storage enables fast retrieval of embeddings for semantic search. Below is a simple PostgreSQL table structure and an insertion example:

CREATE TABLE agent_memory (
    id SERIAL PRIMARY KEY,
    session_id UUID NOT NULL,
    embedding VECTOR(1536) NOT NULL, -- Common dimension for OpenAI text-embedding-3-small or Gemini embedding models
    metadata JSONB,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);

-- Inserting a new memory entry
INSERT INTO agent_memory (session_id, embedding, metadata)
VALUES (
    'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
    '[0.12, -0.04, 0.33, ...]',  -- 1536‑dimensional embedding
    '{"topic":"planning","importance":"high"}'
);
Enter fullscreen mode Exit fullscreen mode

This table stores a separate embedding for each session, enabling long-term knowledge accumulation. The VECTOR(1536) dimension is compatible with one of the recommended output dimensions for models like OpenAI's text-embedding-3-small and Google's Gemini embedding models. The metadata field contains additional contextual information (e.g., topic, priority) and allows for filtering during querying.

Performance Tips for Long-Term Memory

  • Indexing: Creating IVFFlat indexes with the pgvector extension can speed up approximate nearest neighbor (ANN) searches.
  • Partitioning: PostgreSQL supports table partitioning. Partitioning tables by session_id can archive older sessions and improve query performance.
  • Compression: PostgreSQL's TOAST mechanism automatically compresses values larger than 2 KB and/or moves them to a separate TOAST table, reducing disk usage.

These settings keep response times at acceptable levels even as the data volume grows.

How is Data Flow Between Memory Layers Modeled?

The data flow between short-term and long-term memory can be defined as a pipeline: new message is stored in short-term memory → summary is extracted → it's added to long-term memory → relevant summary is retrieved from long-term memory in the next request → it's combined with short-term memory. This flow minimizes system latency while maintaining information consistency.

The following Mermaid diagram visualizes this process:

Diagram

In this diagram:

  • Summary Extractor concatenates messages from short-term memory and generates a summary using a transformer-based model.
  • Summary Retriever fetches relevant session summaries from long-term memory and combines them with short-term memory.
  • The Loop repeats with each new request, allowing the system to use both immediate context and historical knowledge simultaneously.

Code Example for Data Flow

def summarize_context(messages):
    # Simple transformer summarization example
    input_text = " ".join(messages)
    summary = llm.summarize(input_text)  # Example LLM API call
    return summary

def persist_summary(session_id, summary):
    pg.execute(
        "INSERT INTO agent_memory (session_id, embedding, metadata) VALUES (%s, %s, %s)",
        (session_id, embed(summary), {"type": "summary"})
    )
Enter fullscreen mode Exit fullscreen mode

These two functions automate the short-term memory → long-term memory transition; the embed function is a helper that converts text into a vector.

What are the Performance and Scalability Trade-offs?

Short-term memory directly impacts RAM consumption; as the maxlen value increases, memory usage grows, and the risk of GC pauses increases under high request volumes. Long-term memory, on the other hand, is associated with disk I/O and CPU intensity; embedding calculation and vector search can lead to a CPU-bound state, especially with high-dimensional datasets. The balance between these two layers determines the latency-throughput balance.

Trade-off Table

Layer Advantage Disadvantage Recommended Use
Short-Term Low latency, immediate context Memory limit, risk of data loss Real-time chat, short sessions
Long-Term Persistent knowledge, consistency CPU & Disk load, complex queries Historical analysis, planning, recommendation systems
Combined Both fast responses and consistent history Management complexity Cross-session AI assistants

In this table, short-term memory should primarily be used in CPU-limited environments, while long-term memory is preferred in environments with sufficient disk and GPU resources. For instance, only short-term memory might be usable on an edge device; however, both layers can be enabled on a central server.

How are Security and Data Privacy Ensured?

Data transferred between memory layers may contain personally identifiable information (PII); therefore, encryption-at-rest and encryption-in-transit are mandatory. Since short-term memory is kept in RAM, memory locking (mlock) can be used at the operating system level; this prevents the memory from being written to swap space. For long-term memory, PostgreSQL's Row-Level Security (RLS) features should be implemented. PostgreSQL does not have a built-in Transparent Data Encryption (TDE) feature; however, this can be achieved through file system encryption or third-party extensions like Percona's pg_tde.

Below is an example of defining RLS in PostgreSQL:

-- User-based access control
CREATE POLICY session_owner_policy ON agent_memory
    USING (session_id = current_setting('app.session_id')::uuid);

ALTER TABLE agent_memory ENABLE ROW LEVEL SECURITY;
ALTER TABLE agent_memory FORCE ROW LEVEL SECURITY;
Enter fullscreen mode Exit fullscreen mode

This policy ensures that only users with the active session ID can access their own session data. Additionally, all read/write operations are logged with extensions like pgAudit, allowing for the monitoring of potential data breaches.

⚠️ Data Privacy Tips

  • Protect short-term memory data with RAM encryption (hardware-based solutions like Intel SGX).
  • Implement masking and tokenization within long-term memory to obscure sensitive fields.
  • Check audit logs daily and report any abnormal access.

Conclusion

AI Agent Memory optimizes both response speed and information consistency by intelligently managing short-term and long-term memory layers. Short-term memory provides low latency in real-time interactions, while long-term memory is indispensable for persistent knowledge accumulation and consistency of past sessions. The data flow between memory layers can be implemented as a pipeline through steps of summary extraction and retrieval, which enhances system scalability and security. By carefully evaluating performance-security trade-offs and implementing appropriate configurations and access controls, you can design AI agents' memory architecture in a practical and sustainable manner.

Next step: Test these two layers separately in your own environment, monitor measurement results, and determine settings that align with real-world workloads. Happy coding!

Official Resources

Top comments (0)