ReAct agent: thought, tool call, observation, with your own question
An agent is a loop: think, call a tool, read the result, repeat until the answer is ready. Type your own question; the tools really run, and you can see every call as the JSON a model would send.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
Each step shows the agent's reasoning, the tool call as JSON and the observation that comes back. On the right are the tool schemas and the final answer. The decision of which tool to call is made by rules standing in for the language model; the tools themselves compute real results.
Things to try
- Ask 'What is 15% of 240 plus 12?'
- Ask 'Convert 10 km to miles'.
- Ask 'How many days after delivery can I return an item, times 2?': two tools, one after the other.
- Ask something outside the shop: the agent says it does not know.
How it works in real systems
OpenAI, Anthropic and open models expose this as tool or function calling: the model returns a structured call, your code runs it and sends the result back. LangGraph's ToolNode and create_react_agent implement the loop.
The tools in this simulation compute real results; the choice of which tool to call is made by rules that stand in for the language model, so you can try your own input without an API key.
Code lab
The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.
Step 1 · Describe the tools as JSON Schema
import json, math
from openai import OpenAI
client = OpenAI()
tools = [
{"type": "function", "function": {"name": "calculator", "description": "Evaluate an arithmetic expression.",
"parameters": {"type": "object", "properties": {"expression": {"type": "string"}}, "required": ["expression"]}}},
{"type": "function", "function": {"name": "convert_units", "description": "Convert a value between km and mi.",
"parameters": {"type": "object", "properties": {"value": {"type": "number"}, "from": {"type": "string"}, "to": {"type": "string"}}, "required": ["value", "from", "to"]}}},
]
def calculator(expression): # never eval() model output: parse it safely
import ast, operator as op
ops = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Div: op.truediv, ast.Pow: op.pow, ast.USub: op.neg}
def ev(n):
if isinstance(n, ast.Constant): return n.value
if isinstance(n, ast.BinOp): return ops[type(n.op)](ev(n.left), ev(n.right))
if isinstance(n, ast.UnaryOp): return ops[type(n.op)](ev(n.operand))
raise ValueError("unsupported expression")
return ev(ast.parse(expression, mode="eval").body)
def convert_units(value, **kw): return round(value * (0.621371 if kw["from"] == "km" else 1.609344), 4)
REGISTRY = {"calculator": calculator, "convert_units": convert_units}
Step 2 · The ReAct loop
Call the model; if it asks for tools, run them and append the results as tool messages; repeat until it answers.
def run(question, max_steps=6):
messages = [{"role": "system", "content": "Use tools for any arithmetic or conversion."}, {"role": "user", "content": question}]
for _ in range(max_steps):
msg = client.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools).choices[0].message
messages.append(msg)
if not msg.tool_calls: return msg.content
for tc in msg.tool_calls:
args = json.loads(tc.function.arguments)
try: result = REGISTRY[tc.function.name](**args)
except Exception as e: result = f"ERROR: {e}"
print(tc.function.name, args, "→", result)
messages.append({"role": "tool", "tool_call_id": tc.id, "content": str(result)})
return "Stopped: step limit reached."
print(run("Convert 42.195 km to miles and add 2"))
Step 3 · Make it safe
Validate arguments against the schema, cap steps and tokens, time out slow tools, log every call, and require human approval for tools that change something (payments, emails, deletions).
More simulations
← Mixture of experts · All AI Simulations · Multi-agent team →