Published on 6 October 20262 min read

What an AI agent is and how to build one from scratch with Python


What an AI agent is and how to build one from scratch with Python

A chatbot and an agent are not the same

An LLM chatbot receives text and returns text. An agent receives a goal, decides what to do, executes actions, and checks the result. The quick test: if your code can check the time, read a file, or call an API without you explicitly asking it to, you have an agent.

Anatomy of an agent

Four pieces, no mystery:

  • Reasoning loop: the LLM decides the next step based on the goal and the history.
  • Tools: Python functions the model can invoke (search, calculate, save).
  • Memory: the state of the conversation and of what has already been tried.
  • Execution: your code launches the action and returns the result to the model.

Building it in Python, step by step

Let's go with an agent that, given a topic, searches for data and writes a summary.

  1. Define the tools as normal, well-documented functions: search_web(query) and save(text).
  2. Describe each tool to the model in JSON (name, description, parameters). Bad descriptions, bad agent.
  3. Set up the loop: send the goal plus the history and see if the response asks for a tool.
  4. If it does, execute it yourself, add the result to the history, and call the model again.
  5. Stop when the final response arrives or you exceed a maximum number of iterations. That limit is not optional: without it, your agent keeps thinking and your API bill goes up.

With the Anthropic or OpenAI API it's about 80 lines. The key isn't the framework, it's being clear about what tools you give it and how you stop.

What usually fails

Ambiguous descriptions, loops without a cap, and memory that grows uncontrolled. Start with two tools and a specific task; add sophistication when the simple version works.

If you want to build it guided and with real APIs, at Clasesdeprogramacion we build it with you from the first line.

Want classes like these?

Book your free 15-minute interview and tell me your goal.

Book interview

No commitment · I reply within 24 hours

Related articles