🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

The ReAct Loop, Built by Hand

Build a real Thought/Action/Observation loop skeleton and understand why the step cap is a safety feature, not an arbitrary limit.

Narrated Video Summary
data-composition-id="aiagentsmasterclass-module1_lesson2"1280×720 @ 30fps3 clips0:48 total

Thought, Action, Observation

ReAct is the pattern behind most real agent loops: at each step, the model reasons about what to do next (Thought), names one real action to take (Action), and your code executes it and feeds the real outcome back in (Observation) — repeating until the model produces a Final Answer instead of another action.

Thought: I should check this ticket's priority first.
Action: check_priority("...")
Observation: high
Thought: High priority — no need to look anything else up.
Final Answer: escalate immediately

One Tool Wired, a Whole Belt to Go

The loop can already run a tool and stop on a final answer — but it only knows about one tool so far. Next lesson: designing TriageAgent's full tool belt, the real actions it needs for this specific job.

/* Next: Designing the Agent's Tool Belt */
0:00 / 0:48
Scene 1 / 3 — Thought, Action, Observation
Total XP: 0|💻 aiagentsmasterclass XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

The ReAct Loop

Reason, act, observe, repeat.

Quick Quiz //

What is the purpose of grounding each agent step in a real observation rather than a planned assumption?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Strip away the framework and an agent loop is a small, understandable piece of control flow — not magic.

1Why ReAct Specifically

ReAct (Reason + Act) interleaves the model's reasoning with real actions and their real observed results, rather than asking the model to plan every step upfront in one shot. Grounding each subsequent decision in a real observation — not a guess about what an action probably returned — is what keeps a multi-step agent from compounding an early wrong assumption through the rest of its run.

2The Step Cap Is a Safety Feature, Not an Afterthought

Nothing guarantees a model reliably produces a clean, parseable Final Answer every time — a bug in your parsing, an ambiguous prompt, or an unusual model response can all leave the loop with no exit condition. A hard max_steps cap is what turns 'this could theoretically run forever' into 'this fails safely and visibly after a bounded number of steps.'

3Step-by-Step Breakdown

Thought, Action, Observation. ReAct is the pattern behind most real agent loops: at each step, the model reasons about what to do next (Thought), names one real action to take (Action), and your code executes it and feeds the real outcome back in (Observation) — repeating until the model produces a Final Answer instead of another action.

Build the Real Loop Skeleton. fake_model_step stands in for a real model call, scripted to return one action step and then a final answer. Finish run_agent so that when it sees an Action line (not yet a Final Answer), it actually extracts the tool name and calls the real tool with it.

Why does run_agent cap its loop at max_steps instead of looping forever until it sees a Final Answer?

  • A model might never produce a clean Final Answer line — a hard step cap prevents a stuck agent from looping indefinitely and burning unbounded time and API cost.
  • Python's for loop syntax physically cannot run without an explicit upper bound.

One Tool Wired, a Whole Belt to Go. The loop can already run a tool and stop on a final answer — but it only knows about one tool so far. Next lesson: designing TriageAgent's full tool belt, the real actions it needs for this specific job.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Surface the Step Count When an Agent Hits Its Cap

If an agent stops because it hit max_steps rather than reaching a real answer, report that distinctly ('stopped after 5 steps without a final answer') instead of presenting it identically to a successful completion.

"error: max steps reached" // distinct from a real answer

SEO Implications

  • 1

    Target 'ReAct agent loop from scratch' and 'AI agent max steps limit' separately

    Developers building their first loop search for the overall pattern and the specific safety-limit practice as distinct concerns.

Best Practices

Log Every Thought, Action, and Observation, Not Just the Final Answer

When an agent produces a wrong final answer, the intermediate steps are almost always where the actual reasoning error happened — without logging them, debugging becomes guesswork.

Frequent Bugs

THE BUG

Parsing the model's action line with a fragile, overly strict string match.

THE FIX

A real model's phrasing varies more than a scripted example — production agents typically use structured tool-calling (the exact mechanism covered in this platform's MCP Masterclass) instead of parsing free-form text, precisely to avoid this fragility.

Real-World Examples

Support Triage Loop

TriageAgent's real loop: check priority, decide whether to retrieve a policy document, decide whether to escalate — each step's real observation determines whether the next step is even needed, which a single completion can't replicate.

if priority == "high": retrieve_policy(); escalate()

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Hardcoding sensitive credentials

// Wrong const API_KEY = 'sk-123456789'; // Correct const API_KEY = process.env.API_KEY;

The Solution //

Never hardcode API keys, passwords, or secrets in your source code. Use environment variables (.env files) to keep them secure and out of version control.

Lesson Glossary

[01]ReAct

An agent pattern interleaving Reasoning (Thought), real Actions, and real Observations of their results in a loop.

Code Preview
Thought -> Action -> Observation -> repeat

[02]max_steps

A hard cap on how many loop iterations an agent may take before stopping, preventing an unbounded or stuck run.

Code Preview
for step in range(max_steps): ...

Continue Learning