Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท Agent Concepts and Loops

The Perceive, Think, Act Loop (ReAct Style)

Understand the core agent loop in depth: observations, thoughts, actions, the ReAct pattern, and how to build a loop that logs thoughts and recovers from bad steps.

Every agent, however sophisticated, repeats the same cycle. It perceives the current situation, thinks about what to do, acts, and then perceives the result of that action. The names vary between textbooks (observe, orient, decide, act; or sense, plan, act), but the structure is constant. In this lesson you take apart each phase, meet the well-known ReAct pattern that interleaves reasoning with tool use, and build a loop that records its thoughts.

The four phases

flowchart LR
  P["Perceive<br/>build the prompt"] --> T["Think<br/>model reasons"]
  T --> A["Act<br/>run the tool"]
  A --> O["Observe<br/>capture result"]
  O --> P

The cycle repeats. The observation of one round becomes part of the perception of the next.

Perceive is not just reading. It is deciding what the model gets to see. You assemble the system prompt, the goal, the relevant history and the latest observation. Everything the model needs must be in this text, and everything that is noise should be left out.

Think is the model call. In many designs the model writes a short piece of reasoning before choosing an action. That reasoning is useful twice: it improves the choice, and it appears in your logs so a human can audit it.

Act is your code, not the model's. The model proposes. Your program validates the proposal, runs the tool and catches errors.

Observe turns the outcome into text: the tool's return value, an error message, or a note that nothing happened. It is appended to the history.

ReAct: reasoning and acting together

The ReAct pattern asks the model to alternate three kinds of text in its own output: a Thought (what it is considering), an Action (a tool and its input) and then it receives an Observation from the program. A transcript looks like this:

TEXT
Thought: I need the price of the item first.
Action: lookup_price["lamp"]
Observation: 24.00
Thought: Now add 8 percent tax.
Action: calculate["24.00 * 1.08"]
Observation: 25.92
Thought: I have the answer.
Final: The lamp costs 25.92 with tax.

The program's job is to parse each model output, run the Action, append the Observation, and stop at Final. The next example implements exactly that with a scripted stand-in model, so you can see the parsing and the loop work on real text.

Python Stand-in: a ReAct text loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at what the program does with text: a regular expression finds the action, a dictionary finds the tool, and a try/except converts any failure into an observation. Modern APIs move this from free text to structured tool-call objects (see what are tools), but the roles are the same.

The calculate tool above uses eval behind a strict character check, which is acceptable for a classroom stand-in. In real systems never pass model-written text to eval, exec or a shell without a sandbox and a whitelist.

Should the model show its thinking?

Short reasoning before an action helps on multi-step problems and gives you an audit trail. It also costs tokens and can be wrong in ways that look convincing, so treat the written thought as a debugging aid, not as proof. Many providers now offer built-in "thinking" that is returned separately from the visible output. Whether you use that or ask for Thought: lines, record it in the log, and keep it out of anything shown to end users unless you mean to.

Perception is design

The biggest lever you have is what goes into the model's view each turn. Compare two ways to present a 500-line tool result: paste everything, or paste the first 20 lines plus a note that more exists and how to request it. The second keeps the context small and the model focused. The next example shows a perception helper that truncates long observations and always labels them.

Python Shape observations before the model sees them

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Loop invariants worth keeping

A well-built loop keeps a few promises every iteration: it terminates (limit or final), it logs the thought, action and observation, it never executes unvalidated output, and it appends something after every action so the model is never blind to what it did. When you debug an agent that behaves oddly, check these four before blaming the model.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the perceive, think, act, observe cycle, which part is performed by your own code rather than the model?

    The model only proposes an action. Your program validates it, runs the tool and captures the result.
  2. 2. What are the three kinds of text in a ReAct transcript?

    Thought and Action come from the model; Observation is added by the program after running the Action.
  3. 3. A tool raises an exception. What should the loop do?

    Reporting the error as an observation lets the model adapt, for example by changing the arguments.
  4. 4. Why truncate long tool results before they reach the model?

    Large dumps waste tokens and bury the useful part. A labelled truncation keeps the model informed.

Summary

  • The loop is perceive, think, act, observe, repeated until a final answer.
  • Perception means choosing what the model sees; observations are text you shape.
  • ReAct interleaves Thought, Action and Observation; the program parses and executes.
  • Failures become observations. Never run unvalidated model output unsandboxed.
  • Keep invariants: terminate, log, validate, and always append a result.

Next: Agent State and Messages