Skip the theory overload. This guide walks you through building a functional AI agent that can search the web, analyze data, and write reports — with real Python code and practical patterns.
What We're Building
By the end of this guide, you'll have a working AI agent that can:
- Accept a research topic from the user
- Search the web for recent information
- Analyze and synthesize what it finds
- Generate a structured research report
- Save the report as a formatted document
We'll use Python, the OpenAI API (or any compatible LLM), and a few lightweight libraries. No massive frameworks required for your first agent — understanding the fundamentals matters more.
The Core Agent Loop
Every agent follows the same fundamental pattern:
while not done:
observation = gather_context()
thought = llm.think(observation + history)
action = parse_action(thought)
result = execute_tool(action)
history.append(thought, action, result)
done = check_if_complete(result)
This is called the ReAct pattern (Reasoning + Acting), introduced in a 2022 paper by Yao et al. The LLM reasons about what to do, takes an action, observes the result, and repeats until the task is complete.
Defining Tools
Tools are the agent's hands. Without tools, an LLM can only generate text. With tools, it can interact with the world. For our research agent, we need two tools:
- web_search — searches the internet and returns relevant snippets
- write_file — saves content to a file on disk
Each tool needs a clear name, description, and parameter schema. The LLM uses the description to decide when and how to use each tool — so write descriptions as if you're explaining the tool to a smart intern.
The System Prompt
Your agent's system prompt is its personality and instruction manual. A good agent system prompt includes:
- Role definition — "You are a research assistant that finds accurate, up-to-date information on any topic."
- Behavioral guidelines — "Always verify claims from multiple sources. Cite your sources. If you're unsure about something, say so."
- Output format — "Structure your final report with an executive summary, key findings, and sources."
- Constraints — "Do not fabricate statistics. Do not present opinions as facts."
Error Handling: Where Most Agent Tutorials Stop
Here's what separates a demo from a useful agent: error handling. Real-world agents encounter:
- Tool failures — APIs go down, rate limits hit, network timeouts. Your agent needs to retry with backoff or try an alternative approach.
- Hallucinated tool calls — The LLM might try to call a tool that doesn't exist, or pass invalid parameters. Validate every tool call before executing it.
- Infinite loops — Without a maximum iteration count, an agent can loop forever. Set a hard limit (typically 10-25 iterations) and gracefully exit with a partial result.
- Token budget overruns — Track cumulative token usage. If you're approaching your budget, instruct the agent to wrap up with what it has.
From Single Agent to Production
Once your basic agent works, here's how to evolve it:
- Add memory — Store conversation history and learned facts so the agent improves over time
- Add evaluation — Test your agent against known queries and measure quality
- Add observability — Log every LLM call, tool use, and decision for debugging
- Add human-in-the-loop — For high-stakes actions, pause and ask for human approval
Building your first agent is about understanding the pattern. Once you internalize the observe-think-act loop, you can build agents for any domain — customer support, data analysis, code generation, or workflow automation.
Start simple. Ship something that works. Then iterate.