Build an AI Agent
Create autonomous AI agents that can reason, plan, use tools, and complete complex multi-step tasks independently.
Introduction
AI agents represent the next evolution beyond chatbots and assistants. While an assistant responds to individual requests, an agent can autonomously plan and execute multi-step tasks with minimal human supervision.
In this advanced guide, you will build an AI agent from scratch that can reason about problems, create plans, use tools, and complete complex objectives on its own.
What Is an AI Agent?
An AI agent is a system with four core capabilities:
- Autonomy — It operates independently, making decisions without being told each step.
- Perception — It observes its environment and understands the current state of things.
- Reasoning — It thinks through problems, evaluates options, and creates plans.
- Action — It takes concrete actions using tools — searching, writing, computing, calling APIs.
💡 Agent vs. Assistant
An assistant waits for your instructions and executes one task at a time. An agent receives a goal and independently figures out the steps needed to achieve it, iterating until the goal is reached.
The Agent Loop
Every AI agent runs on a continuous loop of four stages:
Observe
The agent gathers information from its environment — the user's request, tool outputs, error messages, or new data.
Think
The agent reasons about what it has observed. It considers the current goal, what has been accomplished, and what the best next step would be.
Act
The agent executes its chosen action — calling a tool, writing a file, running code, or sending a request.
Reflect
The agent evaluates the result. Did the action work? Is it closer to the goal? Should it try a different approach?
Tools and Environment
Tools are what give agents their power. Without tools, an agent is just a language model. With tools, it can interact with the real world:
class Tool:
"""Base class for agent tools."""
def __init__(self, name, description):
self.name = name
self.description = description
def execute(self, **kwargs):
raise NotImplementedError
class WebSearchTool(Tool):
def __init__(self):
super().__init__(
"web_search",
"Search the internet for current information"
)
def execute(self, query):
# In production, use a real search API
return f"Search results for: {query}"
class FileWriteTool(Tool):
def __init__(self):
super().__init__(
"write_file",
"Write content to a file on disk"
)
def execute(self, filename, content):
with open(filename, 'w') as f:
f.write(content)
return f"Written to {filename}"
class CodeExecutionTool(Tool):
def __init__(self):
super().__init__(
"run_code",
"Execute Python code and return the output"
)
def execute(self, code):
# WARNING: In production, use sandboxed execution!
import subprocess
result = subprocess.run(
["python", "-c", code],
capture_output=True, text=True, timeout=30
)
return result.stdout or result.stderrBuilding an Agent
Let us build a complete AI agent with the observe-think-act loop:
from openai import OpenAI
import json
class AIAgent:
def __init__(self, name, goal, tools):
self.name = name
self.goal = goal
self.tools = {tool.name: tool for tool in tools}
self.client = OpenAI()
self.memory = []
self.max_iterations = 10
def _get_tool_descriptions(self):
return [
{
"type": "function",
"function": {
"name": t.name,
"description": t.description,
"parameters": {"type": "object", "properties": {}}
}
}
for t in self.tools.values()
]
def think(self, observation):
"""The agent's reasoning step."""
self.memory.append({"role": "user", "content": observation})
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""You are {self.name},
an autonomous AI agent. Your goal: {self.goal}
Available tools: {list(self.tools.keys())}
Think step by step:
1. What have I learned so far?
2. What should I do next?
3. Which tool should I use?
4. Am I done with my goal?
Respond with JSON:
{{"thought": "your reasoning",
"action": "tool_name or DONE",
"action_input": {{"param": "value"}}}}"""},
*self.memory
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
def run(self, initial_task):
"""Main agent loop."""
print(f"Agent '{self.name}' starting: {initial_task}")
observation = initial_task
for i in range(self.max_iterations):
print(f"\n--- Iteration {i+1} ---")
# Think
result = self.think(observation)
print(f"Thought: {result['thought']}")
# Check if done
if result["action"] == "DONE":
print(f"\nAgent completed: {result['thought']}")
return result["thought"]
# Act
tool = self.tools.get(result["action"])
if tool:
try:
output = tool.execute(**result.get("action_input", {}))
observation = f"Tool '{tool.name}' returned: {output}"
print(f"Action: {tool.name} -> {output[:200]}")
except Exception as e:
observation = f"Error: {str(e)}"
else:
observation = f"Unknown tool: {result['action']}"
self.memory.append(
{"role": "assistant", "content": json.dumps(result)}
)
return "Max iterations reached."
# Create and run the agent
agent = AIAgent(
name="Research Assistant",
goal="Research a topic and write a summary report",
tools=[WebSearchTool(), FileWriteTool(), CodeExecutionTool()]
)
agent.run("Research the latest trends in AI agents for 2025")Multi-Step Planning
For complex goals, agents benefit from creating an explicit plan before execution:
class PlanningAgent(AIAgent):
"""Agent that creates a plan before executing."""
def create_plan(self, task):
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""Create a step-by-step
plan to accomplish this task. Available tools: {list(self.tools.keys())}
Return JSON: {{"steps": ["step 1", "step 2", ...]}}"""},
{"role": "user", "content": task}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
def run(self, task):
# First, create a plan
plan = self.create_plan(task)
print(f"Plan: {plan['steps']}")
# Then execute each step
results = []
for step in plan["steps"]:
result = super().run(step)
results.append(result)
return results✅ Planning Strategy
Have the agent create a plan first, then execute it step by step. If a step fails, let the agent re-plan from the current state rather than starting over. This is more efficient and mirrors how humans handle complex tasks.
Safety and Guardrails
Autonomous agents need safety measures. Here are critical guardrails:
- Sandbox Execution — Run agent code in isolated environments. Never give agents unrestricted access to your filesystem or network.
- Iteration Limits — Set maximum iteration counts to prevent infinite loops. Most tasks should complete in under 10 steps.
- Human Approval Gates — For high-stakes actions (deleting files, sending emails, making purchases), require human confirmation.
- Comprehensive Logging — Log every thought, action, and result. This is essential for debugging and auditing agent behavior.
⚠️ Critical Safety Warning
Never give an AI agent unrestricted access to execute code, manage files, or make financial transactions without human oversight. Start with read-only tools and gradually expand capabilities as you build trust.
Summary
You have built an autonomous AI agent. Key takeaways:
- AI agents operate on a continuous observe-think-act-reflect loop.
- Tools give agents the ability to interact with the real world.
- Planning before execution improves success rates for complex tasks.
- Safety guardrails (sandboxing, limits, human approval) are not optional — they are essential.