Build a Crash-Resilient Python AI Agent Step-by-Step (Full Transcript)

Learn AgentSpan basics: tools, runtimes, server dashboard, parallel tool calls, and resuming executions with idempotent tools after crashes.
Download Transcript (DOCX)
Speakers
add Add new speaker

[00:00:00] Speaker 1: 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 Mac OS. 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 resources 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 init and then Enter to create your project. Then type uv add agentspan to add agentspan and all of its dependencies to your project. To make sure everything installed correctly, run this command with me, uv run agentspan 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 olama 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 getWeather, 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 doc string 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 doc string 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'd 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 gonna 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 agent span 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 agent span server. So let's open a terminal window and this time we'll type in uvrun agent span 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 uvrun 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 agent span server infrastructure. If we scroll down, you'll see the weather bot completed and we got the agent output. We see it called the tool and output the results directly. Now a cool thing about agent span 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 weather bot. This is the agent span 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 weather bot 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 agent span 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 gotta 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 test bot 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 a 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 uvrun to run the agent. It takes about 15 seconds. You should see agent test bot 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 agent span 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 test 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. It 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 finished. 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, 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 item potent. That describes a function that you can run multiple times without changing the end result. And this version of SafeTest 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 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. Now, in this tutorial, we ran both the server and the agent on the same machine. But when you really deploy this, they can run on totally separate machines. You can install AgentSpan through pip, NPM, or your package manager of choice. I'm David, thanks for watching, and happy coding.

ai AI Insights
Arow Summary
The transcript is a step-by-step tutorial on building a first AI agent in Python using the open-source AgentSpan framework. It covers prerequisites (Python 3.10+, JDK, uv), setting up a project in VS Code, installing AgentSpan, configuring AI providers (Ollama, OpenAI, Anthropic) and fixing certificate issues. The tutorial then builds a simple weather agent with a tool function, demonstrates running it via the AgentSpan server, and explores the AgentSpan dashboard for execution traces and tool call inspection. Next, it builds a more complex “test bot” that generates unit tests for a price calculator module by calling a file-writing tool once per function in parallel. Finally, it demonstrates crash resilience by starting an execution to obtain an execution ID, killing the client mid-run, and resuming the same server-side execution; it highlights the need for idempotent tools to avoid duplicate outputs. The key value proposition is that agent state lives on the AgentSpan server, enabling recovery from client crashes and even moving to another machine.
Arow Title
Build a Crash-Resilient Python AI Agent with AgentSpan
Arow Keywords
AgentSpan Remove
Python Remove
AI agent Remove
tools Remove
system prompt Remove
uv Remove
VS Code Remove
JDK Remove
OpenAI API key Remove
Anthropic Claude Remove
Ollama Remove
AgentSpan server Remove
agent runtime Remove
dashboard Remove
execution ID Remove
resume execution Remove
crash resilience Remove
idempotent tools Remove
unit tests Remove
parallel tool calls Remove
Arow Key Takeaways
  • Install prerequisites: Python 3.10+, a JDK (LTS 25 or 21), and the uv package manager.
  • Initialize a project with uv and install AgentSpan; verify setup with `agentspan doctor`.
  • Configure an AI provider (Ollama/OpenAI/Anthropic) via environment variables; address common Mac certificate errors if needed.
  • Define tools as well-documented, type-hinted Python functions; names, docstrings, and return types help the agent choose and use tools correctly.
  • Create an agent with a name, model identifier (provider/model), and tool list; run it through an AgentSpan server-connected runtime.
  • Use the AgentSpan dashboard to inspect executions, nodes, tool inputs/outputs, and parallelism for debugging.
  • For long, multi-step work, start runs to obtain an execution ID and resume the same execution after client crashes.
  • Design tools to be idempotent so retries/resumes don’t create duplicate side effects (e.g., duplicate test code).
Arow Sentiments
Positive: The tone is instructional and encouraging, emphasizing ease of setup, practical demos, and the benefits of resilience and debugging via the dashboard.
Arow Enter your query
{{ secondsToHumanTime(time) }}
Back
Forward
{{ Math.round(speed * 100) / 100 }}x
{{ secondsToHumanTime(duration) }}
close
New speaker
Add speaker
close
Edit speaker
Save changes
close
Share Transcript