Building an AI Agent from Scratch: No Magic, Just a Deterministic Loop
I was using Claude, Codex, Cursor, Gemini, Copilot, or Junie every day, but I still could not point to the exact line where “chatbot” turns into “agent”, I couldn’t explain what made them agents, So I wrote the naive version myself from scratch to find out.
For me, the best way to understand a new concept is to build it and explain it to someone. This article does both. I combined the story of the experiment with a practical tutorial, and I promise you’ll find it useful.
We’ll start with just 50 lines of Python, connect it to OpenAI, swap to local models via Ollama, build a mixed mode that uses both, add tools, implement MCP, and finally compare it to Claude CLI. By the end, you’ll see exactly what’s happening under the hood.
No LangChain. No LangGraph. No CrewAI. Just Python, an LLM, and a while loop.
What We’re Building (The Spec)
Before you build something, you have to define what it is and spec what it does.
An AI agent is a program that:
- Accepts a high-level task from a user
- Reasons about what to do next
- Takes an action (calls a tool, searches the web, reads a file)