• Home
  • /
  • Blog
  • /
  • How to Build AI Agents in Python: A Beginner’s Guide (2026)

How to Build AI Agents in Python: A Beginner’s Guide (2026)

I’ve watched hundreds of tutorials on AI agents, and honestly? Most of them just make my head hurt. They talk about “orchestration” and “Kubernetes” before you’ve even written a line of code.

But recently, I spent 30 minutes watching a guide by Cole Medin, and it clicked. He argues that you can learn 90% of what you need by ignoring the complicated stuff and focusing on just four things.

I tested his method on my laptop, and it actually works. You don’t need to be a genius to do this. You just need to stop overthinking it.

Here is the simple, honest shortcut to building your first AI agent.


The 4 Parts of Every Agent

Before we touch the code, you need to understand what we are building. An AI agent isn’t magic. It’s just a Large Language Model (LLM) that can do things.

Think of it like this:

  • The Brain (LLM): Decides what to do.
  • The Program (System Prompt): Creating the persona and rules.
  • The Hands (Tools): Functions it can run (like searching the web or using a calculator).
  • The Memory (Context): Remembering what you said 5 minutes ago.

Note: If your bot doesn’t have tools, it’s not an agent. It’s just a chatbot.


Step 1: Pick Your Brain (The LLM)

First, we need to choose which AI model will power our agent.

I used to stress about picking the “perfect” model, but here’s the truth: It doesn’t matter yet. When you are prototyping, you want something cheap and fast.

I recommend using OpenRouter. It’s a platform that lets you swap between models (like Claude, GPT-4, or Gemini) by changing one line of code.

For this guide, I used Claude Haiku 4.5. It’s fast and smart enough for testing.

The code snippet showing the LLM definition using OpenRouter
The code snippet showing the LLM definition using OpenRouter

You basically tell the code: “Hey, use this specific model ID.” If you want to switch to GPT-4 later, you just change that string text. Easy.


Step 2: Write the “God Mode” Instructions

This is called the System Prompt. This is where you tell the agent who it is.

Don’t overcomplicate this. You don’t need a 50-page manual. I use a simple template that covers the basics:

  • Persona: “You are a helpful coding assistant.”
  • Goal: “Help the user write clean Python code.”
  • Output Format: “Be concise and use code blocks.”
The system prompt template with sections for Persona, Goal, and Output
The system prompt template with sections for Persona, Goal, and Output

Warning: If you don’t define clear goals, your agent will ramble. Keep it specific!


Step 3: Give It Hands (Tools)

Now for the fun part. We need to give our agent the ability to interact with the world.

In Python (specifically using pydantic_ai or similar libraries), you do this by writing a normal function and adding a “decorator” on top.

For example, LLMs are notoriously bad at math. So, let’s give it a calculator tool.

  1. Write a basic add function: You write a Python function that takes two numbers, adds them, and returns the result.
  2. Add the decorator: This tells the AI, “Hey, this is a tool you can use!”
The Python function 'add_numbers' with the tool decorator above it
The Python function ‘add_numbers’ with the tool decorator above it

The Docstring is Key: See that text inside the function (the comment)? That is not for you. That is for the AI. It tells the agent when to use this tool. If you leave it blank, the agent won’t know this tool exists.


Step 4: Run the Loop

Finally, we need a way to talk to it.

We set up a simple loop in the terminal:

  1. Wait for user input.
  2. Send input + conversation history to the Agent.
  3. Agent decides if it needs to use a tool (like our calculator).
  4. Agent executes the tool and gives a final answer.
  5. Print the answer.
The terminal window showing the interaction "Hi" and a math question
The terminal window showing the interaction “Hi” and a math question

When I tested this, I asked “What is 5435345 + 5436346346346?”. The agent didn’t guess. It saw the math question, used my add_numbers tool, and gave me the exact answer.


Common Pitfalls (Read This!)

I made these mistakes so you don’t have to.

1. Too Many Tools

Don’t give your agent 50 tools. Keep it under 10. If you give it too many options, it gets confused and starts using the wrong ones. If you need more, build a second agent.

2. Ignoring Security

I found this part scary, but important. Never hardcode your API keys. If you put your OpenAI key directly in the code, bots will steal it in seconds. Use environment variables (like a .env file).

Also, consider using Guardrails. It’s a way to stop your agent from saying offensive things or leaking private data.

3. Over-Engineering Memory

You might be tempted to build a complex “infinite memory” system. Stop. For 90% of beginners, simply passing the last 10-20 messages is enough. Only look into vector databases (like Mem0) when you actually hit a limit.


Conclusion

Building an AI agent feels overwhelming, but it’s really just connecting a brain (LLM) to some hands (Tools).

You can build a proof of concept in less than 50 lines of Python code. Start with one simple tool, get it working, and then worry about the fancy stuff. Have you tried building a simple agent yet? Let me know what tool you gave it first—I’m curious to see what you made!


FAQs

  1. What is the difference between a Chatbot and an AI Agent?

    Think of a chatbot (like standard ChatGPT) as a brain in a jar. It can think and talk, but it can’t touch anything. An AI Agent is that same brain, but you gave it “hands” (tools). It can use a calculator, search Google, or open a file on your computer.

  2. Do I need to be a Python expert to build this?

    No. If you know the very basics of Python (like how to define a function), you are ready. As shown in the guide, you can build a working prototype in less than 50 lines of code. You don’t need to overcomplicate it.

  3. Which AI model should I start with?

    I recommend Claude Haiku 4.5 or GPT-4o Mini for testing. They are very fast and very cheap. Don’t stress about picking the “perfect” model right now—using a tool like OpenRouter lets you switch models by changing just one line of text later.

  4. Is this free to run?

    It is extremely cheap, but not 100% free if you use powerful models like GPT-4. However, for the simple agent in this guide, you will likely spend less than $1 USD while testing. If you want it to be free, you can run local models (like Mistral) on your own laptop, but that is a bit harder to set up.

  5. How do I give my agent “infinite” memory?

    Be careful with this! Beginners often try to make the agent remember everything, and it breaks the bot. For your first agent, just let it remember the last 10-20 messages. That is enough for 90% of use cases. Don’t build a complex database until you actually need it.

Leave a Reply