Context Bloat: Pruning and Compacting Agent State

RoadmapsAI Engineering

Overview

Techniques for managing an agent's growing state by summarizing old context (compaction) and dropping irrelevant tool outputs (pruning) before calling the LLM.

Agents accumulate state at every step. Without pruning, this unbounded state leads to context collapse, massive latency, API limits, and ultimately agent failure.

Where used: LangGraph state machines, Multi-agent systems, Persistent AI assistants

Why learn this

Code walkthrough

def pruned_node(state):
    recent_msgs = state['messages'][-3:]
    summary = state.get('summary', '')
    return llm.invoke(f"{summary}\n{recent_msgs}")

Focus: Notice how the node extracts only a slice of `messages` and a pre-computed `summary`, purposefully ignoring the rest of the massive `state` object.

Common mistakes

Recall questions

Understanding checks

If an agent retrieves 50 documents and runs this code, what is the most likely outcome?

The API request fails with a `context_length_exceeded` error, or suffers massive latency.

Passing the entire unbounded state dictionary directly to the LLM causes context bloat. The state must be pruned before invocation.

Why would an architecture keep raw documents in the `state` dictionary if they aren't being passed to the LLM?

Because the state machine uses them for programmatic routing, metric logging, or passing to specific downstream tools.

State is the system's database, while the LLM prompt is just its working memory. They do not need to be perfectly synchronized.

Practice tasks

Implement a Compaction Reducer

Write a function that takes a list of messages. If the list has more than 5 messages, replace the oldest messages with a 'summary' string, keeping the 3 most recent messages intact.

Continue learning

Previous: State machines for agents

Previous: Agent Memory: Short vs. Long Term

Previous: Context Window

Return to AI Engineering Roadmap