PTENES
MODULE 1-2

🔄 The actual execution cycle

Read, decide, execute, observe, repeat. The step-by-step loop that turns a written recipe into completed work.

6
Topics
22
Minutes
Beginner
Level
Theory
Type
0 of 60%
1

🔄 The 5-step cycle

New here? O execution cycle it’s the sequence the agent repeats several times within a session: read the workflow step, choose a tool, run it, review the result, and decide what to do next. It isn’t a single step—it’s a loop (loop, repetition: the same sequence running over and over) that continues until the task is complete.

In module 1.1, you saw the kitchen analogy: recipe (workflow), cook (agent), utensils (tools). It was static there, like a photo of the finished dish. This module is the movie — the cook reading the next step, picking up the right knife, cutting, tasting the result, and only then turning the page. It’s this “read → decide → act → taste → repeat” that turns a static recipe into real work — a finished report, a sent newsletter, an organized batch of invoices.

Each complete loop iteration is called a iteration. A simple task takes 3 or 4; organizing a month's worth of receipts can take dozens — the agent decides on each pass whether it's done.

1

Read the next workflow step

The agent identifies where it left off in the process and what comes next.

2

Decide which tool to call

From the available tools, it chooses the one that handles that specific step.

3

Run

The tool actually runs — the file is read, the command is executed.

4

Observe the result

The agent reads what came back: did it work? Was there an error? Does the number match?

5

Decide the next step

Moves to the next workflow item, tries again, or says it needs help.

1

Read

2

Decide tool

3

Run

4

Observe

5

Decide next

Caption: this timeline shows ONE iteration of the loop — one complete cycle. Step 5 points back to step 1 (not shown here) because the cycle starts over until the workflow is finished.

2

📖 Reading the workflow: where the agent “is”

Each time the cycle starts over, the agent needs to know "where I am in the recipe." In a Claude Code session, this usually happens within a single conversation: the conversation history acts as short-term memory, remembering what has already been done. In a workflow saved to a file (which we’ll practice in module 1.5), the current step can be explicit in the text itself or inferred from what’s already on disk — for example, if the file "relatorio-semana.md" already exists, step 4 is complete.

New here? Context it’s everything the agent “sees” when making a decision: the conversation so far, the files it has already read, CLAUDE.md, and the results of tools it has already run. Think of context as the cook’s worktable—only what’s on it is available at that moment; an ingredient in a closed cabinet (a file never opened) simply doesn’t exist to the cook. This context has a cost, measured in tokens — chunks of text the model processes each turn. The more stuff "on the table," the more tokens are spent just looking at everything before deciding.

Example: in a “reply to customer emails” workflow, the agent reads the next email at each iteration (the “where am I”), decides whether to reply with a template or escalate to a human, executes, and observes whether the email was sent successfully. The “state” is simply how many emails have been processed.

🔍 Inside

  • State: "where I am in the task" — what’s already been done and what’s left.
  • Context: everything the agent "sees" at the moment—conversation, files, CLAUDE.md.
  • Tokens: the cost of what’s in the context — more text read/generated means more tokens spent.
  • Where it’s recorded: in the conversation, or on disk, in files that the workflow checks on every cycle.

💡 Practical Tip

Task too slow or expensive? Look at what goes into the context on each run — it’s common for the agent to reread the same huge file every iteration when it only needed to read it once.

3

🧭 Decide which tool to call

This is the "smartest" part of the cycle: given the current step and the list of available tools, the agent chooses the one that best solves it. If the step is "sum by category," it may choose to run a calculation script instead of trying to add everything manually. If the step is "publish on the site," it chooses the deploy tool (covered in Track 4), not an email tool.

This choice depends directly on which tools have been made available to the agent — that’s why Tracks 2 (MCP) and 3 (Skills) in this course teach you how to add and connect tools: the clearer the menu of options, the better the choice.

Another example: in a “weekly sales report,” the step “add up revenue by salesperson” has two candidate tools — a script that adds it up, or manual formulas. A well-configured agent chooses the script, which is faster and deterministic — if it’s available. If not, it adapts, which brings us to the most common mistake.

⚠️ Common errors at this stage

  • Vague recipe: "organize the invoices" with no criteria — the agent chooses on its own, and it might not be what you wanted.
  • Missing tool key: many tools require one API key (password that authorizes its use). Without it, the tool won’t run.
  • The agent "invents" a step (hallucinates): without a clear tool, it sometimes describes an action as completed without actually calling the tool — that’s why checking the result (topic 4) matters as much as choosing the tool.
Step
What to do
Menu
Available tools
Choice
The most suitable
Trigger
Real execution

✓ Well-written step

  • ✓"Add up the revenue in the vendas.csv spreadsheet, grouped by salesperson"
  • ✓Says which tool to prefer when there is more than one option

