Skip to main content
AI Agents & Automation

⏱ About 15 min15 XP

Trace the Loop

Knowing the stages of the agent loop is one thing — watching them play out in a real task is another. In this lesson, you will slow everything down and trace every single step an agent takes from the moment it receives a goal to the moment it delivers its final answer. Tracing is one of the most important skills in AI agent work: it is how designers debug failures, verify correct behavior, and build intuition for how intelligent systems actually operate.

The Task: Plan a School Science Fair Project

Here is the scenario you will trace. A student types this message to a research agent: 'Help me plan a science fair project about renewable energy. I need a hypothesis, three experiments I could do at home, and a list of materials for the first experiment.' The agent has access to two tools: web_search, which searches the internet and returns a list of results, and produce_answer, which writes the final response to the user. The agent has a maximum of eight loop cycles before it must deliver whatever it has found. Walk through what the agent does, step by step.

Why Trace?

Tracing means following an agent's loop cycle by cycle, labeling each stage: what was perceived, what was reasoned, what was done, what was observed. A trace makes the invisible visible and reveals exactly where and why the agent succeeds or fails.

A Completed Trace: Cycles One and Two

Read this worked example of the first two loop cycles for our science fair agent. Notice how each stage connects to the next. Cycle 1. Perceive: The agent reads the user's full message. It identifies three separate deliverables: (1) a hypothesis, (2) three at-home experiments, (3) a materials list for the first experiment. Think: 'I have three distinct sub-goals. I should address them in order. First I need background knowledge on renewable energy experiments suitable for students. I do not want to fabricate experiments — I should search for verified ideas. Action plan: search for home renewable energy experiments for students.' Act: web_search('renewable energy experiments for students at home') Observe: The search returns a page listing solar-powered water heater experiments, wind turbine models made from recycled materials, and a simple hydroelectric generator using a water bottle. Cycle 2. Perceive: The agent sees the search results alongside the original user goal. It notes that all three ideas from the search are genuinely performable at home with common materials. Think: 'I now have three strong experiment candidates. I will select these three and then formulate a hypothesis that fits them. The common thread is converting a natural energy source into a measurable output. My hypothesis: If the surface area exposed to sunlight increases, then the output of a solar-powered device will increase proportionally. I still need to detail materials for experiment one — I can do that from memory for a solar water heater since this is standard equipment. I will produce the full answer now.' Act: produce_answer(...) Observe: The answer is delivered to the user. No further cycles are needed — goal satisfied.

Noticing the Connections

In a trace, every Observe feeds into the next Perceive. Every Perceive shapes the Think. Every Think justifies the Act. If any connection is missing or broken, the trace reveals it immediately.

What Makes a Good Trace?

A good trace has four qualities. First, every stage is labeled — you can tell at a glance whether you are reading a Perceive, Think, Act, or Observe step. Second, the reasoning in the Think stage explicitly explains why the chosen action was selected, not just what the agent will do. Third, the Observe stage honestly records what the action returned — including failures or surprises, not just successes. Fourth, the connections between stages are clear: you can see why the observation led to the next perception and why that perception led to the next thought. A thin trace — one that skips reasoning or vaguely labels stages — is not useful for debugging or verification. The detail is the point.

In a loop trace, what is the main job of the Think stage?

Why is tracing an agent's loop such a valuable skill?

Full Loop Trace Challenge

  1. You are tracing a research agent handling a new task. The agent has two tools: web_search and produce_answer.
  2. The user's message: 'I want to start a small vegetable garden on my apartment balcony. What vegetables grow best in containers, and what size pot do I need for each one?'
  3. Your trace challenge has five parts:
  4. Part 1 — PLAN: Before you write the trace, plan the agent's strategy. How many loop cycles do you expect it to need? What sub-goals must it satisfy? Write this out before you start the trace.
  5. Part 2 — CYCLE 1 TRACE: Write the full first loop cycle with all four stages labeled. In the Think stage, include at least three sentences of reasoning. In the Observe stage, invent a realistic web search result (four to six lines of plausible information about container vegetables).
  6. Part 3 — CYCLE 2 TRACE: Write the full second loop cycle. This cycle should build directly on what was observed in Cycle 1. The Think stage should reference specific information from Cycle 1's Observation.
  7. Part 4 — STOPPING CONDITION: Write the final Act of the last cycle. Is this a produce_answer action? Write out the first two sentences of what the agent's answer would say.
  8. Part 5 — REFLECTION: After completing your trace, answer these questions in writing: (a) At which stage did the agent's reasoning do the most important work? (b) If the web_search in Cycle 1 had returned no useful results, how would the trace change? (c) What would you add to this agent's toolkit to make it more capable for gardening questions?