The agent loop
An agent is a model in a loop: think about what to do, call a tool, read the result, and repeat until it can answer.
Reason, act, observe
ReAct interleaves thoughts (reasoning in text), actions (tool calls in a structured format) and observations (what the tool returned). The model never runs anything itself: the application around it parses the action, runs the tool and appends the observation.
Each turn, the model sees the whole transcript so far and decides whether to act again or to give a final answer.
Step through a run
The lab replays a short run with two tools. The weather numbers are made up. Before each action, predict what the agent should do next.
In LangChain
create_agent runs exactly this loop: call the model; if it returns tool calls, run them and append the results as tool messages; repeat until the model answers without calling a tool. It is built on LangGraph, and a run that never finishes stops at the recursion limit.
from langchain.agents import create_agent
agent = create_agent(
model="provider:model-name",
tools=[get_weather],
system_prompt="You answer questions about the weather.",
)
result = agent.invoke(
{"messages": [{"role": "user", "content": "Which is cooler today, Hanoi or Da Lat?"}]}
)
print(result["messages"][-1].content) # every call and result is in result["messages"]A worked example
Asked which city is cooler today, Hanoi or Da Lat, the agent thinks it needs both temperatures, calls get_weather("Hanoi") and observes 31 °C, calls get_weather("Da Lat") and observes 19 °C, then answers that Da Lat is 12 °C cooler. Each observation is appended to the context, so the next thought is written with the new facts in view.
In practice
Cap the number of steps, stop when the same action repeats, and log every step for debugging. Many modern APIs do the same loop with native tool calls instead of the text format of the original ReAct paper, but the shape is identical: decide, act, observe, repeat.
Easy to confuse
- The “thoughts” are generated text, useful for steering and debugging, not a guaranteed record of how the model decided.
- More steps are not better: each one adds cost, latency and another chance to go wrong.
In the ReAct loop, who actually runs the tool?
The model emits a structured action. The harness executes it and appends the result as an observation for the next step.
Why does the transcript keep growing during a run?
The model is stateless between calls; the growing transcript is its working memory, which is why long runs hit context limits.
What stops the loop?
Each turn the model decides: act again or finish. Harnesses add step and cost limits as a safety net.
Read more in the books
- LangChain and LangGraph documentation, LangChain: LangChain: Agents (the agent loop).
- AI Engineering, Chip Huyen: Ch. 6, “RAG and Agents”: agents (planning and reflection).
- Artificial Intelligence: A Modern Approach (4th ed.), Russell, Norvig: Ch. 2, “Intelligent Agents”.