Agent loop
The agent loop turns one question into a cited answer. A single agent works on a named task. It discovers evidence in the graph, fetches the exact text it needs, and writes an answer that cites only the evidence it fetched. A citation check runs after the answer, and one repair pass asks the agent to remove or fetch citations that do not check out. The run also records messages, tool calls, and tool results as conversation history.
Why it exists
One search can miss part of a multi-part question. "Who worked with Charles Babbage, and where?" needs two facts that may sit in different passages. The agent runs several evidence calls for one task, and it keeps the task state between calls.
The agent can also resume a task. Each task has a name. A run with the same task name reads the events recorded for that task and reuses its completed steps. The agent fetches again the passages it cites, so each citation comes from a fetch in the current run.
How it works


- The run creates or resumes the task and builds its task view. The view is data, and the agent reads it before it acts.
- The agent calls evidence tools.
search_evidencereturns evidence ids for passages in scope.entity_searchandexplore_relationshipssearch the graph.navigate_document_treemoves from one passage to related text. See The agent's tools. - The agent calls
fetch_evidenceon the ids it wants to cite. Only a fetch that returns complete text makes an id citable. - The agent writes the answer and cites ids in square brackets.
- The run checks every cited id against the ids that were fetched in scope. If some are invalid and the tool budget has room, one repair pass asks the agent to remove or fetch them.
- The run returns the answer, a status, the valid citations, and any invalid ids.
The agent's tools
The agent has two groups of tools. The evidence tools read your documents and graph. The task and decision tools record what the agent does and decides. The caller fixes the document scope when it builds the agent, and the model has no argument to change it. The parameters and limits of each tool are in the tools reference.


Evidence tools
| Tool | What the agent uses it for |
|---|---|
search_evidence | Finds passages for a question by lexical, semantic, or both and returns evidence ids and previews. |
fetch_evidence | Reads the exact text for evidence ids. Only a complete fetch makes an id citable. |
navigate_document_tree | Reads the section, the neighbors, or the table rows around a passage, using one of six relations. |
entity_search | Finds entities by name or meaning and lists the passages that mention them. |
explore_relationships | Follows the relations around an entity, out to two steps. |
search_themes | Finds themes, groups of related entities from the last community detection run, with the passages that mention them. Use it for broad questions that span many documents. |
check_citations | Checks that cited ids were fetched in this run. |
The system prompt tells the agent to read the task view first, to find and fetch evidence before it answers, to fetch relevant hits before it refines a search, and to stop searching once the fetched passages answer the question. It tells the agent to cite only ids whose fetch returned complete text, and to say when the evidence is insufficient.
For a question that names an entity, the agent usually calls the tools in this order.
entity_searchfinds the entity and the passages that mention it.explore_relationshipslists the relations around it.fetch_evidencereads the passages on the assertion edges that matter.- The agent writes the answer with the evidence ids in square brackets.
A question with no named entity usually starts with search_evidence, then fetch_evidence, and uses navigate_document_tree when a passage is cut off.
Conversation history and decision tools
The agent also has tools to read its task (get_task, list_events, fetch_event, list_steps), record progress (set_step), and read or write decisions (search_decisions, get_decision, list_decisions, list_support_history, list_decisions_for_review, record_decision, supersede_decision, reaffirm_decision, continue_decision_review). It cannot reject a human decision. Tasks and resuming describes how a run records and reuses this work.
Tasks and resuming
The run records messages, tool calls, tool results, and errors as ordered task events without a separate call from the agent. A repeated write returns the saved result. The first run stores the question as the task goal, and a later run appends its question as an event. When a run resumes, steps that were still in progress become unknown, and the task view lists each completed step as reusable or not. The agent uses the first client in AGENT_LLM_CLIENTS, and you pass a tracer to build_agent to record spans for the run.
The agent can also record progress with set_step and record decisions with record_decision. It reads earlier decisions with search_decisions. A decision counts only when its write tool confirms that the record was saved.
Result status
| Status | Meaning |
|---|---|
answered | The agent returned a structured answer that the evidence supports. All cited ids passed the citation check. |
insufficient_evidence | The agent returned a structured answer that says the evidence is insufficient. All cited ids passed the citation check. |
partial | The agent returned no structured answer, or some cited ids were still invalid after the repair pass. The repair pass runs only when the tool budget has room. |
partial_retrieval | A search, graph, or fetch call failed, or returned partial or truncated results, during the run. |
budget_exhausted | The run used all of its max_tool_calls tool calls, or it reached recursion_limit. |
recording_failed | A task event or decision could not be saved. The result lists each one in recording_failures. |
When more than one condition applies, the run returns the first status that applies in this order: recording_failed, budget_exhausted, partial_retrieval, partial, insufficient_evidence, and then answered.
Citation ids
Evidence ids start with ev1:. A span id names a range of a chunk, and a chunk id names a whole chunk. The agent copies these ids from search results. It does not make them up.
Limits
These settings bound one run. Set them with AGENT_ variables or AgentSettings.
| Setting | Default | Effect |
|---|---|---|
recursion_limit | 50 | The maximum LangGraph steps. A run that reaches it returns budget_exhausted. |
max_tool_calls | 20 | The maximum tool calls in one task run. |
max_result_characters | 12000 | The maximum text that one evidence fetch returns. |
The result has two main parts. answer holds Markdown with numbered references such as [1] and a Sources list. Numbers follow first appearance, and passages from the same document version share a number. citations lists the actual fetched evidence, including canonical IDs, stored titles, source URIs, and locations. A citation to a narrower span retains its fetched parent in that list. Invalid citation groups remain visible and keep the run's partial status.
For example, an answer can contain Revenue increased. [[1]](https://example.org/report.pdf) followed by [1] [Annual report](https://example.org/report.pdf) in its Sources list. Local files and inline documents use plain numbered references and document titles. URLs point to the stored source address; the fetched text and content hash identify the cited version. Superseded evidence is labeled in the source list.
The agent validates canonical IDs before formatting the answer. Do not pass the formatted answer back to check_citations to recover provenance. Use the returned citations records instead. Existing Markdown links are left unchanged; the automatic formatter handles canonical IDs in plain square brackets, including comma- or semicolon-separated groups.
Citations in MCP clients
fetch_evidence returns the same source metadata and a citation_markdown field in both its structured result and serialized text. The field is null unless the evidence was fetched completely. MCP clients should keep evidence_id for check_citations, then use numbered claim references and a Sources list built from citation_markdown. The client controls its final answer and link rendering, and can also build its own presentation from the returned title, URI, and passage location.
The agent needs the agents extra. build_agent raises AgentMissingExtraError when the extra is not installed.
Design choices
The agent has no subagents. The general-purpose subagent is disabled in the harness profile, so every evidence call runs in the one agent.
The agent judges only the evidence it fetched. The citation check, not the model, decides whether a cited id is valid. A fetch outside the trusted scope fails, so the agent cannot cite text the caller did not allow.
Source passages, task events, and tool results are data. The system prompt tells the agent not to follow instructions found in them.
Related
- Guide: Retrieve and answer shows how to call the agent and read its result.
- Guide: Trace an agent run shows how to record spans for a run.
- Retrieval compares the strategies behind the evidence tools, and Evidence ids and citations describes the citation check that follows the answer.
- Concepts: Observability describes the spans that an agent run records.
- Reference: Tools for tool parameters, Agents API, Configuration
- Reference: Candidates explains the records that hold items the schema rejected.