Skip to content
Back to log

The AI agent loop: Think, call a tool, observe

Chainabit's Chao · · 8 min read

In short

How does an agent use a tool result to decide what to do next? Explore the loop through a ReAct example, Claude tool calls, and Chainabit run events.

What does an agent loop do?

Anthropic defines agents as systems that dynamically manage their own processes and tool use. A task can begin with a user instruction or a conversation with the user. Once the task is clear, the agent can make a plan, work independently, and return to the user when it needs more information or feedback.

Anthropic describes an agent as an LLM that uses tools in a loop guided by feedback from its environment. That feedback can be the result of a tool call or the output of running code. The agent uses those results to assess its progress. It can pause for human feedback when it encounters a roadblock or reaches a particular milestone.

The ReAct study explores how a model interleaves task-related reasoning with actions. Reasoning helps the model create, track, and revise an action plan. Actions let it interact with the external environment and gather more information.

Reasoning, acting, and observing

In ReAct, a reasoning step can determine what needs to be done or revise the plan based on information from the previous action. The observation that follows an action contains the information returned by the external environment. The model can use that observation in subsequent reasoning and action steps.

The study treats reasoning as a language action that leaves the external environment unchanged. The step itself produces no observation from the environment. The model reasons over the current context to generate information that can support later reasoning or action steps.

Reasoning steps can serve different purposes. One step might break down the task goals and create an action plan. Another might extract key information from an observation, track progress, or revise the plan in response to a new development. The study also identifies synthesizing available information into a final answer as a function of reasoning steps.

The ReAct study's information retrieval setup uses three types of actions. search returns the beginning of the relevant Wikipedia page if it exists and suggests similar titles otherwise. lookup returns the next sentence on the page that contains the specified phrase. finish ends the task with an answer.

In one example from the study, the model is asked for the elevation range of the area into which the eastern sector of the Colorado orogeny extends. The model first searches for the Colorado orogeny. The initial observation leaves out the eastern sector mentioned in the question. The model then uses Lookup to search for the relevant phrase.

The resulting observation says that the eastern sector extends into the High Plains. In its next reasoning step, the model uses that information to search for the High Plains. The search result shows that the name refers to more than one region. The model then changes its search to High Plains (United States).

The next observation contains the elevation range the question asks for. The model uses that information to formulate its answer, and the example ends with a Finish action. Each new search target comes from the preceding observation. The more specific search follows the observation that identifies multiple regions with the same name.

In the ReAct study, the order of reasoning and action depends on the type of task. In examples that rely heavily on reasoning, the model alternates between reasoning and action. In decision-making tasks that can involve many actions, reasoning steps may occur less frequently, at relevant points in the sequence.

In Anthropic's description of agents, tool results also help the agent assess its progress. The run can end when the task is complete. Anthropic notes that adding a stopping condition is also a common way to keep the loop under control.

How does a tool call return its result?

According to Anthropic's Claude documentation, the model uses the user's request and the tool description to decide when to call the tool. It then returns a structured call. Where the call runs depends on whether the tool is a client tool or a server tool.

A client tool runs in the application. Claude's response sets stop_reason to tool_use and contains one or more tool_use blocks. The application runs the operation and sends the result back as a tool_result. Claude can then use the result to respond.

In Anthropic's weather example, the initial request defines the get_weather tool. The model returns a tool_use block containing the tool name and inputs. Once the application has the result, it adds the previous assistant content and the result to the messages in the next request. The tool_use_id field links the result to the corresponding call. Claude then answers the original question.

In the example, the tool's input_schema defines a location property and lists it as required. The user's message includes the location. The call Claude generates contains the tool name and inputs, and the application passes the tool result to the model in the next request.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

A server tool runs on Anthropic's infrastructure. Anthropic notes that the results of these tools are available directly. Execution works differently if Claude calls a server tool in the same batch of parallel calls as one of the application's client tools.

With tool_choice set to its default value of auto, Claude decides on each turn whether to call a tool or respond directly. It calls a tool if the request matches the tool's described function and the answer is absent from the current context. Anthropic also explains that tool_choice can require a tool call.

If the user's request omits a required tool parameter, the model may notice the missing information and ask for it or infer a value. Anthropic notes that when a weather tool requires a location and the user leaves it out, the model may guess a value, though this behavior can vary.

The loop in Chainabit run events

Chainabit defines a Run as a single execution of an AI feature, agent, or workflow. A Run follows a defined state machine and can be tracked in real time through Server-Sent Events (SSE). The agent-style run Chainabit describes for Chao can include multiple rounds of planning, tool calls, and response generation before the final assistant response.

A single user message can generate multiple tool.started and tool.completed events. A single message.completed event can follow once the response has been verified. This gives the start and completion of tool execution their own events, separate from the completed message.

Run states indicate where execution stands. queued means the run has been created and is waiting to execute. starting is the preparation stage. While the run is running, execution is underway and partial results can stream over SSE. waiting_tool means the run is waiting for a tool to finish, and waiting_user means it is waiting for user input or approval.

Terminal Run states include completed, which indicates success, and failed, which indicates that execution ended with an error. partially_completed applies when execution stops after producing a partial result. According to Chainabit, a retry creates a new run and leaves the original run in its terminal state.

run.status.changed is emitted when a run transitions to another state. run.error and run.failed indicate that an error occurred during execution. run.settlement.completed indicates that the run has fully settled and reached a terminal state.

Phase and verification events

Chainabit says clients that want a structured view of an agent's progress can subscribe to the agent.* event family alongside message.* and tool.* events. The agent.phase.changed event carries the phase and iteration fields when the agent changes high-level phases.

Possible phase values include plan_or_reason, tool_request, execute_tool, and observe_tool_result. These represent planning or reasoning, requesting a tool, executing the tool, and observing its result. verify, message_draft, and final are also valid phase values for the same event.

Verification has its own events. agent.verify.started indicates that the agent is evaluating a draft response before sending it. agent.verify.passed means the verifier accepted the response. agent.verify.failed means the verifier rejected the draft and the agent will revise it. That event also carries the refinementReason field.

According to Chainabit, agent.budget.exhausted indicates that the agent has reached its limit on reasoning steps and that the final message will contain a partial result. In this case, the event occurs before message.completed. The content field in message.completed contains text explaining the partial result.

Chainabit defines message.delta as clean partial text for display to the user, with raw reasoning excluded. In an eligible Chao session, cot.step is a reasoning step formatted for display that preserves the sequence of reasoning, tool use, and further reasoning while keeping the model's private transcript hidden.

Limitations and when to use agents

Anthropic draws an architectural distinction between workflows and agents. In a workflow, predefined code paths orchestrate the LLM and tools. In an agent, the LLM dynamically directs the process and tool use. Anthropic lists open-ended problems as potential use cases for agents when the number of steps is hard to predict and a fixed path cannot be coded in advance.

As an agent works independently, errors can build up. Anthropic recommends thorough testing in isolated environments and appropriate guardrails for these systems. In some applications, a single LLM call enhanced with retrieval and in-context examples may be enough.

One failure mode examined in the ReAct study is the model repeating earlier thoughts and actions. The researchers also observed that empty search results or results containing no useful information can disrupt the model's reasoning. For coding agents, Anthropic says automated test results can provide feedback as the agent iterates on a solution. It also emphasizes the importance of human review in assessing whether the solution fits broader system requirements.

Ready to start?

Download the app and continue from your phone.

End of Entry