LearnFrenzy
Sign in Take a free test

How Agents Work · Lesson 2 of 6

The Agent Loop: Planning, ReAct and Knowing When to Stop

Every agent runs the same cycle: think about the next step, act with a tool, observe the result, repeat. See the loop step by step with a real example, and learn why stopping conditions matter as much as intelligence.

10 min read Beginner Updated Nov 9, 2026 Reviewed by Saurabh Samir

If you remember only one thing about agents, remember the loop. Every agent — a coding assistant, a research tool, a support bot — is a language model sitting inside a simple cycle that your code runs over and over.

The cycle: think → act → observe

  1. Think. The model looks at the goal and everything that has happened so far, and decides the next step.
  2. Act. If it needs information or an action, it asks for a tool: "call search with this query".
  3. Observe. Your code runs the tool and adds the result to the conversation.
  4. Repeat — until the model says "I have the final answer" or a limit is reached.

This pattern was named ReAct (Reason + Act) in a 2022 research paper, and almost every agent framework is a variation of it.

Watch one run, step by step

Goal: "My train to Pune leaves at 6:40 pm. If the cab takes 35 minutes and I want to reach 20 minutes early, when should I book it? Also tell me the weather in Pune tonight."

Thought:     I need the departure time minus 20 min minus 35 min. Then the weather.
Action:      calculator("18:40 - 00:20 - 00:35")
Observation: 17:45
Thought:     Booking time is 5:45 pm. Now the weather.
Action:      get_weather(city="Pune", when="tonight")
Observation: 24°C, light rain expected after 8 pm
Thought:     I have everything I need.
Final:       Book the cab by 5:45 pm. Pune will be around 24°C with light
             rain after 8 pm — carry an umbrella.

Notice what the model did not do: it did not try to do time arithmetic in its head (where LLMs often slip), and it did not guess the weather. It used tools for facts and calculations, and used its own strength — language and reasoning — to decide and explain.

Key idea: the model never runs the tool itself. It only asks. Your code decides whether to run it, runs it and reports back. That gap is where you put every safety check.

Planning: think first, then loop

For longer tasks, a single step at a time can wander. Two common fixes:

  • Plan-then-execute. First ask the model for a numbered plan, then work through it step by step, updating the plan if something unexpected happens. Good for research and multi-file coding tasks.
  • Task decomposition. Break a big goal into smaller sub-goals, sometimes handled by separate, simpler agents ("researcher", "writer", "reviewer"). Useful, but every extra agent adds cost and new ways to fail — start with one.

Stopping conditions: the most underrated part

A loop without limits is how agents burn money and time. Real agents always have several ways to stop:

  • Goal reached — the model returns a final answer instead of another tool call.
  • Step limit — for example, at most 10 tool calls. Beyond that, stop and report what was done.
  • Time or cost budget — stop after 2 minutes or a set number of tokens.
  • Repeated action — the same tool with the same input three times usually means it is stuck.
  • Needs a human — missing information, a risky action or low confidence: pause and ask.

Real-world failure: an agent searches, does not like the results, searches again with almost the same words, and repeats forever. A step limit and a "same call repeated" check turn an expensive bug into a polite "I could not find this — here is what I tried".

Why agents make mistakes

  • Error compounding. If each step is 95% reliable, a 10-step task succeeds only about 60% of the time (0.9510 ≈ 0.6). Fewer, better steps beat many clever ones.
  • Bad observations. A tool returns an error or messy text and the model misreads it. Return short, clear tool results.
  • Lost context. In very long runs, early instructions get crowded out. Restate the goal and constraints in the system prompt.

Try it yourself: Take the goal "Plan my study schedule for the 5 days before my end-semester exams, using my timetable file." Write the Thought / Action / Observation trace you would expect, with at most 6 actions. Then list two stopping conditions you would add.

What to learn next

You have seen the model "ask for a tool". Next, we look at exactly how that request is made — tool calling, JSON schemas and the Model Context Protocol.

Common interview questions

Try answering each one aloud before you open it.

1What is the ReAct pattern?

ReAct (Reason + Act) is an agent pattern where the model alternates between reasoning about what to do next and acting by calling a tool, then observes the tool result and repeats until it can give a final answer.

2Why do agents need stopping conditions?

Without them an agent can loop forever, repeat failing actions and run up cost. Typical limits are a maximum number of steps, a time or token budget, detection of repeated identical calls, and escalation to a human.

3What is error compounding in agents?

Small per-step error rates multiply over many steps. At 95% reliability per step, a 10-step task succeeds only about 60% of the time, which is why fewer, more reliable steps are better.

Practise this

Build something with it

FAQs

Do I have to show the "Thought" text to users?

No. Many systems keep the reasoning internal and show only actions and the final answer. Logging it for developers is very useful for debugging.

Is a multi-agent system better than one agent?

Not by default. Multiple agents help when sub-tasks are truly different, but they add coordination cost and failure points. Most problems are solved better by one well-designed agent with good tools.

References & further reading

Finished this lesson?

Mark it complete. Create a free account to save it on every device.

Next lesson

Search LearnFrenzy

Esc

Quick links