Build Your First AI Agent
From zero to production in one weekend
30-Day Money-Back Guarantee
If this guide doesn't help you build a working AI agent, email me and I'll refund you. No questions asked.
π Read the First 17% Free
2,465 of 14,938 words
Build Your First AI Agent: The Complete Guide
From Zero to Production: A 2-3 Week Journey (With Honest Timeline)
This guide is built for learning and working code. Realistic timeline: 2 days learning architecture, 3-4 days building, 3-4 days testing and debugging. One weekend is possible if you're an experienced Python developer β but budget 2-3 weeks for your first agent.
---
β‘ 5-Minute Quick Start
Impatient? Here's how to run a working agent in 5 minutes:
# 1. Get API keys (2 minutes)
# - Anthropic: https://console.anthropic.com/ (copy sk-ant-xxx)
# - NewsAPI: https://newsapi.org/ (copy your key)
# 2. Set up project (1 minute)
mkdir my-agent && cd my-agent
python3 -m venv venv && source venv/bin/activate
pip install anthropic python-dotenv requests
# 3. Create .env (1 minute)
echo "ANTHROPIC_API_KEY=sk-ant-xxx" > .env
echo "NEWS_API_KEY=your-key" >> .env
# 4. Create agent.py (copy from "Quick Start Example" below) (1 minute)
# 5. Run it
python3 agent.py
Quick Start Example - Complete, Runnable Code
#!/usr/bin/env python3
"""The absolute simplest AI agent you can build."""
import os
import json
from anthropic import Anthropic
from dotenv import load_dotenv
import requests
load_dotenv()
# Setup
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
NEWS_API_KEY = os.getenv("NEWS_API_KEY")
# Tool 1: Search news
def search_news(topic: str) -> list:
"""Search for news articles."""
response = requests.get(
"https://newsapi.org/v2/everything",
params={"q": topic, "sortBy": "relevancy", "pageSize": 3, "apiKey": NEWS_API_KEY}
)
articles = response.json().get("articles", [])
return [{"title": a["title"], "source": a["source"]["name"]} for a in articles]
# Define tools for Claude
tools = [
{
"name": "search_news",
"description": "Search for news articles on a topic",
"input_schema": {
"type": "object",
"properties": {
"topic": {"type": "string", "description": "Topic to search"}
},
"required": ["topic"]
}
}
]
# The agent loop
def run_agent(user_request: str):
"""Run the agent with tool calling."""
messages = [{"role": "user", "content": user_request}]
for _ in range(3): # Max 3 iterations
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=500,
tools=tools,
messages=messages
)
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason == "end_turn":
# Agent finished
for block in response.content:
if hasattr(block, "text"):
return block.text
elif response.stop_reason == "tool_use":
# Agent wants to use a tool
tool_results = []
for block in response.content:
if block.type == "tool_use" and block.name == "search_news":
articles = search_news(block.input["topic"])
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": json.dumps(articles)
})
messages.append({"role": "user", "content": tool_results})
return "Agent completed"
# Run it
if __name__ == "__main__":
result = run_agent("Find me 3 news articles about AI and tell me what's happening")
print(result)
That's it. You now have a working AI agent that searches for news and summarizes what it finds. Run it, iterate, add more tools.
---
Table of Contents
1. Introduction: Why AI Agents Matter 2. What Is An AI Agent? (And What It Isn't) 3. The Core Architecture: How Agents Think 4. Prerequisites: What You Need Before Starting 5. Your First Agent: A Step-by-Step Build 6. Complete Code Walkthrough 7. Adding Memory: Making Agents Learn 8. Adding Tools: Giving Agents Superpowers 9. Error Handling: When Things Go Wrong 10. Testing Your Agent 11. Deployment: Running 24/7 12. Three More Agents You Can Build Today 13. Common Mistakes and How to Avoid Them 14. Advanced Patterns 15. Resources and Next Steps
---
1. Introduction: Why AI Agents Matter {#introduction}
You've seen ChatGPT. You've maybe built a simple chatbot. But you keep hearing about "AI agents" and wonder: what's the difference? Why does it matter?
Here's the difference in one sentence: Chatbots wait for you. Agents work for you.
A chatbot sits there until you type something. An agent wakes up at 6 AM, processes your emails, summarizes what's important, and sends you a briefing before you've had coffee.
The 3x Leverage
Think about your work day: - You work ~8 hours - You sleep ~8 hours - You do other stuff ~8 hours
An agent works 24 hours. That's 3x your working time.
If an agent saves you just 1 hour per day by handling repetitive tasks, that's: - 365 hours per year - 9+ work weeks - $36,500 in time (at $100/hour)
And that's one agent doing one thing.
What You'll Build In This Guide
By the end of this guide, you'll have:
1. A working agent that runs autonomously and does real work 2. Understanding of how agents think, plan, and execute 3. Code templates you can modify for any use case 4. Debugging skills for when (not if) things break 5. Deployment knowledge to run agents 24/7
This isn't theory. You'll write real code, run it, and see results.
Let's start.
---
2. What Is An AI Agent? (And What It Isn't) {#what-is-an-agent}
The Simple Definition
An AI agent is a program that: 1. Receives a goal 2. Makes a plan to achieve that goal 3. Executes the plan autonomously 4. Adapts when things don't go as expected 5. Reports what it accomplished
That's it. No magic. No sentience. Just software that can think through problems and take action.
What Agents Are NOT
Not chatbots. Chatbots are reactiveβthey respond to input. Agents are proactiveβthey pursue goals.
Not automation scripts. Scripts follow fixed rules. Agents make decisions based on context.
Not AGI. We're not building Skynet. We're building useful tools that handle specific tasks.
The Spectrum of Autonomy
Think of a scale from "dumb script" to "fully autonomous":
Script -------- Simple Agent -------- Complex Agent -------- AGI
| | | |
Fixed rules Makes simple Makes complex General
decisions decisions intelligence
"If X, do Y" "Classify this "Plan a project, "Do anything
email and execute steps, a human can"
route it" adjust as needed"
This guide focuses on simple to complex agentsβuseful, buildable, not science fiction.
Real-World Agent Examples
Example 1: Email Triage Agent - Goal: Process incoming support emails - Plan: Read email β Classify urgency β Route to right team β Send acknowledgment - Autonomy: Decides urgency level, picks routing rules, writes custom responses - Adaptation: Learns from corrections ("That should have been urgent")
Example 2: Research Agent - Goal: Find and summarize news about competitors - Plan: Search news sources β Filter relevant articles β Extract key points β Generate report - Autonomy: Decides what's relevant, prioritizes important news, summarizes intelligently - Adaptation: Adjusts relevance criteria based on what you actually read
Example 3: Code Review Agent - Goal: Review pull requests before humans look at them - Plan: Read code changes β Check against standards β Identify issues β Post comments - Autonomy: Decides severity of issues, writes specific feedback, suggests fixes - Adaptation: Updates standards based on team decisions
Example 4: Meeting Prep Agent - Goal: Prepare briefings before your meetings - Plan: Check calendar β Look up attendees β Gather context β Generate brief - Autonomy: Decides what context matters, prioritizes information, formats for quick reading - Adaptation: Learns what information you actually use
Each of these agents does work that would take a human 15-60 minutes, automatically, every time it's needed.
---
3. The Core Architecture: How Agents Think {#architecture}
The Agent Loop
Every agent follows the same basic pattern:
βββββββββββββββββββββββββββββββββββββββββββ
β 1. PERCEIVE β
β (What's the current situation?) β
β - Read inputs (emails, files, APIs) β
β - Understand context β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β 2. THINK β
β (What should I do?) β
β - Analyze the situation β
β - Consider options β
β - Make a decision β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β 3. ACT β
β (Do the thing) β
β - Execute the decision β
β - Use tools if needed β
β - Record what happened β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β 4. REFLECT β
β (Did it work?) β
β - Check results β
β - Learn from outcome β
β - Adjust for next time β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
ββββββββΊ Back to PERCEIVE
This is called the OODA loop (Observe, Orient, Decide, Act) in military strategy, or Perceive-Think-Act in robotics. Same idea, different names.
The Three Components
Every agent has three core components:
1. The Brain (LLM) This is where thinking happens. Claude, GPT-4, or another language model. It: - Understands natural language instructions - Reasons about what to do - Generates responses and decisions
2. The Memory (State) This is what the agent remembers. It includes: - Short-term: Current conversation/task context - Long-term: Past interactions, learned preferences, accumulated knowledge
3. The Hands (Tools) This is how the agent affects the world. Tools might include: - Reading/writing files - Sending emails - Calling APIs - Running code - Browsing the web
βββββββββββββββββββββββββββββββββββββββββββββββ
β AGENT β
β βββββββββββ βββββββββββ βββββββββββββββ β
β β BRAIN β β MEMORY β β TOOLS β β
β β (LLM) ββββ€ (State) ββββ€ (Actions) β β
β β β β β β β β
β β Claude β β Files β β Email API β β
β β GPT-4 β β Databaseβ β Web Search β β
β β etc. β β Vector β β Code Exec β β
β βββββββββββ βββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββ
How Information Flows
Let's trace through a real example: Email Triage Agent
Step 1: PERCEIVE
# Agent checks for new emails
new_emails = email_client.get_unread()
# Result: 15 new emails since last check
Step 2: THINK
# For each email, agent decides what to do
for email in new_emails:
# LLM analyzes the email
analysis = llm.analyze(f"""
Email from: {email.sender}
Subject: {email.subject}
Body: {email.body}
Classify as: urgent, normal, or spam
Route to: support, sales, or engineering
Suggest response: yes or no
""")
Step 3: ACT
# Agent takes action based on decision
if analysis.classification == "urgent":
send_slack_alert(f"Urgent email from {email.sender}")
apply_label(email, analysis.route)
if analysis.suggest_response:
draft = llm.generate_response(email)
save_draft(email, draft)
Step 4: REFLECT
# Agent logs what it did
log_action({
"emails_processed": 15,
"urgent": 2,
"drafts_created": 5,
"time_taken": "45 seconds"
})
# If human corrects a classification, agent learns
if correction_received:
update_classification_rules(correction)
This loop runs continuously (or on a schedule), handling emails without human intervention.
---
4. Prerequisites: What You Need Before Starting {#prerequisites}
Required Knowledge
You should be comfortable with: - Python basics (variables, functions, loops, classes) - Command line (running scripts, installing packages) - APIs (making HTTP requests, handling JSON)
You don't need: - Machine learning expertise - Prior AI/LLM experience - A computer science degree
If you can write a Python script that calls an API, you can build an agent.
Required Tools
1. Python 3.9+ Check your version:
python3 --version
2. An API Key You'll need access to an LLM. Options: - Anthropic (Claude): Best for complex reasoning. $3/million input tokens. - OpenAI (GPT-4): Good all-around. $10/million input tokens. - Local (Ollama): Free but less capable. Good for learning.
For this guide, we'll use Claude. Get an API key at: https://console.anthropic.com/
3. Required Python Packages
pip install anthropic python-dotenv requests
4. A Code Editor VS Code, PyCharm, or whatever you prefer.
Prerequisites: What You Need to Know
This is NOT a beginner-friendly guide. You should already know:
- β Python classes and OOP (you can read and modify class definitions)
- β How to make HTTP API requests and parse JSON
- β How to debug Python errors and read stack traces
- β Virtual environments and pip package management
If you're unsure about any of these, take 2 hours to review the Python docs first. This guide assumes intermediate Python knowledge.
API Keys You'll Need
Before starting, get these 4 keys (takes ~15 minutes):
1. Anthropic API Key (Required)
- Get it: https://console.anthropic.com/
- Looks like:
sk-ant-xxxxx - Cost: ~$0.0003 per agent run (very cheap to experiment)
- Free tier: $5 credit
2. NewsAPI Key (Required)
- Get it: https://newsapi.org/
- Looks like: random alphanumeric string
- Cost: FREE (100 requests/day, more than enough)
- Signup time: 30 seconds
3. Gmail App Password (Optional, if using email)
- Get it: https://myaccount.google.com/apppasswords
- Looks like:
abcd efgh ijkl mnop(16 characters with spaces) - Why not regular password? Gmail blocks basic auth. This is their secure alternative.
4. Stripe Test Key (Optional, if building payments)
- Get it: https://dashboard.stripe.com/test/apikeys
- Looks like:
sk_test_xxxxx - These are TEST keysβsafe to commit and share
Project Setup
Create a new directory for your agent:
mkdir my-first-agent
cd my-first-agent
Create a .env.example file as a template:
# .env.example - Copy to .env and fill in your values
# This is a template showing ALL possible configuration keys
# Only fill in the ones you actually use
# ============================================================================
# CORE API KEYS (REQUIRED)
# ============================================================================
# Anthropic Claude API
# Get from: https://console.anthropic.com/
# Format: sk-ant-xxxxx
ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxx
# NewsAPI for news searching
# Get from: https://newsapi.org/
# Free tier: 100 requests/day
# Format: random alphanumeric string
NEWS_API_KEY=your-newsapi-key-here
# ============================================================================
# EMAIL SETUP (Optional, only if using email tools)
# ============================================================================
# SMTP Server (Gmail example shown, customize for your provider)
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
# Gmail: Use app password, NOT your regular password
# Get from: https://myaccount.google.com/apppasswords
# Format: xxxx xxxx xxxx xxxx (16 chars with spaces)
SMTP_PASSWORD=your-app-password-here
# Email to receive briefings/alerts
BRIEFING_RECIPIENT=you@example.com
# ============================================================================
# PAYMENT PROCESSING (Optional, if using Stripe)
# ============================================================================
# Stripe API keys (use TEST keys for development)
# Get from: https://dashboard.stripe.com/test/apikeys
# Note: Test keys start with sk_test_
STRIPE_API_KEY=sk_test_xxxxxxxxxxxxx
# ============================================================================
# MONITORING & ALERTING (Optional)
# ============================================================================
# Sentry error tracking
# Get from: https://sentry.io/
SENTRY_TOKEN=your-sentry-token-here
# Slack webhook for notifications
# Get from: https://api.slack.com/messaging/webhooks
# Format: https://hooks.slack.com/services/YOUR/WEBHOOK/URL
SLACK_WEBHOOK=https://hooks.slack.com/services/YOUR/WEBHOOK/URL
# ============================================================================
# AGENT CONFIGURATION (Customize as needed)
# ============================================================================
# Topics your agent monitors
AGENT_TOPICS=artificial intelligence,startups,technology
# How many stories per topic
STORIES_PER_TOPIC=3
# Logging level: DEBUG, INFO, WARNING, ERROR
LOG_LEVEL=INFO
# Agent name (for logs and alerts)
AGENT_NAME=DailyBriefing
# ============================================================================
# OPTIONAL: OTHER INTEGRATIONS
# ============================================================================
# Database connection (if storing data)
# DATABASE_URL=postgresql://user:pass@localhost:5432/agent_db
# Google Sheets API (if syncing to sheets)
# GOOGLE_SHEETS_CREDENTIALS=/path/to/credentials.json
# Telegram bot (if sending alerts via Telegram)
# TELEGRAM_BOT_TOKEN=xxx
# TELEGRAM_CHAT_ID=xxx
# OpenAI API (if using GPT instead of Claude)
# OPENAI_API_KEY=sk-xxxxx
Setup Instructions:
- Copy this file:
cp .env.example .env - Edit
.envand fill in ONLY the keys you need - Never commit
.envto git (add to.gitignore) - For required keys (Anthropic, NewsAPI), get them first before running
Loading .env in Python:
# config.py - Load your environment variables safely
import os
from dotenv import load_dotenv
# Load from .env file
load_dotenv()
# Get required keys (fail if not set)
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
NEWS_API_KEY = os.getenv("NEWS_API_KEY")
if not ANTHROPIC_API_KEY:
raise ValueError("ANTHROPIC_API_KEY not set in .env")
if not NEWS_API_KEY:
raise ValueError("NEWS_API_KEY not set in .env")
# Get optional keys (with defaults)
SMTP_SERVER = os.getenv("SMTP_SERVER", "smtp.gmail.com")
SMTP_PORT = int(os.getenv("SMTP_PORT", "587"))
SMTP_USER = os.getenv("SMTP_USER", "")
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "")
# Agent settings
AGENT_TOPICS = os.getenv("AGENT_TOPICS", "artificial intelligence,startups,technology").split(",")
STORIES_PER_TOPIC = int(os.getenv("STORIES_PER_TOPIC", "3"))
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
# Use these in your agent code
from config import ANTHROPIC_API_KEY, NEWS_API_KEY, AGENT_TOPICS
Create a basic structure:
my-first-agent/
βββ .env.example # Template (commit to git)
βββ .env # Your keys (DO NOT commit)
βββ .gitignore # Include: .env, logs/, memory/
βββ config.py # Configuration loading
βββ agent.py # Main agent
βββ tools.py # Tool implementations
βββ memory.py # Memory management
βββ logs/ # Agent logs
You're ready to build.
π Continue reading the full guide...
Including 9 more chapters, advanced patterns, and deployment guides
Frequently Asked Questions
What do I get exactly?
A 10,000+ word guide with 12 working code examples. Covers architecture, memory, tools, testing, and deployment. Delivered as a PDF you can access instantly and forever.
Do I need to know Python?
Examples are in Python, but the concepts work in any language. If you can code, you can follow along.
How is this different from free tutorials?
Free tutorials teach you to call an API. This teaches you to build a production system β with memory, error handling, testing, and deployment. The stuff that actually matters when you ship.
What if it doesn't help me?
Email me within 30 days and I'll refund you. No hoops, no forms, no questions.
Works with:
Questions? alex@osolobo.com