LLM Lab

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.

Where this sits in a LangChain agent
Model→Tools→Agent loop (create_agent)→Middleware→Memory (checkpointer)→Integrations (MCP)→Guardrails

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.

yesloopno tool call → stopquestionmodelthoughttool call?toolactionobservationappended to messagesfinal answer
The agent loop as create_agent runs it: think, act, observe, repeat. The loop ends when the model replies without a tool call, or when the step limit is reached.

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"]
The whole ReAct loop in LangChain: a model, a list of tools and a system prompt. The returned messages hold every tool call and tool result of the run.

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.

ThoughtI need the weather in both cities.
Actionget_weather("Hanoi")
Observation31 °C, humid
Actionget_weather("Da Lat")
Observation19 °C, clear
AnswerDa Lat is 12 °C cooler.
A ReAct run: the model alternates short reasoning, a tool call, and reading the result, until it can answer. Each observation is appended to the context before the next step.

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

Check yourself · 1 of 3

In the ReAct loop, who actually runs the tool?

Check yourself · 2 of 3

Why does the transcript keep growing during a run?

Check yourself · 3 of 3

What stops the loop?

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”.
All books
Labstep the loop
How to read this
  • The log shows the run so far: thoughts, actions (tool calls) and observations (tool results), with the two available tools listed at the top.
  • At some steps you are asked to predict what the agent should do next before it is revealed; Back and Next move through the run.
tools: get_weather(city) · compare(a, b)