PTENES
MODULE 1.2

🔄 The Complete Cycle: Plan → Build → Troubleshoot → Optimize

The four-phase cycle, demonstrated end to end in an email support agent with RAG. You learn the permission modes, see the AI plan, build, debug, and optimize—and discover why green ≠ correct.

8
Topics
~60
Minutes
Basic
Level
Build
Type
Module progress 0% · 0 of 8

Every conversational build follows the same four-phase cycle. You describe the result, the AI plans and builds (build); when something breaks, send the error back to it debug (troubleshoot) and, finally, ask for improvements to optimize. The diagram below shows the loop—and that it always returns to the plan.

1 · Plan research + plan 2 · Build creates + validates nodes 3 · Troubleshoot error → fix 4 · Optimize improvements in PT the result feeds into a new plan—the cycle is iterative the assistant coordinates everything via MCP server → n8n

Illustrative diagram — conceptual recreation of the cycle, not a real screenshot.

1

🔐 Permission modes & Plan Mode

What it is: before the AI does anything, you choose the permission mode — how much autonomy it has to edit files and run commands. The modes range from "ask before everything" to "skip all approvals". The Plan Mode is the mode where it only plans: it researches, asks questions, and proposes, without changing anything until you approve.

🧭 Why learn

The right mode balances speed and safety. Starting in Plan Mode ensures you always see the plan before the build; Bypass is faster, but you can’t see what the AI is doing—use it carefully, and never for potentially dangerous commands without understanding the risk.

✓ When to use each mode

  • ✓Plan Mode: whenever you’re about to start something new—just plan.
  • ✓Ask first: learns by observing each step.
  • ✓Automatic editing / Auto: when you already trust the workflow.

✗ Beware of the Bypass

  • ✗Bypass skips approval for potentially dangerous commands.
  • ✗You stop seeing what the AI is doing.
  • ✗Enable it only when you understand the build and want speed.

🎯 Practical tip

To enable Bypass, search for "bypass" in the settings (allow skipping permissions with care) and restart. But the golden rule still applies: always start in Plan Mode and only switch modes once you see the approved plan.

Key concepts

Plan Mode

Only plans, doesn’t make changes.

Ask first

Approve each edit.

Auto / Edit

More autonomy.

Bypass

Skips approval — be careful.

2

📋 Phase 1 — Plan

What it is: during the planning phase, the AI researches the relevant nodes and patterns, then clarifying questions (which columns to record, extra actions such as labels or notifications) and then proposes a plan for you to approve. Nothing is built before your approval.

🧭 Why learn

The plan is your checkpoint: it’s where you correct course before a single line exists. Answering clarifying questions well determines the quality of everything that follows—it’s worth investing more here than fixing things at the end.

Prompt in Plan Mode (illustrative example)
I need a workflow that monitors Gmail
for emails with "urgent" in the subject
and logs each one in a Google Sheets spreadsheet.

# the AI responds with:
- questions: which columns? label? notification?
- a plan with Gmail trigger → append row
- the list of required credentials

🎯 Practical tip

Non-determinism is normal: the same prompt can generate different node names, structures, and layouts each time. Variation isn’t an error—don’t compare your screen pixel by pixel with someone else’s.

Key concepts

Research

Finds nodes and patterns.

Questions

Clarify before acting.

Approval

You subscribe to the plan.

Variation

Non-deterministic output.

3

🏗️ Phase 2 — Build

What it is: once the plan is approved, the AI creates and validates the nodes in n8n, automatically fixing validation issues. In the example, it builds a Gmail trigger connected to a Google Sheets “append row” node. But there are things it doesn’t do by you.

✓ What the AI does

  • ✓Creates the nodes and connects the workflow structure.
  • ✓Validates the configuration and automatically fixes validation errors.
  • ✓Reports that credentials need to be configured manually.

✗ What AI Does NOT Do

  • ✗Configure OAuth credentials (Gmail, Sheets).
  • ✗Create the destination spreadsheet in your Google account.
  • ✗Map the field values (from, subject, snippet, ID, date).

🧭 Why learn

Knowing the boundary between "what the AI builds" and "what requires manual work" avoids frustration: you won't wait for the AI to perform an OAuth login that only you can do. The build delivers the structure; you connect the accounts and create the destination.

Key concepts

Creates nodes

Builds the workflow.

Validate

Auto-corrects config.

Gmail → Sheets

Trigger + append row.

Limit

Credentials are manual.

4

🔑 OAuth credentials & keys

What it is: securely connect the accounts the workflow uses. Gmail and Google Sheets are connected through OAuth2 (you sign in with your Google account); OpenAI is accessed through API key (a secret key you generate in the dashboard). It's the most important manual step in the build.

Credentials walkthrough

1

OAuth2 in Gmail and Sheets

In the node, choose “Create new credential” → OAuth2 → sign in with Google. Use a same account where you'll create the destination spreadsheet.

2

OpenAI API key

In the OpenAI dashboard, under “API keys,” create a secret key, give it a descriptive name, and copy it right away—it appears only once. Paste it into the node as a new credential.

3

Treat as sensitive data

Treat the key like a credit card: anyone with access can spend your tokens. Keep a secure copy (digital + physical).

⚠️ Why learn — and the trap

  • ✗The OpenAI key is shown only once—if you don't copy it, generate another.
  • ✗Never paste credentials into the chat—a leaked key can be abused.
  • ✗Authorizing with the “wrong” account creates the spreadsheet that the AI can’t see.

Key concepts

OAuth2

Gmail/Sheets login.

API key

OpenAI secret key.

Shows 1×

Copy it during creation.

