PTENES
MODULE 3.3

🎯 Prompt design and decision logic

The prompt is the brain of AI automation — and, since there’s no human to correct it in real time, it needs to be right from the start. Here you design prompts as specifications, enforce validatable JSON output, protect with guardrails, branch the flow based on AI, and iterate with tests until you trust it.

6
Topics
~60
Minutes
Intermediate.
Level
Practical
Type
1

📝 Prompt as specification

In automation, a prompt isn’t a conversation — it’s natural language code. It will run on its own a thousand times, with no one there to correct the AI in real time. That's why it needs to be a specification: precise, unambiguous instructions that define exactly the expected behavior.

✓ Specification prompt

  • ✓Defines the role, task, and exact output format.
  • ✓Cover edge cases ("if there’s no data, return X").
  • ✓Behaves the same way every time it runs.

✗ Conversational prompt

  • ✗"Help me analyze this" — vague and dependent on context.
  • ✗Doesn’t say what to do when something goes off script.
  • ✗Gives different answers each time.

💡 Practical tip

Before writing the prompt, ask: if I gave this instruction to a new employee, could they carry it out without asking me anything? If the answer is no, the prompt still has gaps.

Code

Runs on its own a thousand times.

No ambiguity

Needs to be right first.

Edges

Cover the unexpected.

Consistent

Same input, same output.

2

🏗️ Structure of a reliable prompt

A fixed skeleton makes behavior predictable: role (who the AI is), context (what it needs to know), task (what to do), output format (how to respond) and rules (what to respect). Below, the same pattern applied to an order classifier.

🧾 Structured prompt example

# ROLE
You are an e-commerce order classifier.
Respond objectively, without extra comments.
# CONTEXT
Valid categories: purchase, support, complaint.
Valid priorities: high, medium, low.
# TASK
Read the customer's message and classify the category
and priority. Estimate your confidence from 0 to 1.
# OUTPUT FORMAT
Respond with ONLY a JSON object, with no text before it
or afterward, with the keys: category, priority,
confidence, justification.
# RULES
- If it can’t be classified, use confianca < 0.5.
- Never invent a category outside the list.
# MESSAGE
{{ mensagem_do_cliente }}

📐 Why named sections work

  • •AI separates instructions (rules) from data (the customer's message) without confusing them.
  • •You review and version each section independently.
  • •Becomes a template: the same framework works for any classifier.
Role

Who the AI is.

Context

What you need to know.

Format

How to respond.

Rules

What to respect.

3

🧾 Structured output (JSON) and validation

Free-form text breaks automation: the next step doesn't know where each piece of information is. Enforcing JSON with fixed fields e validate the response before using it is what makes the AI output reliable enough to trigger an automated action.

📤 Expected JSON output

{
  "category": "complaint",
  "priority": "high",
  "confidence": 0.92,
  "justification": "customer mentions a delay and asks for a refund"
}

✅ Validation before use (pseudocode)

# 1. Is it valid JSON?
data = parse_json(ai_output)  # failed → retry/escalate
# 2. Does it have all required fields?
require(data, [category, priority, confidence])
# 3. Are the values in the allowed list?
if dados.categoria not in CATEGORIAS: reject
# 4. Only then proceed to the decision

⚠️ Attention

Never blindly trust the AI’s output. If the JSON comes back malformed or with an invented category, validation should reject and retry (or escalate)—never let an invalid value proceed to an automated action.

JSON

Fixed, named fields.

Validate

Before using the output.

List

Values within the allowed range.

Reject

Invalid → retry or escalate.

4

🚧 Guardrails and fallback

AI makes mistakes and hallucinates. Guardrails those are the limits — what it can't do, what validation rejects. Fallback is what happens when it isn’t sure: ask for confirmation, escalate to a human, or use a safe default. Together, they prevent an error from becoming an irreversible action.

1

Prompt guardrail

Explicit instructions about what never to do: don’t invent categories, make decisions outside the scope, or mark confidence as low when uncertain.

2

Validation guardrail

A programmatic check that rejects outputs outside the format or list of values—the AI proposes, the code verifies.

3

Confidence-based fallback

When confidence falls below a threshold (e.g., 0.6), the workflow routes to human review instead of acting on its own.

💡 Practical tip

Ask the AI for a field confidence for every output and use it as a fallback trigger. It’s the simplest way to turn "the AI isn’t sure" into a concrete flow decision: act on its own only above the threshold; escalate below it.

Limit

What AI can’t do.

Confidence

Field that triggers a fallback.

Scale

Human when uncertain.

Safe

An error doesn’t trigger an irreversible action.

5

🔀 Branching logic based on AI

This is where AI stops being decoration and becomes the brain: a categorical field in the JSON output (e.g., category or priority) becomes the key to a IF/switch in the workflow, routing each case to a different action. AI classifies, the workflow executes.

AI → JSON category + confidence switch purchase → create order support → open a ticket complaint → escalate illustrative — AI decides; the flow branches

📐 Rules for a healthy branch

  • •Use a field categorical (closed list) as the key—never free text.
  • •Always have a default branch for the case that doesn't match any option.
  • •Combine with confidence: the right category plus low confidence can still be escalated.
AI decides

Workflow runs.

Key

Closed categorical field.

Pattern

Branch for the rest.

Combine

Category + confidence.

6

🔁 Test and iterate on the prompt

A prompt that gets it right in one example can fail in ten. Build a set of test cases — including the difficult and adversarial ones—run the prompt against all of them, measure the accuracy rate, and adjust until it stabilizes. Only this cycle gives you the confidence to let AI make decisions on its own in production.

1

Gather real-world cases

Use real messages from the process (from the T2 audit), including ambiguous ones and those that tend to cause confusion.

2

Define the answer key

For each case, note what the correct output would be. This answer key is the yardstick you use to measure the prompt.

3

Run, measure, adjust

Run the prompt on all cases, compare the results with the answer key, calculate the accuracy rate, and adjust the prompt where it failed. Repeat until it stabilizes.

✓ Healthy iteration

  • ✓Includes difficult and adversarial cases from the start.
  • ✓Measures the accuracy rate before and after each adjustment.
  • ✓Stop iterating when the rate stabilizes, not on the first attempt.

✗ Naive iteration

  • ✗Tests with just one easy example and calls it done.
  • ✗Adjusts based on "guesswork," without measuring whether it improved.
  • ✗Fixes one case and breaks three without noticing.
Set

Multiple cases, not just one.

Difficult

Adversarial cases first.

Measure

Accuracy rate, not guesswork.

Stabilize

Iterate until it stops improving.

🎯 Module Summary

✓
Prompt is specification — code in natural language that runs on its own a thousand times.
✓
Fixed structure — role, context, task, format, and rules in named sections.
✓
Validated JSON — structured output, checked before it becomes an action.
✓
Guardrails and fallback — limits + scaling by confidence prevent incorrect irreversible actions.
✓
Branch and test — AI decides via a switch; iterate with real cases until it stabilizes.

Next Track:

Track 4 · Build — move from the blueprint to building the end-to-end automation.