DEV Community

Srijan Verma
Srijan Verma

Posted on

Stop Passing Full Chat Transcripts: Build a Two-Tier AI Memory System

Decouple ephemeral conversation context from persistent user preferences with a hybrid cache and KV store.

Most developer tutorials show you how to build AI agents by appending every user message to a local Python list. While this works in a notebook, it immediately falls apart in production backends.

The Bottleneck in Production

The naive approach stores chat histories directly in process memory or dumps entire conversation logs back into the LLM context window on every turn.

# The Anti-Pattern: Unbounded memory growth & volatile storage
chat_history.append({"role": "user", "content": prompt})
response = openai.chat.completions.create(model="gpt-4o", messages=chat_history)
Enter fullscreen mode Exit fullscreen mode

This pattern breaks for three reasons:

  1. Volatile Lifecycle: If your container restarts or scales down, the user's entire interaction state vanishes.
  2. Context Bloat & Cost: Sending 30 turns of raw conversation spikes latency and blows through token budgets without adding meaningful signal.
  3. Loss of Critical Facts: When the message list inevitably hits context limits, naive truncation drops early messages—erasing core details like dietary preferences, user IDs, or system constraints.

The System Architecture & Fix

To build a production-grade agent, you must decouple conversational context (short-term) from durable user preferences (long-term).

Instead of maintaining a massive array of raw dialogue, implement a two-tier memory architecture:

  • Tier 1 (L1 - Short-Term Session Cache): A fast, in-memory store (or Redis) that tracks the active session's recent dialogue turns and immediate state.
  • Tier 2 (L2 - Long-Term Persistent KV Store): A durable key-value store (DynamoDB, Postgres JSONB, or S3) containing structured, long-lived facts about the user.
                  +-----------------------+
                  |      User Request     |
                  +-----------+-----------+
                              |
                              v
                  +-----------------------+
                  |    Agent Controller   |
                  +-----+-----------+-----+
                        |           |
       1. Read Session  |           | 2. Read Preferences
                        v           v
             +------------+       +-------------+
             |  L1 Cache  |       | L2 KV Store |
             |  (Redis)   |       |  (Database) |
             +------------+       +-------------+
                        |           |
                        +-----+-----+
                              |
                              v
                  +-----------------------+
                  | Merged Prompt Context |
                  +-----------+-----------+
                              |
                              v
                  +-----------------------+
                  |     LLM Provider      |
                  +-----------------------+
Enter fullscreen mode Exit fullscreen mode

When a request arrives, the agent loads structured facts from Tier 2, merges them with the recent context from Tier 1, and passes an optimized

Top comments (0)