"AI agent" is the most hyped term in tech right now, and also one of the least understood. Strip away the buzz and an agent is surprisingly simple: a language model in a loop that can call functions you give it. Build one yourself and you will understand agent frameworks, interview questions about agentic AI and most of the news headlines in one go.
This guide builds a working agent from scratch in plain Python, with no framework. The agent gets three tools (a safe calculator, a Wikipedia lookup and a reader for your own notes), decides which one a task needs, and stops after a fixed number of steps so it can never loop forever. You will see every line, then the guardrails, the tests and the project ideas that turn it into a strong submission.
New to the idea? Read what an AI agent is first; it takes five minutes and makes the code below obvious.
What is an AI agent project?
An AI agent project is a program where a language model chooses actions (tool calls) to complete a goal, instead of only replying with text. The model plans, the code acts, and the result goes back to the model for the next decision.
Compare three things you might build:
- A chatbot answers from what the model already knows.
- A RAG chatbot retrieves passages from your documents first, then answers.
- An agent decides for itself which tool to call, how many times, and when it is done.
That last part, the model choosing the next step, is what makes it an agent. A simple example of an agent in AI: ask "How many years before 2026 was the Taj Mahal commissioned?" The agent looks up the Taj Mahal on Wikipedia, reads that it was commissioned in 1631, calls the calculator with 2026 - 1631, then answers. Nobody wrote that sequence; the model chose it.
If you are unsure where single agents end and multi-agent systems begin, see AI agents vs agentic AI.
What do you need to build an AI agent in Python?
You need Python 3.10 or newer, the OpenAI Python SDK and an API key for a chat model that supports tool calling (also called function calling). No GPU and no agent framework are required.
| Part | What we use | Why |
|---|---|---|
| Language | Python 3.10+ | Standard library covers the tools |
| LLM access | openai Python SDK | Its tool-calling format is widely copied by other providers |
| Model | Any tool-calling chat model, set by an environment variable | Swap models without touching code |
| Tools | ast, urllib, pathlib | All built into Python |
| Wikipedia data | Wikipedia REST summary endpoint | Free, no key needed |
You should be comfortable with Python functions, dictionaries and loops, and with reading JSON. If LLMs are still a black box to you, spend a few minutes there first.
How does a tool-using AI agent work? (architecture)
A tool-using agent works as a loop: the model either answers or asks for a tool, your code runs the tool and sends back the result, and the loop repeats until the model answers or a step limit is hit.
- You send the task plus a list of tool descriptions (name, purpose, parameters as JSON Schema).
- The model replies with either a final answer or one or more tool calls, each with a tool name and JSON arguments.
- Your code runs each requested function and appends the result to the conversation as a message with role "tool", linked by the tool call's id.
- The model sees the results and decides the next step.
- The loop ends when the model answers without a tool call, or when you reach max_steps.
The model never runs code itself. It only asks; your program decides whether and how to run anything. That separation is where all the safety in an agent lives.