Learn/LangChain Mastery/Part 7 · 6. Memory & State Management
Lesson 50 of 68
SyllabusLangChain Mastery · Part 7

Entity Memory

Moving from Chat Logs to Database Records. Structured facts.

Course outline & contents
LangChain Mastery
Why Memory Breaks SystemsConversationBuffer & Window MemoryConversationSummaryMemoryEntity MemorySession IsolationPersistence Layers (Redis / DB)

Chat history is unstructured text. To find "What is Bob's favorite color?", the model must read 10,000 words of logs. This is inefficient and prone to error.

Entity Memory solves this by extracting facts into a structured Key-Value store.

Instead of storing: "I was born in 1990 and I love dogs."

Entity Memory extracts:

Json
{
  "entity": "User",
  "facts": {
    "birth_year": 1990,
    "likes": ["dogs"]
  }
}

This is Stable State. It doesn't scroll away. It doesn't get summarized into oblivion.

  1. Extract: An "Extractor Chain" runs on every user input. It identifies entities (People, Places, Preferences).
  2. Update: It updates the in-memory store for that specific entity.
  3. Inject: When the user speaks again, we inject specific facts about mentioned entities into the prompt.
entity_memory.py
from langchain.memory import ConversationEntityMemory

memory = ConversationEntityMemory(llm=llm)
_input = {"input": "My friend Alice likes pizza."}
memory.load_memory_variables(_input)
# Result: Updated 'Alice' entity with fact 'likes pizza'

Pros:

  • Permanent recall of specific facts.
  • Very token efficient (only injects relevant entities).

Cons:

  • Rigid. Ideally needs a schema.
  • Extraction errors (Model might extract "Alice" as a place).

For complex assistants (Travel Agents, RPG NPCs), unstructured logs are not enough. You need a "Character Sheet" for your user.

Key Intuition: "Don't remember the conversation. Remember the facts."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonConversationSummaryMemoryNext lesson →Session Isolation