What Large Documents Taught Me About Agent Context and Hallucinations

November 29, 2026 | 8 minutes

When I started building agents to work with large documents, I kept running into the same problems: vague feedback, references to sections that didn't exist, and confident explanations that weren't grounded in the text. I'd given the agent access to the document, so I expected it to use that information accurately, but instead, it tended to remember things that weren't there.

Dividing the work between multiple agents helped reduce those failures. I separated the agent that interpreted the user's request from the agents that read and analyzed the document, then gave each of those agents a focused task and the evidence it needed. That didn't eliminate hallucinations, but it reduced them, made the work easier to inspect, and helped me trace mistakes back to the context each agent received. This post introduces that architecture and the decisions behind it.

The Core Mistake: Treating Context Like Storage

My starting point was straightforward: if the agent needed to understand a document, I'd give it the whole document (assuming it could all fit in the agent's context window). That could work for a short input with a focused question, but the setup became harder to manage as both the documents and the tasks grew more complex.

The agent had to do everything including interpreting the request, finding relevant passages, choosing tools, and producing useful analysis within the same context. As an example, a request to review a manuscript for pacing, repetition, and consistency made that especially difficult because each kind of review needed different evidence. Including all the text didn't tell the agent which passages mattered for each task or how to check its conclusions against them.

To improve the agent's performance, I started treating context as something to assemble for a particular decision. Before passing text to an agent, I wanted to know what it was supposed to do with that text and whether it had enough evidence to do the job. That shift in mindset led me to separate task selection from document analysis.

Separating Orchestration from Document Analysis

In the revised architecture, the main agent interprets the user's intent, identifies the kind of task, and chooses a workflow. Sub-agents read the relevant document sections and carry out the analysis. The main agent never receives the full document, so it's harder for it to hallucinate or think it remembers something from context. It only has the request, available task modes, and the document-related details that the sub agents return.

This gives each part of the system a more focused responsibility. The main agent can decide whether the user wants a review, an edit, a comparison, or a summary without also keeping the manuscript in its context. The sub-agents can concentrate on the requested analysis without choosing among every workflow the application supports.

Before: One Context for the Whole Task

User request
  |
Main Agent
  (instructions + rules + entire document)
  |
Search + tool selection + analysis
  |
Response

In this version, the same agent handles every step. When it returns an incorrect reference, it's difficult to tell whether it found the wrong passage, misread the right one, or produced an answer without checking the text.

After: Separate Contexts for Separate Responsibilities

User request
  |
Main Agent
  (intent + task selection)
  |
Orchestrator Tool
  (workflow + evidence selection)
  |
Sub-Agents
  (selected document sections + focused task)
  |
Results + source references
  |
Main Agent response

The orchestrator selects the evidence, dispatches the work, and collects the results for the main agent. It can use things like BM25 and vector search to narrow the document to the most relevant passages to further reduce the context given to the sub-agent. Because each sub-agent receives a defined task and a known set of passages, I can inspect its input alongside its output when something goes wrong.

How Sub-Agents Get the Right Sections

Separating the agents only helps if they receive the context their tasks require. Selecting that context is the next part of the architecture, and the approach depends on how much the user's request tells you about where to look.

For a targeted request such as "review chapter 6 for pacing" or "compare scenes A and B for tonal drift," the orchestrator can select the relevant sections by their position in the document. The user has already identified the material, so the workflow can pass those sections directly to the appropriate sub-agent.

For broader questions about where Dorothy shows leadership, the relevant passages aren't known in advance. In cases like this, a retrieval pipeline using BM25 and vector search can find candidate passages for the sub-agent to analyze.

Searching for candidate passages give it a starting point, but they don't establish that every relevant passage has been found. This distinction matters for tasks such as contradiction detection. Two conflicting statements may be chapters apart, and an agent that sees only one of them can't compare the pair. A workflow for that task needs to collect related statements across the document and bring them together for review. The goal is to provide enough context for the task, rather than make every input as small as possible.

Why Focused Tasks Helped

Once I separated evidence selection from analysis, I could make the instructions more specific. Instead of asking one agent to review an entire manuscript for pacing, tone, clarity, repetition, and consistency, I could ask one to review chapter 6 for pacing and another to compare selected scenes for tonal drift.

That gave me a clearer way to assess the results. I knew which question each agent was answering and which passages it had received, so I could check whether its feedback followed from that evidence. In my experience, this helped reduce vague observations and made incorrect references easier to investigate.

A focused task still doesn't guarantee an accurate answer, though. An agent can misread a passage or invent a quotation even when its context is short, and a retrieval step can leave out information that would change the conclusion. That's why this architecture still needs verification against the source text, along with a way to report when the available evidence isn't enough.

Tool Design: One Entry Point for Related Workflows

The same separation also changed how I exposed tools to the main agent. Early versions had individual tools for reviewing, editing, summarizing, comparing sections, checking consistency, and detecting repetition. As those workflows grew, I found it easier to manage them through one orchestration tool with explicit modes.

For example, the main agent can call runDocumentTask with a mode such as "review.pacing" and the target chapter. The tool then selects the appropriate workflow, gathers the document sections, dispatches the sub-agents, and collects their results. This keeps the workflow details out of the main agent's instructions while giving the application a defined place to manage them.

One entry point isn't necessarily the best choice for every application. If its modes are vague or overlap, the agent can still choose the wrong one. For this pattern to be useful, each mode needs a clear purpose and defined inputs, and the orchestration code needs to validate the request before starting the work.

The tradeoff is that more of the workflow lives in code that I have to maintain. In return, I can test routing rules, input validation, and evidence selection separately from the model's analysis. I can also evaluate the prompts and outputs, though I can't assume the model will produce the same answer on every run.

Applying the Pattern to Different Documents

The unit of work should follow the task and the document's structure. For a manuscript, a pacing review might need a chapter, while a consistency check might need scenes from several chapters. If the writer is editing during the review, the workflow also needs to track which version supplied the evidence; the incremental indexing post covers keeping those indices current.

For a contract, a similar workflow could gather a clause together with its definitions and referenced sections before reviewing a specific question. For code or technical specifications, it could collect the relevant implementation, interfaces, and callers before checking a proposed change. In each case, the boundaries need to preserve the relationships the agent is being asked to analyze.

That requirement is easy to lose when splitting a document into small pieces. A section may be understandable on its own but still depend on facts elsewhere, so the workflow needs a way to request more evidence when the initial selection isn't sufficient.

The Rule I Follow Now

When an agent struggles with a large document, I look at how the work and evidence are divided before assuming that a larger context window will solve the problem. I want each step to have a clear question, enough source material to answer it, and an output I can check against that material.

Scoped sub-agents helped me build that structure, but the useful part is the explicit connection between the task, its evidence, and its result. Paired with a retrieval pipeline and verification, that connection gives me a more practical way to investigate errors and improve the system.

This architecture depends on selecting useful evidence, which is where retrieval comes in. In Retrieval Is Not One Thing, I compare BM25, vector search, and LLM-based analysis to show what each contributes to that process. Hybrid Retrieval in Practice then combines the layers into a working pipeline, and Incremental Indexing covers keeping the evidence current as users edit.

Related Posts