Course Content
LangChain Mastery
7 sections · 109 lessons
How do you debug LangChain memory issues in long conversations?
What you need to know
Look at what was sent
For an agent with a checkpointer, read the stored state for the thread:
1config = {"configurable": {"thread_id": "emp-10233"}}2state = agent.get_state(config)3messages = state.values["messages"]45from langchain_core.messages.utils import count_tokens_approximately6print("messages:", len(messages), "tokens:", count_tokens_approximately(messages))7for m in messages[:2] + messages[-4:]:8 print(m.type, "|", str(m.content)[:80])In LangSmith, open the model run for the failing turn: the input shows the exact messages after any trimming or summarising middleware ran — which may differ from what is stored.
The usual suspects
| Symptom | Likely cause | Check |
|---|---|---|
| Cost and latency rise every turn, then a context error | Trimming or summarisation not running | Plot tokens per turn — should level off |
| Forgets something said 2 turns ago | Trimmed from the wrong end, or budget too small | strategy="last", larger budget |
| Forgets the system instructions | System message trimmed away | include_system=True |
| Facts slowly change ("12 days" becomes "10 days") | Summaries of summaries | Keep key facts in structured state; summarise from original messages |
| Provider error about tool messages | Trimming split a tool call from its result | start_on="human", trim at message-pair boundaries |
| Sees another user's history | Shared or wrong thread_id | Build ids from the authenticated user, never from input |
Store important facts outside the chat
Facts that must never be lost — employee id, approved dates, booking reference — belong in structured state (custom agent state fields or a database), not only in chat text that may be trimmed or summarised.
A real-life example
An HR bot handles leave planning in long chats. Around turn 25, employees report it "forgets" that they already chose dates and asks again.
The engineer logs tokens per turn: they grow until turn 24, then drop sharply — SummarizationMiddleware fires at 6,000 tokens. The LangSmith trace for turn 25 shows the summary: "User discussed leave options." The chosen dates, 14–18 October, were lost in the summary.
Two fixes: the summary prompt now says "keep all dates, numbers and decisions exactly", and the chosen dates are saved into a leave_draft field in the agent state when the user confirms them, so they no longer depend on chat history. The repeat-question complaints stopped.
Follow-up questions to expect
- "How do you test memory behaviour?" — Scripted multi-turn conversations in the eval set that ask about something from turn 3 at turn 30.
- "Trim or summarise?" — Trim for short support chats; summarise when older context matters, and keep key facts in structured state either way.
- "What about legacy memory classes?" —
ConversationBufferMemory,ConversationSummaryMemoryand friends are inlangchain-classic; the debugging approach is the same — inspect what reaches the model.