Prompting AI Agents

Reviewed & published by Brayan K

An AI agent does not just answer once, it works toward a goal over multiple steps. The simple formula is LLM + tools + a loop: it plans, acts, observes the result, and iterates until done.

Part of the free Prompt Engineering course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

This lesson covers multi-step planning, the ReAct (reason + act) loop, reflection and self-correction, and the guardrails and stopping conditions that keep agents safe and on track.

What You'll Learn in This Lesson

1 The reason + act loop

Give the agent a goal and tools. In each turn it reasons, acts, and observes, then loops back. This is the ReAct pattern:

2 Reflection and self-correction

Strong agents pause to review their own work. Asking the agent to critique itself catches mistakes before they pile up:

This reflection step is often the difference between an agent that drifts and one that reliably reaches the goal.

3 Guardrails and stopping conditions

Loops can run forever. Always set limits so the agent stops cleanly and stays within safe bounds:

Try it for real

An agent is a loop, not a personality: the model names a tool, your code runs it, the result goes back in. Run this and watch the third turn ask to delete a database. It is refused for one reason — it is not in the allow-list. The model can ask for anything; it gets exactly what your dispatcher contains, which is the entire security model in one line of code.

# An "agent" is a loop: the model names a tool, your code runs it, the result
# goes back in. The model never touches your systems — your dispatcher does.

import re

def get_order(order_id):
    orders = {"1001": "shipped 12 Aug", "1002": "awaiting payment"}
    return orders.get(order_id, "no such order")

def refund_window(_):
    return "30 days from delivery"

TOOLS = {"get_order": get_order, "refund_window": refund_window}

# Recorded model turns. Only the TOOL: lines are machine-readable; everything
# else is chatter your dispatcher must ignore.
transcript = [
    "I'll look that up. TOOL: get_order(1001)",
    "Thanks. TOOL: refund_window()",
    "TOOL: delete_database()",          # a tool you never registered
    "So your order shipped on 12 Aug and you have 30 days to return it.",
]

for turn in transcript:
    m = re.search(r"TOOL:\s*(\w+)\((.*?)\)", turn)
    if not m:
        print(f"FINAL   {turn}")
        continue
    name, arg = m.group(1), m.group(2).strip()
    if name not in TOOLS:
        print(f"BLOCKED {name}() — not a registered tool")
        continue
    print(f"CALL    {name}({arg!r}) -> {TOOLS[name](arg)!r}")

print()
print("The third turn is the whole security model in one line: the model can ASK")
print("for anything, and gets only what your allow-list actually contains.")
# 🎯 YOUR TURN — write the dispatcher's allow-list check.

import re

def get_stock(sku):
    return {"A1": "12 in stock", "B2": "out of stock"}.get(sku, "unknown sku")

TOOLS = {"get_stock": get_stock}

transcript = [
    "Let me check. TOOL: get_stock(A1)",
    "TOOL: wipe_orders()",
    "You have 12 in stock.",
]

for turn in transcript:
    m = re.search(r"TOOL:\s*(\w+)\((.*?)\)", turn)
    if not m:
        print("FINAL  ", turn)
        continue

    name, arg = m.group(1), m.group(2).strip()

    # 1) Refuse anything that is not on the allow-list.
    if name ___ TOOLS:
        # 👉 replace ___ with  not in
        print("BLOCKED", name)
        continue

    # 2) Look the function up by name and call it with the argument.
    print("CALL   ", name, "->", TOOLS[___](arg))
    # 👉 replace ___ with  name

# ✅ Expected output:
# CALL    get_stock -> 12 in stock
# BLOCKED wipe_orders
# FINAL   You have 12 in stock.

Common Mistakes (and the fix)

⏱ Test Yourself — Timed Quiz

10 quick questions, 12 seconds each. Instant feedback — beat the clock!

🎉 Lesson Complete

Practice quiz

What is an AI 'agent' in this context?

  • An LLM combined with tools and a loop that pursues a goal over multiple steps
  • A single one-shot answer
  • A type of database
  • A human worker

