---
title: 'Build Your First AI Agent in Python (Step by Step)'
source: 'https://youtube.com/watch?v=YKPO2S6j7MM'
video_id: 'YKPO2S6j7MM'
date: 2026-06-17
duration_sec: 738
---

# Build Your First AI Agent in Python (Step by Step)

> Source: [Build Your First AI Agent in Python (Step by Step)](https://youtube.com/watch?v=YKPO2S6j7MM)

## Summary

This tutorial provides a step-by-step guide to building your first AI agent in Python using the open-source framework Agentspan. It covers setting up prerequisites, defining tools and agents, running them, and making them resilient to crashes. The video is sponsored by Agentspan and aims to make AI agent development accessible to beginners.

### Key Points

- **Prerequisites** [0:27] — Install Python 3.10+, JDK (version 25 or 21), and a package manager like uv.
- **Project Setup with uv** [2:09] — Use `uv init` to create a project and `uv add agentspan` to add the framework.
- **Defining a Tool** [3:46] — A tool is a Python function with the @tool decorator; its name, type hints, and docstring guide the AI.
- **Agent Runtime Architecture** [5:14] — The agent runs on the Agentspan server; the Python script is just a client.
- **Agentspan Dashboard** [6:11] — The Agentspan dashboard records every execution and allows inspection of tool calls.
- **Crash Resilience** [9:48] — Use `runtime.start()` to get an execution ID and `agent.resume()` to recover from crashes.
- **Idempotency** [11:30] — Tool functions must be idempotent to avoid duplicate results when retried.

## Transcript

Today, let's build your first AI agent in Python step by step. We'll start with
the basics like how to define an agent, define a system prompt, and how to
write tools for your agent to use. Then we'll move on to make sure your
agent survives crashes halfway through its work. I'm David, and today we'll use a free
open-source framework called Agentspan. They're also the sponsor of today's video. Let's get started.
Before we jump in, there are a couple of prerequisites to install. First up is
obviously Python, and you'll need at least
version 3.10. If you don't have Python already, head to this page, you'll find
a link to it inside the resource guide, which is in the description below. Once
you're here, click on the yellow button at the top of the screen to get
set up. Next up is the Java Development Kit, otherwise known as the JDK. You'll
find a link to this page in the resource guide as well, and once you're
here, click on the appropriate download either Windows or macOS. Unless you have a
reason otherwise, install the latest long-term support JDK.
That's 25 as of filming. But if you need to stay on 21, that's no
problem. Agentspan supports that just fine. Finally, if you are new to Python, I recommend
installing a package manager called uv. We'll use this throughout today's tutorial, so choose the
proper operating system, either Mac or Windows, and then copy the command that shows up
first in the list. On Mac, you can use the terminal to run this. On
Windows, you can open up a new PowerShell window by opening the start menu and
then typing PowerShell.
Click on Windows PowerShell to open it and then paste that command directly in the
terminal. Run it by pressing Enter, and you should see everything's installed.
Before you go, make sure you run this command if you're on Windows. This is
also in the resource guide to make your life a lot easier. With prereqs
out of the way, let's get into VS Code.
Start off by opening an empty folder to work in using this open folder link.
Next, in order to run Python, we need to use the terminal. You can access
it by clicking the view menu then terminal.
Python projects need their own set of dependencies, and that's what we'll use uv for.
If you're comfortable with them for something else, feel free to substitute that here.
But for the rest of you, type uv, space, init, and then enter to
create your project. Then type uv, space, add, space, agentspan to add Agentspan
and all of its dependencies to your project.
To make sure everything installed correctly, run this command with me. uv, space, run, space,
agentspan, space, doctor. Now, chances are you won't see all green here, and it'll
be for one of two reasons.
First would be if your AI provider is not set up correctly. Now, Agentspan
spun up my Ollama instance here locally, but some of you might want to use
OpenAI or Anthropic to connect your agent.
If you want to use OpenAI, you can head over to platform.openai.com and
then click on API keys on the left and create new key. Very similar for
Anthropic and Claude. Just head over to platform.claude.com, go over to manage API
keys, and then you can create a new key.
Once you've got your key, set the corresponding environment variable. So for OpenAI, set OPENAI
API key. In PowerShell, that would look something like this.
Now, the second issue you might run into is this certificate verification failure. This is
especially prevalent on Mac. So if your screen looks like this, run the command below,
you'll also find this in the resource guide.
With that, setup is now done. Let's write the agent. Let's get rid of the
terminal first and start by opening the main Python file.
Let's first get rid of all the auto-generated code. Building an agent might seem
daunting, so let's break it down into three steps.
Step one, write the tools the agent can use. Step two, write the agent itself,
and step three, write code to run the agent. So let's start with the tool.
A tool is just a Python function that the agent can call. This is a
very simple function called get_weather, and this @tool decorator exposes it to the agent.
Something that beginners can easily miss is that the function name, the type hints on
the parameters, as well as the return value, as well as the docstring all
have a purpose.
They give the AI agent clues on what this tool is used for and how
to use it. So if you got rid of the docstring and then called
this tool one, the AI agent might not even call this if you asked about
the weather.
Now, of course, in production, you hit a real weather API here, but this hard
coded string is fine for the demo. We just need something for the agent to
invoke.
Before we move on, we still got the squiggly line under tool. That means we
haven't set up our imports properly yet.
That's a one liner you can pull from the resource guide. Now let's set up
the agent. The agent has three required fields, a name, the AI model name, as
well as the tool list.
The model string here uses the format provider slash model name. You can typically find
these model names just by searching for the provider and then model names.
What you're looking for is text that looks like this, where it's GPT-5
.5, 5.4-mini, and so on.
Today, we're going to use 5.4-mini for its speed, and notice that I've
already loaded up the get_weather function as a tool.
The third piece has the code to run the agent here. We create an instance
of an agent runtime that connects to the Agentspan server.
The agent actually runs on that server and your Python process is just the client.
The reason for this will become clear later to actually call the agent though.
We call the runtime.run function and we pass in a simple prompt here. We're
just asking the weather in New York City to run this.
We have to start the Agentspan server. So let's open a terminal window and
this time we'll type in uv run agentspan server start.
Now the first time you run this, it takes about 30 seconds. Every startup after
that is pretty quick.
Now we've got this running. We can run the agent just by using uv run
main.py. This takes about 10 seconds to spin up. So let's see what
it's actually doing.
These log entries tell you the agent's been kicked off. Anything with Conductor in the
title is basically Agentspan server infrastructure.
If we scroll down, you'll see the weatherbot completed and we got the agent
output. We see it called the tool and output the results directly.
Now a cool thing about Agentspan is that there's a web app sitting on
the server. This is what it looks like.
Now down below you see I've done two runs of the weatherbot. This is
the Agentspan dashboard. It's where every agent execution gets recorded.
Each of the rows in this table is called an execution. And if you click
on any one of the execution IDs, you get a visual representation of the agent
workflow.
So the weatherbot is pretty simple. It started with prompting what's the weather in
NYC. That made its way to the AI model.
And yes, you can click on any node to get more detail. ChatGPT saw
that it needed to call the get_weather tool, which it did.
It went back to the model and then returned the output to inspect a tool
call. All you have to do is click on the tool call node and then
look at the input and the output.
Here on the input tab, you can see the exact parameters that were passed in
and on the output tab, you can see the result.
Because the Agentspan server owns the agent state, it's resilient to crashes. So let's
show that by building an agent that actually has multiple steps to interrupt.
In this next section, we'll write an agent that writes unit test cases and we'll
run it against this price calculator module on screen.
There are just four simple functions here that calculate discounts, taxes, shipping and total costs.
We'll leave the main Python file behind and create a new one called agent.py.
This time we'll start off with the imports so we don't have to worry about
them. And then do you remember the next step? That's right, we got to put
the tools in.
This tool is a little bit more complex. So let's tackle it together. The idea
is that this writes test cases for every test.
We get the function name being tested and then the AI model generates all the
code. All the tool does is write the test code to a file.
Next up, let's define the agent. It's not much different from the last file. I've
just renamed it to testbot and given it the new tool name.
Finally, we've got to run the agent. The code's broken up into three parts. Part
number one, we're writing the imports for the test file.
Part number two, we're reading the code file and then part number three, we're actually
starting the agent.
This prompt is a bit longer than the last one. So let's cover the key
parts. First, we're asking the agent to write tests for each function below.
Now below, we're writing the entire Python file source code. So that shows up here.
And then we're telling it explicitly call this tool once per function with the function
name and the test code.
We want raw Python, not Markdown. Now I think you're starting to get the
pattern here. We save our file, open the terminal once again and then use uv
run to run the agent.
It takes about 15 seconds. You should see agent testbot completed. There's now a
new file in your workspace called test_price_calculator inside of it.
You'll see test cases for each of the four functions in the original price calculator
module. And if you go back to the Agentspan server dashboard and click into
the new execution, you'll see a new type of run.
All four tool calls were done in parallel and you can click into any of
them to see the actual test code generated.
This is a pretty resourceful way to debug your agent if something goes wrong. Now
let's break the agent on purpose. We'll kill the Python process mid run and see
how it survives the crash.
Now we're looking at a new version of the agent file called resilient_agent.py.
There are a few changes here. The first one I want to call out relates
to this execution ID variable.
Just like before we have an agent that can run the save_tests tool, but
now the way we run it is different. If execution ID doesn't exist, it's the
same flow as before.
But if it does exist, then we are going to resume an existing run to
get the execution ID to start. We have to call runtime.start instead
of runtime.run.
That returns an agent handle with an execution ID property. Once we have that ID,
if the agent crashes, we use this agent handle. It's a reference to an execution
that already exists on the server.
You hand it the execution ID and it knows how to resume the agent stream.
So let's see how this works in practice. Now I have made some changes to
the tool call.
The most important of which is that it sleeps for two seconds. That gives us
a chance to interrupt the agent before it completes all four calls.
So let's run this and our job is to stop this halfway through it writing
tests. The easiest way I found to do that is just to watch this file
and then stop it as soon as it writes one test.
Okay, so that was actually pretty lucky. Took me about 10 minutes to get that
right. Hopefully you got it faster. Here's the fun part. The client says we've got
test cases for calculate discount.
If we go to the server though and look at this latest execution, it actually
says that none of those tool calls succeeded. So the agent started fine, but no
tool calls actually finish.
And that's most likely because we killed it in the middle of that sleep. We
need the execution ID to retry this. We can grab this from the top of
the screen.
Then back in the resilient_agent file, we'll assign it to the execution ID variable. Let's
open up the test_price_calculator and then let's run this one more time. We'll
see what happens.
We see immediately that it resumed an existing agent run instead of starting a new
one and inside the test file, we see four sets of tests. There's one, two,
three, and four.
Now you might be wondering if it had to retry all of those tool calls.
Why didn't we get a duplicate set of tests? That raises a very important point
about your tool calls. They have to be idempotent.
That describes a function that you can run multiple times without changing the end result.
This version of save_tests does not write the test cases if it already finds
them in the file.
Finally, if you go back to the dashboard and look at this same run, you'll
see all the tool calls have completed. And yes, this is the same execution ID.
And that's a unique thing that Agentspan can do for you because your agent's state
lives on the server. Your Python script can crash, restart, or even move to another
machine and the agent just doesn't care. It just keeps going.
One more tip before you leave. In this tutorial, we ran both the server and
the agent on the same machine. But when you deploy an Agentspan agent, the
server can run on a totally different machine.
Check out the Agentspan docs in the description below to learn more about that.
I'm David. Thanks for watching and happy coding.
