How to Build a Simple AI Agent in Python for Free (No API Key Needed)
Most AI agent tutorials assume you're paying for the OpenAI API. This one doesn't. You'll build a real, working AI agent in plain Python using Ollama, a free tool that runs open-source language models directly on your own computer. No API key, no billing, no framework.
What Is an AI Agent, Really?
A regular chatbot takes one message and gives one reply. An AI agent is different: it can decide to take an action, look at the result, and decide what to do next, repeating that loop until the task is done. That loop, think, act, observe, repeat, is what separates an agent from a simple prompt-and-response script.
What You'll Need
- Python 3.10 or newer
- About 8GB of free RAM
- Ten minutes, and zero dollars
Step 1: Install Ollama
Ollama lets you download and run open-source models like Llama and Qwen on your own machine, completely free. Go to ollama.com, download the installer for your operating system, and install it like any normal app.
Once installed, pull a small, capable model that supports tool calling:
This downloads the model once, then it runs locally forever with no internet connection and no cost.
Step 2: Set Up Your Project
That's the only package you need. No paid SDK required.
Step 3: Give Your Agent a Tool
An agent becomes useful the moment it can do something beyond talking, like running a calculation. Here's a simple tool function the agent will be able to call on its own.
Step 4: Write the Agent Loop
This is the core of the tutorial. The agent receives a goal, asks the local model what to do, and either calls the tool or finishes with an answer.
Step 5: Run It
Make sure Ollama is running in the background (it starts automatically after installation), then run:
The agent reads your goal, decides it needs the calculate tool, runs it, feeds the result back to itself, and returns a final answer, all running locally on your machine at zero cost.
Why Local Instead of a Paid API?
Running the model locally means unlimited experimentation. You can rerun the agent hundreds of times while you learn, without watching a bill climb. The tradeoff is that a local model is a bit slower and less capable than the largest hosted models, but for learning how agents work, that tradeoff is well worth it.
When You'd Move to a Paid API
Once you're comfortable with the pattern, you might switch to a hosted model for production use, faster responses, or a smarter model on bigger tasks. But there's no reason to pay anything just to learn how agents work.
Final Thoughts
You now have a working AI agent built entirely in free, local Python, no API key required. From here, try swapping in your own tool, a file reader, a to-do list manager, a web scraper, and watch the same loop handle it without any changes to the core logic.