Answer: An LLM combined with tools and a loop that pursues a goal over multiple steps. An agent is an LLM plus tools plus a loop that works toward a goal step by step.

The ReAct pattern stands for…

  • Reading and acting on emails
  • Reacting to errors only
  • Reason plus Act: the agent thinks, then takes an action, and repeats
  • A JavaScript library

Answer: Reason plus Act: the agent thinks, then takes an action, and repeats. ReAct interleaves reasoning and acting in a loop.

Why do agents work in a loop?

  • To avoid using tools
  • So they can plan, act, observe the result, and adjust until the goal is done
  • To waste time
  • Because loops are required by law

Answer: So they can plan, act, observe the result, and adjust until the goal is done. The loop lets the agent take multiple steps and react to each result.

A simple formula for an agent is…

  • A spreadsheet
  • Just a prompt
  • A printer
  • LLM + tools + a loop

Answer: LLM + tools + a loop. An agent is an LLM with tools driven by an iterating loop toward a goal.

What is 'reflection' or self-correction in an agent?

  • The agent reviewing its own progress and fixing mistakes before continuing
  • Restarting the computer
  • Looking in a mirror
  • Deleting the goal

Answer: The agent reviewing its own progress and fixing mistakes before continuing. Reflection means the agent critiques its own work and corrects course.

Why are stopping conditions important for agents?

  • To hide the goal
  • Without them an agent can loop forever or run up cost
  • They are not
  • To make answers random

Answer: Without them an agent can loop forever or run up cost. Stopping conditions prevent endless loops and runaway cost.

Guardrails for an agent typically…

  • Make it faster
  • Remove its tools entirely
  • Translate its output
  • Limit which actions it may take and when to stop or ask for help

Answer: Limit which actions it may take and when to stop or ask for help. Guardrails bound the agent's actions and define safe limits.

Multi-step planning means the agent…

  • Picks a random tool
  • Answers in one shot only
  • Breaks a goal into steps and works through them in order
  • Ignores the goal

Answer: Breaks a goal into steps and works through them in order. The agent decomposes the goal into a sequence of steps.

A good task for an agent rather than a single prompt is…

  • Saying hello
  • A multi-step goal like 'research a topic, gather sources, and write a summary'
  • Echoing a word
  • Counting to three

Answer: A multi-step goal like 'research a topic, gather sources, and write a summary'. Open-ended, multi-step goals benefit from the plan-act-observe loop.

When an agent observes a tool result, it should…

  • Use it to decide the next step or to finish the goal
  • Repeat the same step forever
  • Ignore it
  • Delete it

Answer: Use it to decide the next step or to finish the goal. The observation feeds the next reasoning step toward the goal.

Continue this course

Frequently asked questions

What is an AI agent, simply put?

An agent is an AI that does not just answer once, it works toward a goal over several steps. The simple formula is LLM plus tools plus a loop. You give it a goal and some tools (search, a calculator, your database), and it plans, takes an action, looks at the result, and decides what to do next, repeating until the goal is met or it decides to stop.

What is the ReAct loop?

ReAct stands for Reason plus Act. In each turn the agent first reasons about what to do, then acts by calling a tool, then observes the result, and loops back to reason again. This interleaving of thinking and acting is what lets an agent tackle problems that need several steps rather than a single reply.

What are reflection and stopping conditions?

Reflection, or self-correction, is when the agent reviews its own progress, notices a mistake or a dead end, and adjusts before continuing, which improves reliability on hard tasks. Stopping conditions are the rules that end the loop: the goal is achieved, a step limit is reached, or it needs human help. Without them, an agent can loop forever or run up cost.

When should I build an agent instead of using one prompt?

Use a single prompt for simple, one-shot tasks like rewriting a sentence. Reach for an agent when the goal is open-ended and multi-step, such as researching a topic across sources and producing a summary, where the AI needs to plan, use tools, and react to intermediate results. Agents add power but also complexity, cost, and the need for guardrails.