Same account

A spreadsheet the AI can see.

5

🔧 Phase 3 — Troubleshoot

What it is: the debugging phase is simple and powerful—you returns the error message for the AI, along with the node location, and it diagnoses and fixes it. You don’t have to decipher the error alone; the AI reads it, understands the cause, and applies the fix.

Debugging loop (illustrative example)
# erro no nó: "intervalo inválido: 14 horas"
# você cola o erro de volta no chat ↓
"Deu este erro no gatilho do Gmail: ..."

# a IA diagnostica e corrige:
- troca o intervalo por uma expressão cron */5
- explica por que o formato anterior falhou

🧭 Why learn

Debugging through conversation changes your relationship with errors: they stop being blockers and become input. Always provide the error and the node location — the more context, the more accurate the fix. After each fix, reload n8n before testing again.

🎯 Practical tip

Some node settings only accept specific formats. The Gmail trigger, for example, only accepts “every minute” or a cron expression — so a request for “5 minutes” is resolved as cron (*/5).

Key concepts

Paste the error

Message + node.

Diagnosis

The AI finds the cause.

Cron

Specific format.

Reload

Before testing again.

6

⚡ Phase 4 — Optimize

What it is: with the workflow working, you ask for improvements in natural language — for example, "change the polling interval to every 5 minutes". The AI applies the change; if it runs into an invalid format, that becomes another troubleshooting round. Optimizing simply means continuing the conversation.

✓ Ask in natural language

  • ✓“Check Gmail every 5 minutes instead of 10.”
  • ✓“Add a label to emails that have already been processed.”
  • ✓“Include the received date in the spreadsheet.”

✗ Don’t Micromanage

  • ✗Edit the cron expression manually inside the node.
  • ✗Rebuild the workflow from scratch because of a small change.
  • ✗Make multiple changes at once without testing.

🧭 Why learn

The optimization phase closes the loop, but the loop never "ends": each improvement can reveal a new error to debug or a new adjustment to plan. Thinking in loops—and not in straight lines—is what separates a toy build from a workflow that evolves.

Key concepts

In PT

Ask for the improvement.

One at a time

Test between changes.

Turns into a loop

You can reopen troubleshooting.

Iterative

The cycle doesn’t end.

7

📨 Project Guide: Support Agent (RAG)

What it is: the guide project for this module is a email support agent with RAG — it reads policy documents in PDF, identifies support questions, and drafts well-supported responses for human review. The architecture has two workflows: one indexes the knowledge, and the other responds to emails.

🧱 The two workflows

  • •Database indexer: manual trigger → code node (document text) → in-memory vector store with OpenAI embeddings.
  • •Support agent: Gmail trigger (every 15 min, unread only) → text classifier → OpenAI model → AI agent with the vector store tool → Gmail draft.

🧭 Why learn — system prompt vs. vector store

An architectural decision shows up right in the plan: embed the documents in the system prompt (simpler and cheaper for short texts) or use a vector store / RAG (scalable, retrieves only the relevant passages, better as you grow). Here we chose the vector store for scalability.

System prompt

Embed the text directly. Simple, inexpensive, great for small docs.

Vector store (RAG)

Searches only for relevant passages. Scales and reduces context costs.

🎯 Practical tip

Execution order matters: run the indexer workflow once manually to populate the vector store; only then enable the support agent. Put the PDFs in the project folder and reference them by tag in the prompt so the AI can read them.

Key concepts

RAG

Responds based on docs.

Classifier

Support vs. irrelevant.

Vector store

Semantic search.

Draft

Human review first.

8

🧪 Verification & Testing

What it is: before trusting the agent, you run a visual checklist and test real-world scenarios. The golden rule: green ≠ correct — an error-free run doesn’t prove the answer is correct. The best agent is the one that refuses to make things up what you don’t know.

Real test scenarios

1

Return question

“I bought a laptop 3 weeks ago; I’ve already opened it.”

The draft must correctly state the 30-day window and the restocking fee. Check the numbers (30 days, not "one year").

2

Warranty question

“The TV broke after 8 months.”

The draft must mention the manufacturer's one-year warranty—exactly as stated in the policy PDF.

3

Out of scope (store hours)

“What time does the store open?”

The agent should honestly say that doesn’t have that information, instead of making things up—a sign that it uses only the provided PDFs.

✓ Test-by-test checklist

  • ✓Email received → classified as support.
  • ✓Correct policy found in the vector store.
  • ✓Precise draft generated and visible in the Drafts folder.

✗ Warning Signs

  • ✗Workflow is “green,” but the number in the response is wrong.
  • ✗The agent makes up a store schedule that doesn’t exist in the PDFs.
  • ✗Non-support email generating a draft (it should stop).

🧭 Why learn

Testing with real scenarios is what turns “seems to work” into “works.” Refusing to hallucinate is a desired result, not a bug—and gray-area questions (like store hours) reveal where you need to decide the scope of your own agent.

Self-healing (optional): the workflow ran without any errors (everything was “green”). Does that guarantee the answer is correct?

📌 Module Summary

✓
Permission modes & Plan Mode — always start by planning; Bypass speeds things up but leaves you blind.
✓
Plan → Build — the AI researches, asks questions, and proposes; then creates and validates the nodes.
✓
Credentials — OAuth2 (Gmail/Sheets) and API key (OpenAI); only in the right place.
✓
Troubleshoot → Optimize — send the error back to the AI; ask for improvements in natural language.
✓
RAG project + tests — support agent in two workflows; green ≠ correct.

Next Module:

1.3 — From “It Works” to “It Works Well”: Enhancement