✗ Vague step

  • ✗"Organize the sales" — organize how? into what?
  • ✗Let the agent guess the right tool
4

👀 Run it and observe the result

After running it, the agent doesn’t blindly move on to the next step — it reads what the tool returned. A command can return an error, an empty file, a number outside the expected range, or exactly what it should. Reading the result triggers self-correction, covered in more depth in module 1.3.

Choose a tool current step Runs for real Observe did it work? Next step or fix it and try again if the result was wrong, it goes back and tries again

Caption: the loop runs from left to right at each workflow step; the dotted return arrow shows self-correction — the agent repeating a step that didn’t go as expected.

"Observing" isn't optional or automatic — it's an active decision to stop and read what came back before continuing. In the newsletter example (module 1.5), after "send email" the agent checks whether it got "success" or an error code. With invoices, after moving a file it checks whether the file is actually in the folder — it doesn't assume it worked just because the command "ran without crashing".

This check has three possible outcomes: (1) it matches expectations, so move on; (2) there’s a clear error, so the agent tries to fix it or lets you know; or (3) the result is ambiguous — neither right nor clearly wrong — and that’s where the deeper judgment covered in module 1.3 on self-correction comes in.

📋 Copy and run: a mini-workflow to see the cycle live

Objective: see the 5 steps in action on a small task, without an external tool.

1. Leia a pasta atual e liste os arquivos .txt que existem.
2. Para cada arquivo .txt encontrado, conte quantas linhas ele tem.
3. Crie um arquivo resumo.md com o nome de cada arquivo e sua contagem de linhas.
4. Confira se resumo.md foi criado e se o número de linhas listadas bate com os arquivos originais.

How to verify: open resumo.md afterward. If each line matches a real file and its correct count, all 5 steps ran correctly for each file processed.

5

🔁 The loop continues until it finishes (or gets stuck)

The cycle repeats step by step until the workflow is finished. On each pass, the agent also decides whether it needs to repeat the current step (for example, if a command failed for a temporary reason) or stop and ask you for help (for example, if a password only you have is missing). This second case—stopping and letting you know—is just as important as execution: a good agent knows when not to proceed on its own.

New here? This decision point — deciding whether to continue, repeat, or stop based on the observed result — is what we call reasoning of the agent. Without it, the cycle would just be a blind sequence of commands, unable to react to surprises.

In the email example: a healthy loop retries “send” only if the failure is temporary, and stops after 2-3 attempts, escalating to you. A stuck loop repeats the same error dozens of times without getting a new result.

✓ Healthy loop

  • ✓Each step moves the task forward for real
  • ✓Temporary errors are fixed in 1-2 attempts
  • ✓Ends with a result or a clear request for help

✗ Stuck loop

  • ✗Repeats the same mistake several times without changing strategy
  • ✗Consumes tokens (cost) without making progress
  • ✗Sign that the workflow or permission needs adjustment — module 1.4
6

🏁 When the cycle truly ends

The cycle ends in one of three ways: (1) the workflow reached the last step and everything checked out—success; (2) the agent identified that something only you can provide is missing and stopped to ask; or (3) it got stuck repeating and it’s time for you to step in. Recognizing which of the three is happening is the central topic of module 1.4.

Let’s reinforce the difference between endings 2 and 3 — they look similar at first, and both end without the finished result. In ending 2 (pause for a decision), the agent knows what’s missing and asks a specific question, such as "I need the Gmail API key — where can I find it?" In ending 3 (stuck), it doesn’t know what else to try and repeats something that has already proven not to work — the solution is for you to adjust the workflow, not answer.

In the invoice example: success means all invoices are in the right folders with a summary by category; pausing for a decision means the agent finds an invoice with no clear category and asks where it belongs; getting stuck means repeatedly trying to open a corrupted PDF without success — time for you to inspect the file manually.

💡 Practical Tip

When reviewing an agent’s work, always ask: “Did it finish successfully, pause, or get stuck?” If you can’t tell from its last message alone, that’s a sign the workflow should end with a clearer summary of what happened.

🎯 Main Concept

  • •Success: the workflow completed with the expected result.
  • •Decision pause: it stopped to ask you something only you know.
  • •Stuck: repeats without making progress — time to review the workflow.

Quick check (optional): what does the agent do after running a tool?

✅ Module Summary

✓
5 steps: read → choose tool → execute → observe → decide what to do next.
✓
State and context: The agent knows “where it is” from the conversation and the existing files.
✓
Healthy loop × stuck loop: repeating while making corrections is normal; repeating without making changes is a warning sign.
✓
Three possible endings: success, pausing to ask, or getting stuck.

Next module:

1.3 — Self-correction: truth and marketing