Lesson 8Hands-on

Building AI Agents

From scratch with code

The Problem: You understand agent concepts, but how do you actually build one? What are the pieces you need? How do they fit together? It's like knowing what a car does but not how to build one.

The Solution: Assembly from Components

Building an agent is like assembling a robot from parts. Each part has a specific function, and together they create something that can think and act. At the core is the ReAct loop -- think, act, observe. You wire in tools via function calling, and frameworks like LangChain provide ready-made building blocks.

Think of it like building a robot assistant:

  • 1. Brain (LLM): The AI model that thinks and decides
  • 2. Instructions (System Prompt): What kind of assistant it should be
  • 3. Hands (Tools): What it can actually do in the world
  • 4. Memory: How it remembers past conversations
  • 5. Senses (Input handling): How it understands your requests
  • 6. Voice (Output formatting): How it presents its responses

Key Steps to Build an Agent

  • Define the goal: What should this agent accomplish?
  • Write the persona: System prompt with personality and constraints
  • Add tools: Functions the agent can call
  • Set up the loop: How the agent thinks → acts → observes
  • Handle errors: What happens when things go wrong

Fun Fact: The simplest useful agent can be built in about 50 lines of code! The core loop is just: get user input → send to LLM with tools → execute tool calls → repeat. Everything else is making that loop better, safer, and smarter.

Try It Yourself!

Use the interactive builder below to assemble your own agent step by step. Add components, configure them, and see how they work together!

Frequently asked questions

What components does an AI agent need?

A minimal AI agent needs: an LLM for reasoning, a system prompt defining its role, tools it can call (APIs, databases, code execution), a loop that processes LLM responses and executes tool calls, and memory for context persistence across interactions.

Should I use a framework like LangChain or build from scratch?

Start from scratch for learning and simple agents — it gives you full control and understanding. Use frameworks like LangChain or LlamaIndex when you need pre-built integrations (vector stores, document loaders) or when building complex multi-step workflows quickly.

How do I prevent an AI agent from running in infinite loops?

Set a maximum iteration count (e.g., 10-20 steps), implement timeout limits, add cost tracking to stop when budget is exceeded, use the LLM's own judgment with a 'final_answer' tool, and log each step for debugging.

What is the ReAct pattern for AI agents?

ReAct (Reasoning + Acting) is a pattern where the agent alternates between thinking (reasoning about what to do) and acting (calling tools). Each cycle: the LLM reasons about the current state, decides on an action, executes it, observes the result, and repeats until the task is complete.

Try it yourself

Interactive demo of this technique

Technique Comparison
Demo Mode
Pre-recorded responses
TaskBeginnerCoding

Build a simple agent that can answer questions about files in a directory.

Without technique
Without technique
Prompt
Write code that answers questions about files in a directory.
Response
import os

def answer_question(directory, question):
    files = os.listdir(directory)
    if "how many" in question.lower():
        return f"There are {len(files)} files"
    elif "list" in question.lower():
        return ", ".join(files)
    else:
        return "I don't understand the question"
Tokens:25/85
Time:520ms
Quality:
With Building Agents
With technique
Prompt
Build an agent with a "think → act → observe" loop and tool definitions. Requirements: - Agent loop (while loop) - Tool definitions (tools) - LLM action parsing - Return final answer Task: agent for answering questions about files in a directory.
Response
import os
from openai import OpenAI

# Define tools
tools = {
    "list_files": lambda dir: os.listdir(dir),
    "read_file": lambda path: open(path).read(),
    "file_info": lambda path: {
        "size": os.path.getsize(path),
        "modified": os.path.getmtime(path)
    }
}

tool_descriptions = [
    {"name": "list_files", "description": "List files in directory", "params": ["directory"]},
    {"name": "read_file", "description": "Read a file", "params": ["filepath"]},
    {"name": "file_info", "description": "Get file info", "params": ["filepath"]}
]

def run_agent(question, directory):
    client = OpenAI()
    messages = [
        {"role": "system", "content": f"You are an agent with access to {directory}. Use tools to answer. Format: ACTION: tool_name(args) or ANSWER: final answer."},
        {"role": "user", "content": question}
    ]
    
    for step in range(5):  # max 5 steps
        response = client.chat.completions.create(
            model="gpt-4", messages=messages
        )
        text = response.choices[0].message.content
        
        if "ANSWER:" in text:
            return text.split("ANSWER:")[1].strip()
        
        if "ACTION:" in text:
            action = parse_action(text)
            result = tools[action.name](*action.args)
            messages.append({"role": "assistant", "content": text})
            messages.append({"role": "user", "content": f"Observation: {result}"})
    
    return "Could not find answer"
🧠Define tools: list_files, read_file, file_info
🧠Agent loop: LLM thinks → picks action → gets result → repeats
👁️Key difference from monolithic code: LLM decides which tools to call
Agent can answer any file question, not only pre-programmed ones
Tokens:95/380
Time:3200ms
Quality:
Why this works

A monolithic approach requires if/else for every question type. An agent loop lets the LLM choose tools itself, making the system flexible and extensible.

1 / 2

Lesson Quiz

1 of 3

1.What is a critical consideration when designing error handling for production agents?

Practice Challenges

Create a free account to solve challenges

3 AI-verified challenges for this lesson

Related lessons:Function CallingMemory

This lesson is part of a structured LLM course.

My Learning Path