DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

AI Agents Explained: the Thought-Action-Observation Loop

A chatbot answers in one shot. An AI agent runs in a loop, uses tools, and acts — Thought → Action → Observation → repeat — until the job's done. Watch one solve a multi-step task by calling a calculator and a search.

🤖 Run the agent: https://dev48v.infy.uk/ai/days/day11-agents.html

Agent = LLM + tools + a loop

You describe tools to the model (name, purpose, arguments). It can't divide big numbers reliably or know today's data — but it CAN decide "call the calculator with this expression". Tools cover the model's weak spots.

The ReAct loop

while (true) {
  const step = await llm(history);            // model emits a Thought + Action
  if (step.type === "answer") return step.text;
  const result = tools[step.tool](step.args); // run the tool
  history += `Observation: ${result}`;         // feed the real result back
}
Enter fullscreen mode Exit fullscreen mode

The model writes a Thought (plan), emits an Action (tool + args), your code runs it and returns an Observation, which goes back into context. Then it thinks again.

Why it beats one-shot

Each observation is REAL, fed back before the next decision — so it's not guessing the tip amount, it sees 126 from the calculator. And it plans the steps itself: "population of France's capital, doubled" becomes search → then calculator, chained because the model worked out the dependency.

Power needs guardrails

Cap iterations (no infinite loops), validate tool inputs, gate risky actions (email, payments) behind approval. Autonomy is the point; limits make it safe.

Run a task and watch the Thought→Action→Observation trace build.

Top comments (0)