Controllable Context Graphs for Long-Running AI Agents
This article explains why context pollution in long agent sessions is a visibility problem rather than a length problem, and how an explicit context graph lets a human decide what a model carries into the next turn. It covers how graph nodes and edges represent "still in effect" context, how this approach divides labor with full-history replay and automatic compaction, the four categories of content worth sending each turn, how a turn is executed and rendered on the graph, when to condense versus preserve originals, how to attach material nodes safely, and the pitfalls that make a context graph collapse back into a chat log.
The problem isn’t whether the model can still read the history
After an agent has worked for dozens or even hundreds of turns, the session is no longer a question-and-answer record. It is a pile of material with different origins, different shelf lives, and different levels of trust: assumptions that have already been disproved, large tool outputs dumped during debugging, branches that went off track, and the decisions and constraints that genuinely need to be honored long term. All of it is stacked on a single linear timeline.
The model can keep ingesting the full history, or the system can summarize it automatically. Neither approach answers a more practical question: before the next turn begins, what is still influencing the model? Automatic compaction is usually not reviewable, and a full history gives you no way to say “this part should no longer be in effect.” The essence of context pollution is not that the history is too long, but that the effective scope is invisible and cannot be revoked individually.
Logs and traces are good at answering “what just happened”: which tools were called, which files changed, where a step failed. They are an audit view. What a context graph adds is something else: before the next request goes out, a human decides what the model should carry forward.
Expressing “still in effect” context as a graph
Turn a long task from a single message stream into a graph that is continuously updated. Nodes can be a conversational turn, a piece of material, or a note; an edge means “this content enters subsequent requests.” There is only one rule: a connection is context.
This has four direct consequences.
- Only nodes connected to the current question enter the next model input. Unconnected content stays on the canvas but does not participate in reasoning.
- Cutting an edge takes a branch out of subsequent requests. The original text is not deleted and can be reconnected at any time.
- A long path can be condensed into a shorter new path. The result is a new node that does not overwrite the original record, which avoids the “the summary was wrong and now nothing can be reconciled” problem.
- External material — document excerpts, web page takeaways, additional constraints — can be attached as a standalone node to a new question, instead of being pasted into an old message where it can never be separated again.
A graph is not another skin for a chat history. Linear conversations emphasize the order in which things happened; a graph emphasizes dependencies and effective scope. Order still matters, but order does not mean “everything must be included.”
How it divides work with automatic compaction and full replay
The three strategies solve different problems and should not substitute for one another.
| Approach | Strengths | Weaknesses |
|---|---|---|
| Full replay | Loses no detail, easy to verify after the fact | Noise accumulates, and users cannot tell what is still in effect |
| Automatic compaction | Controls length, reduces irrelevant detail | The basis for compaction is opaque, and a wrong assumption can be written down as “fact” |
| Explicit context graph | A human selects the effective set; edges can be cut, reconnected, and checked against the source | Requires a human to make a choice; it cannot be zero-effort |
In practice they can coexist: the trace keeps recording what the agent did, while the graph maintains only “what the next turn should see.” If automatic compaction is used, it should produce a new condensed node and keep an edge pointing to the original text, rather than overwriting in place.
What actually goes to the model next turn should be fixed as four checkable categories, not “the last N messages.”
- The task goal and its completion criteria.
- Constraints still in effect: interface contracts, directories that must not be changed, compliance boundaries, tradeoffs the user has already confirmed.
- Key decisions on the current path, each with one sentence on “why it still holds.”
- The evidence required to complete this step — for example, the key fragment of a particular tool result, not the entire log.
Disproved assumptions can stay on the graph but are not connected by default. If the model needs to be told “do not go down this path again,” use a short negative-constraint node rather than reconnecting the whole failed conversation.
How a turn travels through the graph
When you start an agent turn on the graph, the question node should still be a real agent turn: it reuses the tools, permissions, and workspace already configured in the current session, rather than spinning up a separate chat-only sandbox. The answer is written back into the graph and also into the session log, so the audit chain stays unbroken.
The flow below is pseudocode. It describes the orchestration relationship, not one fixed API:
question = new_node(user_prompt)context = nodes_reachable_by_selected_edges(question)payload = render(context) + questionresult = agent_turn(payload, tools=session.tools)attach(question -> result_node(result))append_session_log(question, result, tool_trace)
render should be stable and repeatable: the same set of connections produces the same context bundle. Group it into sections for constraints, decisions, evidence, and the question, and tag each fragment with its source node ID so it is easy to answer “where did this sentence come from.” Do not put presentation data such as canvas coordinates or colors into the model input.
After an edge is cut, the next context computation must reflect it immediately. If the system separately maintains a “hidden rolling summary” that still contains the cut branch, the graph has lost its meaning. There can be only one source of truth for the effective set: the currently selected edges.
When to condense and when to keep the original
Condensing fits these situations: multiple debugging rounds have converged and only the final conclusion remains; only a few fields in a tool output are still useful; the same constraint has been stated repeatedly and needs to be reduced to one canonical statement.
Cases where you should not drop the original edge after condensing: the conclusion is still disputed; numbers, paths, or error codes may need to be verified later; the user explicitly said a particular original text is authoritative. In these cases the condensed node can connect into the next question while a “provenance” edge to the original is retained. Provenance edges do not enter the model input by default and exist only for a human to expand.
Condensing should itself be a replayable operation: record who summarized what scope, and which parent nodes the summary node has. If you later find the summary drifted, you can cut the summary and reconnect the original instead of hunting through the entire session.
Attaching material nodes without polluting context again
Do not paste document excerpts or supplementary notes directly into the question body. Make them standalone material nodes and state their purpose clearly: this is a spec, this is an example, this is a counter-example. Connect only the few needed to complete the current question. When one piece of material is shared by several questions, editing the material node affects all subsequent connections, which avoids the same passage being copied separately across a dozen turns and gradually diverging.
Before material enters the model, trim it in a way a human can scan: remove navigation, repeated headers, and long quotations unrelated to the task. The trimmed result should preferably still be a node on the graph, not something that exists only in a temporary string for one request.
Pitfalls to watch for in practice
- Treating the graph as a second chat window and connecting everything, which makes it no different from a graph-less setup.
- Cutting an edge while an old assumption still lives in the rolling summary or the system prompt.
- Condensing over the original text, so the evidence from that moment cannot be recovered after an error.
- Storing entire tool outputs and connecting them by default, so noise quickly exceeds the decisions themselves.
- Having only a view, with no list of “which nodes were actually sent in this request,” so the user still does not know what the model saw.
The minimum viable checkpoints are actually few: show the list of nodes that will be included before sending the request; write the answer and the trace back into the same graph after execution; make every automatic summary produce a new node. Do these three things, and context in a long session shifts from “the model probably still remembers” to “a human decides what it remembers.”
