How to Build Your First AI Agent (Step-by-Step Guide)
A practical, beginner-friendly guide to building your first AI agent. From choosing your tools to deploying a working agent - no PhD required.
Vincent Mugondora
software engineer & AI Builder
You’ve read about AI agents. You understand the concept. Now you want to build one.
This guide walks you through building a simple but functional AI agent from scratch. By the end, you’ll have a working agent that can accept a goal, use tools, and complete a multi-step task autonomously.
If you want a practical first project, start with something useful for a real business: an AI agent that answers FAQs from a knowledge base, qualifies leads from incoming messages, or summarises company information from a website or document set. That is also a strong way to test whether an AI agent is the right fit before investing in a more advanced build.
If you’re not just learning, but evaluating AI for a business or product, I also build custom AI solutions for businesses and AI automation workflows. No PhD required. Just basic Python and curiosity.
Prerequisites
Before you start, you’ll need:
- Basic Python knowledge - functions, loops, dictionaries, working with APIs
- An API key - from OpenAI (GPT-4) or Anthropic (Claude). Either works.
- A conceptual understanding of AI agents - if you’re not clear on what an agent is or how it differs from a chatbot, read I Built an AI Agent for a Real Zimbabwean Workflow — Here’s What Broke first.
That’s it. No machine learning background, no maths degree, no special hardware.
Step 1: Define what your agent will do
The biggest mistake beginners make: trying to build a general-purpose AI assistant. Don’t.
Pick one specific task with clear success criteria. For this tutorial, we’ll build:
An agent that researches a company and writes a brief summary - including what they do, how they make money, and recent news.
Success criteria:
- Given a company name, the agent produces a 200-word summary
- The summary includes factual information (not hallucinated)
- The agent uses web search to find current information
This is a small, achievable goal. Once it works, you can expand.
Step 2: Choose your approach
You have two options:
Option A: Use a framework (LangChain, CrewAI, Autogen)
- Faster to get something working
- More abstraction - harder to understand what’s actually happening
- Good for production once you understand the fundamentals
Option B: Build from scratch
- Takes longer initially
- You understand every piece
- Easier to debug and customise
My recommendation: Build from scratch first. Once you understand the pattern, frameworks become tools instead of magic. We’ll go with Option B.
This is fundamentally different from building a chatbot - a chatbot responds to messages, but an agent takes autonomous action over multiple steps.
Step 3: Build the core loop
Every AI agent follows the same pattern:
Think → Act → Observe → Think → Act → Observe → ... → Done
Here’s the skeleton in Python:
def run_agent(goal, tools, max_steps=10):
history = []
for step in range(max_steps):
# THINK: Ask the LLM what to do next
thought = think(goal, history, tools)
# CHECK: Are we done?
if thought["status"] == "complete":
return thought["final_answer"]
# ACT: Execute the chosen tool
action = thought["action"]
tool_name = action["tool"]
tool_input = action["input"]
result = execute_tool(tool_name, tool_input, tools)
# OBSERVE: Record what happened
history.append({
"thought": thought["reasoning"],
"action": action,
"result": result
})
return "Max steps reached without completing the goal."
Let’s break this down:
think()sends the goal, history, and available tools to the LLM. The LLM decides what to do next.execute_tool()runs the chosen tool and returns the result.historykeeps track of everything that’s happened so far - this is the agent’s short-term memory.- The loop continues until the LLM says it’s done or we hit the safety limit.
The think() function is where the real work happens:
def think(goal, history, tools):
tool_descriptions = format_tools(tools)
prompt = f"""You are an AI agent. Your goal: {goal}
Available tools:
{tool_descriptions}
Previous steps:
{format_history(history)}
Decide what to do next. Respond in JSON:
- If you need to use a tool: {{"status": "working", "reasoning": "...", "action": {{"tool": "tool_name", "input": "..."}}}}
- If the goal is complete: {{"status": "complete", "reasoning": "...", "final_answer": "..."}}
"""
response = call_llm(prompt)
return parse_json(response)
That’s the entire brain of your agent. The LLM receives context (goal + history + tools) and decides the next action.
Step 4: Add tools
Tools are functions your agent can call. Without them, it’s just a chatbot thinking out loud.
Each tool needs:
- A name the LLM can reference
- A description so the LLM knows when to use it
- A function that actually does the work
def search_web(query):
"""Search the web and return top results."""
# Use any search API (SerpAPI, Tavily, etc.)
results = search_api.search(query)
return results[:5]
def write_file(filename, content):
"""Write content to a file."""
with open(filename, "w") as f:
f.write(content)
return f"Written to {filename}"
# Define tools as a dictionary
tools = {
"search_web": {
"function": search_web,
"description": "Search the web for current information. Input: search query string."
},
"write_file": {
"function": write_file,
"description": "Write text content to a file. Input: JSON with 'filename' and 'content' keys."
}
}
The execute_tool() function simply looks up and calls the right one:
def execute_tool(tool_name, tool_input, tools):
if tool_name not in tools:
return f"Error: Unknown tool '{tool_name}'"
tool_fn = tools[tool_name]["function"]
try:
return tool_fn(tool_input)
except Exception as e:
return f"Error: {str(e)}"
Start with 2-3 tools. You can always add more later.
Step 5: Add memory
Your agent already has short-term memory - the history list that tracks actions within a single run.
For many tasks, that’s sufficient. But if you want your agent to remember things between runs, you need long-term memory:
import json
MEMORY_FILE = "agent_memory.json"
def save_to_memory(key, value):
"""Store information between sessions."""
memory = load_memory()
memory[key] = value
with open(MEMORY_FILE, "w") as f:
json.dump(memory, f)
def load_memory():
"""Load stored information."""
try:
with open(MEMORY_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
You can add save_to_memory and recall_from_memory as tools - then the agent decides when to store or retrieve information.
For our company research agent, short-term memory is enough. But if you were building something that learns about a user over time, long-term memory becomes essential.
When you’re thinking about how these pieces connect - the tools, memory, and core loop - you’re really doing system design. The same principles of separation of concerns and clear interfaces apply.
Step 6: Handle errors gracefully
Agents fail. Tools return errors, APIs time out, the LLM generates invalid JSON. Plan for it.
Tool failures:
def execute_tool(tool_name, tool_input, tools):
if tool_name not in tools:
return f"Error: Unknown tool '{tool_name}'. Available: {list(tools.keys())}"
tool_fn = tools[tool_name]["function"]
try:
return tool_fn(tool_input)
except Exception as e:
return f"Tool '{tool_name}' failed: {str(e)}. Try a different approach."
Return the error to the agent as an observation. A good agent will adapt - try a different tool or reformulate its approach.
Invalid LLM output:
def think(goal, history, tools, max_retries=3):
for attempt in range(max_retries):
response = call_llm(prompt)
try:
return parse_json(response)
except json.JSONDecodeError:
if attempt == max_retries - 1:
return {"status": "error", "reasoning": "Failed to produce valid response"}
# Give the LLM another chance with a clearer prompt
Infinite loops:
The max_steps parameter in the main loop is your safety net. Without it, a confused agent could loop forever. Ten steps is a reasonable default for simple tasks.
Step 7: Test and iterate
Run your agent on the task:
result = run_agent(
goal="Research the company 'Stripe' and write a 200-word summary of what they do, how they make money, and any recent news.",
tools=tools
)
print(result)
Watch for common problems:
- Loops - the agent repeats the same action. Fix: include history in prompts so it sees what it already tried.
- Wrong tool selection - the agent uses
write_filewhen it shouldsearch_web. Fix: improve tool descriptions. - Hallucinated actions - the agent tries to use a tool that doesn’t exist. Fix: be explicit about available tools in the prompt.
- Premature completion - the agent declares success before actually doing the work. Fix: add “verify your answer is based on tool results, not assumptions” to the system prompt.
Each failure mode tells you something about your prompts. Iterate on the prompt engineering - this is where most of the improvement comes from.
What to build next
Once your basic agent works:
- Add more tools - email sending, database queries, API calls to your business systems
- Add human-in-the-loop - before taking important actions (sending emails, making purchases), have the agent ask for approval
- Connect to real systems - move from toy examples to actual business workflows
- Add streaming - show the user what the agent is thinking and doing in real-time
- Deploy it - wrap it in an API endpoint or connect it to a messaging platform like WhatsApp
The pattern is what matters
Building your first AI agent is about understanding the core pattern: think → act → observe → repeat.
Once you have that loop working with one task and two tools, everything else is incremental. More tools, better prompts, richer memory - each improvement builds on the same foundation.
The technology is moving fast. The frameworks will change, the models will improve, the APIs will evolve. But the agent pattern - goal, tools, loop, memory - remains the same.
Start simple. Get it working. Then make it better.
If you’d rather have someone build AI agents for your business while you focus on running it, I offer AI solutions for organisations in Zimbabwe and beyond. From WhatsApp bots to custom research agents, I can help you implement AI that delivers measurable value.
You might also like: I Built an AI Agent for a Real Zimbabwean Workflow — Here’s What Broke | AI Agents vs Chatbots: What’s the Difference? | Designing Offline-First Sync Engines: Surviving Network Drops in Harare