Context Window Management for Persistent Agents

Managing token budgets in multi-day, multi-turn agents -- sliding window truncation, LangGraph summarization nodes with the trim_messages utility, vector DB offloading of episodic memory, hybrid summarization-plus-retrieval architectures, and the LangGraph 0.3+ Store API for cross-thread persistent memory.

Overview

Overview

Production agents that run for days or weeks hit a hard physical limit: the LLM context window. Even large windows (GPT-4o offers 128K tokens) fill up, because a multi-day conversation with tool round-trips, retrieved documents and verbose responses can add tens of thousands of tokens per hour, and the raw history passes the limit within one business day.

This chapter works up to the design production agents use: memory kept in tiers and assembled into the prompt on every turn. The recent tier holds the last 15-20 exchanges in messages state. The medium-term tier is a running_summary. The long-term tier is a vector DB of prior sessions, and semantic facts live in the LangGraph Store. Each turn, the prompt is built from top to bottom as the system prompt, the retrieved past exchanges, the running summary, the recent messages verbatim and finally the current user message.

> [!WARNING] Context overflow is a user experience failure, not just a technical error. An agent that forgets a constraint the user stated two hours ago is not useful as a persistent assistant.

Interview questions

  • Explain how you manage the context window in a persistent, multi-turn agent that might have a conversation spanning several days.
  • Walk me through the LangGraph code for a summarization node that fires when the conversation exceeds 4000 tokens.
  • How do you ensure the agent remembers a key fact the user mentioned 3 days ago without storing the entire raw conversation?

Sign up free to read the full topic

More Agentic AI topics

Start free on Cariara · Coding problems · System design studio