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!
Try it yourself
Interactive demo of this technique
Build a simple agent that can answer questions about files in a directory.
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"
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"
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.
Create a free account to solve challenges
3 AI-verified challenges for this lesson
This lesson is part of a structured LLM course.
My Learning Path