AI Agents and the ReAct Loop: Explained Visually

Updated 02 Nov 202610 min readLevel: Beginner1 interactive visuals

An AI agent is an LLM that can take actions, not just write text. You give it a goal; it decides which steps to take, calls tools (search, calculator, APIs, databases), reads the results and keeps going until the goal is done. The most popular way to build one is the ReAct pattern: the model alternates between Reasoning about what to do and Acting by calling a tool.

What you will learn
  • What an AI agent is, and how it differs from a chatbot
  • The ReAct loop: Thought, Action, Observation
  • How tool calling works under the hood
  • Agent patterns: planning, reflection, multi-agent and routing
  • MCP and popular agent frameworks
  • Guardrails: loops, costs, permissions and prompt injection
Contents
  1. Chatbot vs AI agent
  2. The building blocks of an agent
  3. The ReAct loop, step by step
  4. How tool calling works under the hood
  5. Common agent patterns
  6. MCP: a standard plug for tools
  7. Popular agent frameworks
  8. Keeping agents safe and reliable

Chatbot vs AI agent

Chatbot (plain LLM) AI agent
What it does Answers from what it already knows Plans, uses tools, checks results, acts
Steps per request One reply Many steps in a loop
Access to live data No Yes, through tools
Can change things No Yes: book, send, update, run code
Example "Explain what annual leave is" "Check my leave balance and apply for 2 days next Friday"
A new employee vs an assistant with access

A chatbot is like a smart new employee who can only answer from memory. An agent is like an assistant who has logins to the HR portal, the calendar and a calculator. When you ask a question, they look things up, do the maths and come back with an answer they have checked.

The building blocks of an agent

Block What it is Example
Model (brain) The LLM that reasons and decides the next step GPT-4o, Claude, Gemini, Llama
Tools Functions the model is allowed to call search_policy(), calculator(), send_email()
Memory What the agent remembers: the current scratchpad and long-term facts Previous steps, user preferences
Instructions The system prompt: role, rules, when to stop "Only answer HR questions. Never share salaries."
Loop / orchestrator Code that runs model → tool → model until done A while-loop, LangGraph, an agent SDK

The ReAct loop, step by step

ReAct was introduced by Yao et al. in 2022. The model writes a Thought (what it should do next), then an Action (a tool call). The tool's result comes back as an Observation. The loop repeats until the model writes a Final Answer.

Press Play to watch an HR agent answer a real question. The scratchpad on the right side of the loop shows exactly what the model sees.

Why the "Thought" step matters

Writing the reasoning down before acting helps the model choose the right tool and makes the agent easy to debug: you can read the scratchpad and see exactly where it went wrong. Many modern models do this reasoning internally, but the loop is the same.

How tool calling works under the hood

The LLM never runs code itself. Tool calling is a conversation between your code and the model:

  1. You describe the tools to the model: a name, a description and a JSON schema for the arguments.
  2. The model replies with a tool call instead of text, for example {"name": "calculator", "arguments": {"expression": "1.5 * 10 - 6"}}.
  3. Your code runs the function and sends the result back as a "tool" message.
  4. The model continues: it either calls another tool or writes the final answer.

A tool definition looks like this:

JSON
{
  "name": "policy_search",
  "description": "Search the HR policy handbook. Use for any question about leave, expenses or rules.",
  "parameters": {
    "type": "object",
    "properties": {
      "query": {"type": "string", "description": "What to search for, in plain English"}
    },
    "required": ["query"]
  }
}

And the agent loop itself is short. This version uses the OpenAI Python SDK; other providers work the same way.

PYTHON
import json
from openai import OpenAI

client = OpenAI()
TOOLS = {"policy_search": policy_search, "calculator": calculator}   # your Python functions

def run_agent(question, tool_specs, max_steps=6):
    messages = [
        {"role": "system", "content": "You are an HR assistant. Use tools. Never guess numbers."},
        {"role": "user", "content": question},
    ]
    for _ in range(max_steps):                       # guardrail: never loop forever
        reply = client.chat.completions.create(
            model="gpt-4o-mini", messages=messages, tools=tool_specs
        ).choices[0].message
        if not reply.tool_calls:                     # no tool needed: this is the final answer
            return reply.content
        messages.append(reply)
        for call in reply.tool_calls:                # Action -> run the real function
            args = json.loads(call.function.arguments)
            result = TOOLS[call.function.name](**args)
            messages.append({"role": "tool", "tool_call_id": call.id, "content": str(result)})  # Observation
    return "Sorry, I could not finish this request."
Write tool descriptions like documentation

The model chooses tools only from their names and descriptions. "Search the HR policy handbook. Use for any question about leave, expenses or rules." works far better than "search tool". Clear descriptions are the cheapest way to make an agent smarter.

Common agent patterns

