Building AI Agents: A Practical Guide for Developers
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
| Feature | Chatbot | Agent | |---------|---------|-------| | **Task** | Answer question | Solve problem | | **Iterations** | Single response | Multiple steps | | **Tools** | None | Uses external tools | | **Memory** | Conversation only | Persistent state | | **Autonomy** | Reactive | Proactive |
**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:
1. **Think:** "What do I need to do next?" 2. **Act:** Call a tool (search, API, database) 3. **Observe:** Process tool output 4. **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 openai
Minimal 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 Musk
---
Tool Calling (Function Calling)
Modern LLMs (GPT-4+, Claude 3+, Gemini) support **native tool calling**.
How It Works
1. You define available tools (JSON schema) 2. LLM decides which tool to call 3. You execute the tool 4. Return result to LLM 5. 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 = 100
---
Advanced: Plan-and-Execute Agent
**ReAct** works for simple tasks, but complex ones need **planning**.
Architecture:
1. **Planner:** Break task into steps 2. **Executor:** Run each step with tools 3. **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:** 1. Search flights to Tokyo 2. Pick cheapest option 3. Search hotels near Narita Airport 4. Book hotel 5. 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
| Type | Use Case | Example | |------|----------|---------| | **Short-term** | Current task context | Chat history | | **Long-term** | Cross-session persistence | User preferences, past tasks | | **Vector** | Semantic search | RAG over past conversations |
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)
---
Recommended Learning Resources
Books
- [Hands-On Machine Learning](https://www.amazon.com/dp/1492032646?tag=techkutak-20) - Foundational ML knowledge for building agents - [Deep Learning with Python](https://www.amazon.com/dp/1617296864?tag=techkutak-20) - Neural networks and LLM architecture - [Python Crash Course](https://www.amazon.com/dp/1593279280?tag=techkutak-20) - If you need Python basics
Hardware for Local Agent Development
- [Raspberry Pi 5 (8GB)](https://www.amazon.com/dp/B0CTQ3BQLS?tag=techkutak-20) - Perfect for prototyping agents - [NVIDIA RTX 4070](https://www.amazon.com/s?k=rtx+4070&tag=techkutak-20) - Run agents with local LLMs - [Samsung 2TB SSD](https://www.amazon.com/dp/B08RK2SR23?tag=techkutak-20) - Store models and agent state
Next Steps
1. **Build a tool:** Start with 1–2 tools (search, calculator) 2. **Test ReAct:** Implement the loop manually 3. **Add memory:** Persist state across sessions 4. **Go multi-agent:** Have agents collaborate (planner + executor)
---
Resources
- [LangChain Docs](https://langchain.com) - [OpenAI Assistants Guide](https://platform.openai.com/docs/assistants/overview) - [AutoGen Framework](https://github.com/microsoft/autogen) - [Crew AI](https://crewai.com)
---
**What are you building?** Drop your agent ideas in the comments — let's brainstorm!
---
**💡 Affiliate Disclosure:** This article contains Amazon affiliate links. If you purchase through these links, we earn a small commission at no extra cost to you. We only recommend products we personally use and trust.