Learn / Frameworks / LangChain / Chat Memory

LangChain · Lesson 6 of 15

Chat Memory

Thread a conversation through message history without stuffing the entire past into every call.

  • Beginner
  • 15 min read
  • 3 objectives

Before this lessonLesson 5: Evaluation and Production

What you will learn

  • Keep a message list
  • Trim old turns
  • Store history outside the process

Your Progress

0 of 15 lessons 0%

  • Lessons0 / 15
  • Completed0
  • Est. time left~ 4 hours

Create a free account to keep your progress on every device.

A chat model is stateless. If you want a conversation, you send the previous messages each turn. Memory is just that list, stored somewhere durable, then trimmed so it fits the context window.

The message list

from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, trim_messages

history = [
    SystemMessage("You are a concise assistant."),
    HumanMessage("My name is Ada"),
    AIMessage("Hi Ada."),
]

def reply(user: str) -> str:
    history.append(HumanMessage(user))
    window = trim_messages(history, max_tokens=2000, strategy="last", token_counter=len)
    ai = model.invoke(window)
    history.append(ai)
    return ai.content

Store it outside RAM

A Python list dies with the process. Write messages to Redis, Postgres or LangGraph's checkpointer, keyed by a thread_id you put on the session cookie or API header.

# sketch: load, append, save
rows = db.load_messages(thread_id)
rows.append({"role": "user", "content": text})
answer = model.invoke(to_messages(rows))
rows.append({"role": "assistant", "content": answer.content})
db.save_messages(thread_id, rows)
Up next · Lesson 7Loaders and SplittersLoad PDFs and web pages, then split them so retrieval stays accurate.