Build AI Agents With Python From Scratch: A Plain Step By Step
There are two ways to think about AI agents. One is to buy a tool and hope it covers your case. The other is to build a small agent yourself with a little code, which sounds scary until you realize the modern way to do it in Python is shockingly plain. You do not need a machine learning degree to build agents from scratch, and often a full framework is overkill for what you are trying to do. This guide walks through how to build an AI agent with Python using plain function calls, an API key, and some thoughtful prompting.
Why Plain Python Beats A Framework To Start
Frameworks promise a lot and deliver complexity. They add abstractions, config files, and learning curves that bury the part you actually care about, which is telling the model what to do and acting on the result. When you are just starting, a plain script with a few functions is easier to read, easier to debug, and easier to understand, meaning more than any feature of the framework. Most simple agents are really just a loop: read the situation, ask the model, do something with the answer, repeat until done.
The Tiny Core Of Any Agent
At its heart an agent is three pieces of plain Python. One function calls the model with your prompt. One function holds the current state, like the customer name or the file being worked on. And one function does an action, like saving a file or sending an email. You wire these in a loop that keeps going until the task is done. That skeleton is not a toy. Many reliable agents that run every day are exactly this small, and the power comes from what you put in the prompt and how you manage state, not from clever framework code.
Setting Up The Environment
You need two things to get going: Python with the requests library and an API key. Create a virtual environment, install requests, and keep the key in a local file you never commit to a repo. Then write a single function that sends a JSON payload to the model and returns the text response. If you can do that, you can already call the model programmatically, which is the foundation the rest of this is built on. Test that one function before doing anything else.
Turning The Call Into A Loop
The step that turns a one off script into an agent is making it repeat until it is done. Define the task, then loop the call, check the result against a simple rule, and decide whether to finish or try again. For a research agent, the loop would keep gathering sources until it has five solid ones. For a drafting agent, it would keep refining until the answer passes a word count. The loop with a clear stopping rule is the part people find most useful, and once it is on the page it all feels obvious.
What A Real Minimal Agent Looks Like
Picture an email summarizing agent. It starts with the raw inbox, and the loop takes registrations, calls the model to turn the message into a one line summary and a priority, writes those to a spreadsheet, and asks the model if the inbox rule should change. Just three functions and a loop. This kind of tiny agent is reliable, easy to fix, and genuinely useful, which is why whole software projects have lived on little more than this shape. The practical guide to AI employees shows how far these plain systems run for small businesses.
Growing It Without Rebuilding
When the basics work, growth is just adding more functions and a smarter state. Add memory with a small file the agent reads and writes. Add guardrails with a check function that stops the loop on risky output. Add multiple steps by chaining functions together. Because the base is plain Python, each addition is a few lines you fully control, and you never get stuck fighting a framework you stopped understanding months ago. That control is worth more than any shine, and it is the same reason serious teams keep their core simple.
Debugging When Goals Do Not Finish
When a loop goes wrong, nine times out of ten it is the stopping rule, not the model. Either the condition is too strict and the loop never ends, or too loose and it stops early. Print the state each pass, watch the loop bounce, and tighten the rule until it exits reliably on good work. A timeout guard also helps draft agents from running forever. Debug with print, just like normal Python, because it is normal Python, and the same skills everyone already has will carry you. If you want to lean on work that has already been done, the AI influencer team playbook shares patterns for coordinating simple agents, and the AI employees starter kit includes the foundation scripts to start from. Building agents in Python is a skill anyone can pick up in a weekend, and this is the gentle on ramp.
Set it up the right way
The book walks through the full system: 4 files, the org chart, the failure modes, and a 30-day blueprint. $29, plain English, 30-day refund.
Get the book, $29