Building AI Agents: A Practical Guide for Developers
AI agents are the next frontier: instead of one-shot Q&A, agents reason, plan, use tools, and iterate until they solve complex tasks.
This guide covers:
- What makes an agent vs a chatbot
- Core agent architectures (ReAct, Plan-and-Execute)
- Tool calling & function execution
- Memory & state management
- Real-world examples with code
By the end, you'll build a working agent that can browse the web, query databases, and chain multi-step workflows.
What Is an AI Agent?
Chatbot vs Agent
Example:
- Chatbot: "What's the weather?"
→ "It's sunny."
- Agent: "Plan my weekend."
→ Checks calendar → Searches events → Books restaurant → Adds to calendar → "Done!"
Core Agent Architecture: ReAct
ReAct = Reason + Act (repeat until done).
Flow:
- Think: "What do I need to do next?"
- Act: Call a tool (search, API, database)
- Observe: Process tool output
- Repeat: Until task is complete
Example (pseudo-code):
Task: "Find the CEO of Tesla and their latest tweet"
[Think] I need to search for Tesla's CEO.
[Act] search("Tesla CEO")
[Observe] Result: Elon Musk
[Think] Now I need to find their latest tweet.
[Act] twitter_search("from:elonmusk")
[Observe] Result: "Exciting progress on Starship..."
[Think] I have the answer.
[Respond] "Elon Musk, CEO of Tesla, tweeted: 'Exciting progress on Starship...'"Building Your First Agent
Requirements
- Python 3.10+
- OpenAI/Anthropic/Ollama API
- LangChain (agent framework)
Install
pip install langchain openaiMinimal Agent (20 Lines)
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
# Define tools
def search_tool(query):
# Placeholder: Integrate real search API
return f"Search results for: {query}"
tools = [
Tool(
name="Search",
func=search_tool,
description="Useful for finding information"
)
]
# Initialize agent
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description")
# Run agent
response = agent.run("Who is the CEO of Tesla?")
print(response)Output:
Thought: I need to search for Tesla CEO
Action: Search
Action Input: "Tesla CEO"
Observation: Elon Musk
Thought: I now know the answer
Final Answer: Elon MuskTool Calling (Function Calling)
Modern LLMs (GPT-4+, Claude 3+, Gemini) support native tool calling.
How It Works
- You define available tools (JSON schema)
- LLM decides which tool to call
- You execute the tool
- Return result to LLM
- LLM continues or finishes
Example: Weather + Calculator Agent
import openai
# Define tools
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
},
{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform math calculation",
"parameters": {
"type": "object",
"properties": {
"expression": {"type": "string", "description": "Math expression"}
},
"required": ["expression"]
}
}
}
]
# Tool implementations
def get_weather(location):
# Placeholder: Call real API
return f"Weather in {location}: Sunny, 22°C"
def calculate(expression):
return str(eval(expression)) # ⚠️ Dangerous in prod, use safe math parser
tool_map = {
"get_weather": get_weather,
"calculate": calculate
}
# Agent loop
messages = [{"role": "user", "content": "What's the weather in Paris? Also, what's 25 * 4?"}]
while True:
response = openai.chat.completions.create(
model="gpt-4-turbo",
messages=messages,
tools=tools
)
message = response.choices[0].message
# If no tool calls, we're done
if not message.tool_calls:
print(message.content)
break
# Execute each tool call
for tool_call in message.tool_calls:
func_name = tool_call.function.name
func_args = json.loads(tool_call.function.arguments)
# Call the function
result = tool_map[func_name](**func_args)
# Add result to conversation
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": result
})
# Add assistant's message (with tool calls)
messages.append(message)Output:
Weather in Paris: Sunny, 22°C
25 * 4 = 100Advanced: Plan-and-Execute Agent
ReAct works for simple tasks, but complex ones need planning.
Architecture:
- Planner: Break task into steps
- Executor: Run each step with tools
- Replanner: Adjust plan based on results
Example Task:
"Book a flight to Tokyo, find a hotel near the airport, and add to my calendar."
Plan:
- Search flights to Tokyo
- Pick cheapest option
- Search hotels near Narita Airport
- Book hotel
- Add trip to calendar
Implementation (LangChain):
from langchain.agents import PlanAndExecute, load_tools
tools = load_tools(["serpapi", "requests", "google-calendar"])
agent = PlanAndExecute.from_llm_and_tools(llm=OpenAI(), tools=tools)
agent.run("Book a flight to Tokyo, find a hotel, and add to my calendar")Memory & State Management
Agents need memory to track:
- Conversation history
- Intermediate results
- Long-term context
Types of Memory
Implementation (LangChain)
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
agent = initialize_agent(tools, llm, memory=memory)
agent.run("My name is Alice")
agent.run("What's my name?") # Agent remembers: "Alice"Real-World Agent Examples
1. Personal Assistant Agent
Tools:
- Google Calendar API
- Email (Gmail API)
- Web search
Tasks:
- "Schedule a meeting with Bob next Tuesday"
- "Summarize my unread emails"
- "Find articles about AI agents"
2. Data Analysis Agent
Tools:
- SQL database
- Python code execution
- Chart generation
Tasks:
- "Show me sales trends for Q1 2026"
- "Which product had the highest revenue?"
3. Code Review Agent
Tools:
- GitHub API
- Static analysis (pylint, eslint)
- Documentation search
Tasks:
- "Review PR #123"
- "Check for security issues"
- "Suggest improvements"
Common Pitfalls & Solutions
❌ Pitfall 1: Infinite Loops
Problem: Agent gets stuck repeating same action.
Solution: Add max iterations, track action history.
agent = initialize_agent(tools, llm, max_iterations=10)❌ Pitfall 2: Tool Hallucination
Problem: Agent calls non-existent tools or wrong parameters.
Solution:
- Provide clear tool descriptions
- Use strict JSON schemas
- Validate tool calls before execution
❌ Pitfall 3: Cost Explosion
Problem: Agent makes 50+ LLM calls for simple tasks.
Solution:
- Use cheaper models for planning (GPT-4 Mini, Claude Haiku)
- Cache repeated tool calls
- Set cost budgets
Frameworks & Tools
LangChain
Pros: Most mature, huge ecosystem
Cons: Complex API, steep learning curve
Best for: Production agents with many tools
AutoGen (Microsoft)
Pros: Multi-agent conversations
Cons: Newer, fewer integrations
Best for: Research, experimentation
Crew AI
Pros: Simple, opinionated
Cons: Less flexible
Best for: Quick prototypes
OpenAI Assistants API
Pros: Managed, zero infra
Cons: Vendor lock-in, limited tools
Best for: Simple bots, quick MVP
Benchmarking Agent Performance
Metrics:
- Success rate: Did it complete the task?
- Steps: How many iterations?
- Cost: Total LLM + tool API costs
- Time: Start to finish
Example:
Task: "Find cheapest flight to Tokyo"
- Agent A: 3 steps, $0.05, 12s → Success
- Agent B: 8 steps, $0.20, 45s → Success (but expensive)Next Steps
- Build a tool: Start with 1–2 tools (search, calculator)
- Test ReAct: Implement the loop manually
- Add memory: Persist state across sessions
- Go multi-agent: Have agents collaborate (planner + executor)
Resources
What are you building? Drop your agent ideas in the comments — let's brainstorm!
(Affiliate disclosure: Some links may include referral codes. I only recommend tools I actually use.)