Skip to main content
Welcome back. In this lesson we cover a core capability for conversational AI: adding memory to LLM applications. Memory (also called history or context) enables multi-turn conversations by supplying prior exchanges to the model so it can respond coherently over time.
The image is a flowchart showing the interaction between a user and an application, which processes a prompt and response through context, history, and a language model.
Why history is necessary
  • LLMs are stateless by default. Each request is treated independently unless you include prior context.
  • Without history, the model has no memory of earlier turns and can produce inconsistent or hallucinated responses.
  • To preserve continuity, you must capture relevant prior exchanges, persist them as needed, and send the appropriate context with each prompt.
LLMs do not remember past requests on their own. Think of each model call like a single HTTP request: to provide continuity, you need to re-supply the prior conversation or a summary of it with each request.
Memory: short-term vs long-term Memory for LLMs generally falls into two categories:
The image shows three icons representing Short-Term Memory, History, and Long-Term Memory, with a note indicating "Stored at RAM" next to the Short-Term Memory icon.
Choosing a long-term store Long-term memory is implemented by externalizing conversational data to a database or vector store. Your choice depends on retrieval patterns, scale, and search needs. Common options include:
  • Redis — simple to integrate, supports multiple data structures and expiration policies.
  • SQLite — lightweight SQL database for local persistence.
  • Vector stores — for semantic similarity search when using embeddings (e.g., Faiss, Milvus, Pinecone, Weaviate).
  • Other databases — Postgres, MongoDB, cloud-managed stores.
The image lists Redis and SQLite as examples of long-term memory storage solutions.
Example: basic Redis-backed conversation memory Below is a minimal pattern for persisting and rehydrating conversational history to Redis. Store each message as a list element per conversation_id, then retrieve recent messages to build the prompt.
  • Pseudocode to append a message:
  • Pseudocode to rehydrate context before generating a response:
This pattern can be adapted for vector stores: instead of saving raw text only, index embeddings to support semantic retrieval for long-term facts and memories. Security, privacy, and retention
Be mindful of privacy and compliance when persisting conversation data. Mask or redact sensitive information, enforce retention policies, and secure access to your storage backend.
Putting it together
  • Short-term memory is ideal for active sessions and quick context passing.
  • Long-term memory is necessary for cross-session continuity, personalization, and recall of past facts.
  • Choose the right store based on access patterns: Redis for fast ephemeral lists, vector stores for semantic search, and SQL/NoSQL systems for structured user data.
  • Always apply retention, redaction, and security best practices to stored conversation data.
Further reading and references
  • Redis
  • SQLite
  • Vector store and embedding solutions (Faiss, Milvus, Pinecone, Weaviate)

Watch Video