Skip to main content
Version: latest

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​

A run creates or resumes a task and builds its task view. The agent calls evidence tools to find ids and fetch exact text, then writes an answer. The run records messages, tool calls, and tool results as task events. A citation check compares the cited ids with the fetched ids. The run returns the result when all ids are valid, or gives the agent one repair pass when the budget allows.A run creates or resumes a task and builds its task view. The agent calls evidence tools to find ids and fetch exact text, then writes an answer. The run records messages, tool calls, and tool results as task events. A citation check compares the cited ids with the fetched ids. The run returns the result when all ids are valid, or gives the agent one repair pass when the budget allows.
  1. The run creates or resumes the task and builds its task view. The view is data, and the agent reads it before it acts.
  2. The agent calls evidence tools. search_evidence returns evidence ids for passages in scope. entity_search and explore_relationships search the graph. navigate_document_tree moves from one passage to related text. See The agent's tools.
  3. The agent calls fetch_evidence on the ids it wants to cite. Only a fetch that returns complete text makes an id citable.
  4. The agent writes the answer and cites ids in square brackets.
  5. 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.
  6. 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.

The agent sits between two tool groups. Evidence tools read the scoped documents and return evidence ids and exact text. Task and decision tools write and read the task log, steps, and decisions. The agent's answer passes the citation check before it is returned.The agent sits between two tool groups. Evidence tools read the scoped documents and return evidence ids and exact text. Task and decision tools write and read the task log, steps, and decisions. The agent's answer passes the citation check before it is returned.

Evidence tools​

ToolWhat the agent uses it for
search_evidenceFinds passages for a question by lexical, semantic, or both and returns evidence ids and previews.
fetch_evidenceReads the exact text for evidence ids. Only a complete fetch makes an id citable.
navigate_document_treeReads the section, the neighbors, or the table rows around a passage, using one of six relations.
entity_searchFinds entities by name or meaning and lists the passages that mention them.
explore_relationshipsFollows the relations around an entity, out to two steps.
search_themesFinds 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_citationsChecks 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.

  1. entity_search finds the entity and the passages that mention it.
  2. explore_relationships lists the relations around it.
  3. fetch_evidence reads the passages on the assertion edges that matter.
  4. 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​

StatusMeaning
answeredThe agent returned a structured answer that the evidence supports. All cited ids passed the citation check.
insufficient_evidenceThe agent returned a structured answer that says the evidence is insufficient. All cited ids passed the citation check.
partialThe 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_retrievalA search, graph, or fetch call failed, or returned partial or truncated results, during the run.
budget_exhaustedThe run used all of its max_tool_calls tool calls, or it reached recursion_limit. Once the budget is spent, the agent offers the model no more tools except the answer tool, and a run can still reach recursion_limit with no answer.
recording_failedA 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.

SettingDefaultEffect
recursion_limit50The maximum LangGraph steps. A run that reaches it returns budget_exhausted.
max_tool_calls20The maximum tool calls in one task run.
max_result_characters12000The 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.

note

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.