AI Agents & Orchestration
State machines have predefined transitions. Agents decide autonomously. You'll implement multiple agent patterns (ReAct, plan-and-execute, tool-use), add safety guardrails against infinite loops and cost runaway, and build a research agent that gathers, summarizes, and compares information.
Use this at work tomorrow
Build a Slack bot agent that answers questions by searching your docs, Jira, and codebase.
Learning Objectives
- 1Implement the ReAct loop: reason → act → observe → repeat
- 2Build plan-and-execute agents that decompose complex tasks
- 3Add safety guardrails: max iterations, cost budgets, timeout limits
- 4Handle agent failure modes: infinite loops, hallucinated actions, cost explosions
- 5Ship a research agent that automates real information gathering
Ship It: Research automation agent
By the end of this day, you'll build and deploy a research automation agent. This isn't a toy — it's a real project for your portfolio.
I can build an AI agent with a ReAct loop, choose the right agent pattern for a task, and implement guardrails to prevent runaway costs and infinite loops.
What makes an AI agent different from a hardcoded workflow?
Agents = Autonomous Loops
State machines transition between predefined states that YOU define. Agents are autonomous loops — the LLM observes the current state, reasons about what to do, takes an action (tool call), observes the result, and repeats until the task is done. It's a dynamic state machine where the LLM is the transition function.
In an agent loop, who decides the next action?
You're building a research bot that needs to search, read pages, and write a report. Which pattern?
Agent Patterns: ReAct, Plan-and-Execute, Tool-Use
ReAct (Reason + Act) is the simplest: think → act → observe → repeat. Plan-and-Execute decomposes the task first, then executes steps. Tool-Use agents are simpler — just give the LLM tools and let it call them iteratively. Each pattern suits different complexity levels. ReAct for simple tasks, Plan-and-Execute for complex multi-step work.
Which agent pattern creates a full plan before executing anything?
Your agent runs 15 steps and costs $2.50 for a simple question. What guardrail failed?
Safety Guardrails: Preventing Agent Disasters
Agents can go catastrophically wrong: infinite loops (keeps calling tools forever), cost explosions ($50 in API calls for one query), hallucinated actions (calling tools that don't exist with wrong parameters), and unsafe actions (deleting data it shouldn't). Always implement: max iteration limits, cost budgets per request, action allowlists, and human-in-the-loop for destructive actions.
An agent calls searchWeb('react hooks') three times in a row. What guardrail catches this?
Agent Failure Modes: What Goes Wrong
The most common agent failures: (1) Infinite loops — agent keeps trying the same failed approach. Fix: max iterations + detect repeated actions. (2) Cost runaway — agent calls expensive tools many times. Fix: token/cost budget per conversation. (3) Hallucinated tools — agent tries to call tools that don't exist. Fix: strict tool schema validation. (4) Context overflow — agent accumulates too many tool results. Fix: summarize tool results before adding to context.
The Full Evolution
Watch one function evolve through every concept you just learned.
Production Gotchas
Always set maxSteps (Vercel AI SDK) or max iterations. 10 steps is a good default. Log every agent step — you'll need the trace to debug failures. Agents are expensive: a 5-step agent call uses 5x the tokens. Use cheaper models (GPT-4o-mini) for simple agent tasks, expensive models (GPT-4o) only when reasoning quality matters. Consider multi-agent architectures: a 'planner' agent decides what to do, 'worker' agents execute individual tools.
Code Comparison
State Machine vs Agent Loop
Predetermined flow vs autonomous decision-making
// Traditional state machine
// YOU define all states and transitions
type State =
| "INIT" | "SEARCH" | "ANALYZE"
| "SUMMARIZE" | "DONE";
let state: State = "INIT";
while (state !== "DONE") {
switch (state) {
case "INIT":
await gatherRequirements();
state = "SEARCH";
break;
case "SEARCH":
const data = await searchDB(query);
state = "ANALYZE";
break;
case "ANALYZE":
const insight = analyze(data);
state = "SUMMARIZE";
break;
case "SUMMARIZE":
await createReport(insight);
state = "DONE";
break;
}
}
// Fixed path: INIT→SEARCH→ANALYZE→DONE
// Can't adapt if search returns nothing// AI Agent loop — LLM decides each step
import { generateText } from "ai";
const { text } = await generateText({
model: openai("gpt-4o"),
tools: {
searchWeb: searchTool,
analyzeData: analyzeTool,
writeReport: reportTool,
askUser: clarifyTool,
},
maxSteps: 10, // Safety limit
system: `You are a research agent.
Given a research question:
1. Search for relevant information
2. Analyze what you find
3. If results are insufficient,
search with different terms
4. Write a summary report
Always cite your sources.`,
prompt: userQuestion,
});
// Dynamic: might search 3 times,
// ask user for clarification,
// skip analysis if data is clear
// Adapts to what it finds!KEY DIFFERENCES
- State machines: YOU define all states and transitions upfront
- Agents: the LLM decides the next action based on what it observes
- Both loop until completion — agents just have dynamic transitions
- maxSteps prevents infinite loops — always set this in production
Bridge Map: State machines / workflows → Agent loops + orchestration
Click any bridge to see the translation
Hands-On Challenges
Build, experiment, and get AI-powered feedback on your code.
Research Automation Agent
Build and deploy an autonomous research agent that takes a topic, searches the web for information, reads and extracts content from multiple sources, and produces a structured research summary with citations. This is real AI agent engineering.
Acceptance Criteria
- Accept a research topic or question as input
- Implement 3+ agent tools: web search, page reading, note-taking
- Use the ReAct loop pattern (reason → act → observe → repeat) with maxSteps
- Add safety guardrails: max iterations, cost budget, timeout limits
- Show the agent's reasoning trace in real-time (which tools it called and why)
- Produce a structured research report with sourced findings
- Deploy to a public URL (Vercel, Netlify, etc.)
Build Roadmap
0/6Create a new Next.js app with TypeScript and Tailwind CSS. Plan the agent architecture: tools, loop control, and output format.
npx create-next-app@latest research-agent --typescript --tailwind --appCreate separate files for tools, agent logic, and the UIDeploy Tip
Push to GitHub and import into Vercel. Set OPENAI_API_KEY and any search API keys in Vercel environment variables. Add rate limiting on the endpoint — agent calls can be expensive.
Sign in to submit your deployed project.
I can build an AI agent with a ReAct loop, choose the right agent pattern for a task, and implement guardrails to prevent runaway costs and infinite loops.