Think, act, observe, repeat. Simple and flexible. Best for tasks with a few tool calls where the next step depends on the last result. This is what the visual above shows.

First write a full plan ("1. find policy, 2. get balance, 3. calculate, 4. draft reply"), then execute the steps, re-planning only when something fails. Uses fewer expensive model calls on long tasks and is easier to show to a user for approval.

After producing an answer, the agent (or a second "critic" model) reviews it against the requirements and improves it. Very useful for code generation, writing and anything with checkable rules.

A small, fast model classifies the request and sends it to the right specialist chain or tool: billing questions to the billing flow, technical questions to RAG over the docs. Cheap, predictable and often enough without a full agent.

Several agents with different roles work together, for example a supervisor that hands tasks to a researcher, a coder and a reviewer. Powerful for big tasks, but harder to debug and more expensive. Start with one agent and split only when it clearly helps.

MCP: a standard plug for tools

The Model Context Protocol (MCP), released by Anthropic in late 2024 and now supported by OpenAI, Google, Microsoft and most agent tools, is an open standard for connecting agents to tools and data. Instead of writing custom code for every app, a company builds one MCP server (for example, for Jira, a database or Google Drive), and any MCP-compatible agent can use it. Think of it as USB-C for AI tools.

Framework Best for
LangGraph Agents as graphs with explicit state, branches, retries and human approval steps
CrewAI Role-based multi-agent teams with simple setup
AutoGen / Microsoft Agent Framework Conversational multi-agent systems; Microsoft ecosystem
OpenAI Agents SDK Lightweight agents, handoffs and guardrails with OpenAI models
Claude Agent SDK Agents built on Claude, with tools, MCP and long-running tasks
Google ADK Agents on Gemini and Vertex AI
LlamaIndex Agents that focus on data and RAG

Keeping agents safe and reliable

Risk What can happen Guardrail
Infinite loops The agent keeps calling tools and burns money Maximum steps, timeouts and a budget limit
Wrong or risky actions It sends an email or deletes a record by mistake Read-only tools by default; human approval before anything irreversible
Prompt injection A web page or document says "ignore your instructions and…" Treat tool results as data, not instructions; limit what tools can do
Too many permissions A leaked agent can access everything Least-privilege API keys per tool and per user
Silent failures A tool errors and the agent invents a result Return clear error messages; log every step; evaluate with test tasks
When not to use an agent

If the steps are always the same (for example, "extract fields from an invoice, then save them"), a fixed pipeline is cheaper, faster and more reliable than an agent. Use an agent only when the next step genuinely depends on what was found.

Key takeaways
  • An AI agent = LLM + tools + memory + a loop. It decides the steps itself.
  • ReAct alternates Thought → Action (tool call) → Observation until it can give a Final Answer.
  • The model only requests tool calls; your code runs them and returns the results.
  • Clear tool descriptions and schemas make agents much more accurate.
  • Patterns: ReAct, plan-and-execute, reflection, router and multi-agent. MCP standardises tool access.
  • Always add guardrails: step limits, least privilege, human approval and defence against prompt injection.

Test yourself

Score: 0 / 5

1. What makes an AI agent different from a normal chatbot?

Agents combine an LLM with tools and a loop, so they can fetch live data and take actions.

2. In the ReAct pattern, what is an "Observation"?

Thought is the reasoning, Action is the tool call, Observation is what the tool returned.

3. Who actually executes a tool call?

The model outputs a structured request. The orchestrator runs the real function and sends the result back.

4. What is the simplest guardrail against an agent looping forever?

A hard cap on iterations stops runaway loops and runaway costs.

5. What problem does MCP solve?

MCP is an open protocol, so one MCP server for a tool can be used by any compatible agent.

Frequently asked questions

What is agentic AI?

Agentic AI means AI systems that pursue a goal with some independence: they plan, use tools, check results and take actions over several steps, instead of producing a single reply.

What is the difference between RAG and an agent?

RAG is a fixed pipeline: retrieve, then answer. An agent decides for itself what to do next, and searching documents can be one of its tools. "Agentic RAG" combines them: the agent decides when to search, what to search for and whether the results are good enough.

Do agents need a vector database?

No. Agents need tools. A vector database is only needed if one of the tools searches documents by meaning. Many agents use APIs, SQL databases or web search instead.

What does ReAct stand for?

Reasoning and Acting. The model interleaves reasoning steps ("I need the leave policy") with actions (calling a search tool), using each observation to decide the next step.

Are multi-agent systems better than one agent?

Not automatically. Multiple agents add cost, latency and coordination problems. Start with one well-designed agent with good tools, and split into specialists only when tasks clearly need different skills or permissions.

Prefer watching?

Real interview walkthroughs and AI explained simply on our YouTube channel.

Subscribe on YouTube