Text of realpython.com › ai-debugging as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

How to Debug Python Code With an AI Agent – Real Python

How to Debug Python Code With an AI Agent by Amanda Adoyi Updated Aug 17, 2026 Reading time estimate 18m intermediate ai tools Table of Contents Remove ads AI debugging is fixing broken code by pairing with an AI coding agent instead of hunting down bugs on your own. In this tutorial, you’ll debug Python code with an AI agent by reproducing a bug with a failing test, giving your agent the context it needs, and verifying the fix that gets your program running again.

Most developers seek what’s called a flow state when building programs or fixing code. This is a state of high focus. However, it can occasionally be interrupted by bugs. While debugging has for decades been largely carried out by humans aided by tools like pdb , AI coding agents have now become instrumental to the process, helping to make it faster. The Python project that you’ll debug in this guide places you in the position of an alien pet owner using a tool to keep your pet happy.

This tool has checks for when your pet is well fed and well rested. However, you’ve learned that your pet is never happy no matter how often it eats or rests. You suspect a bug, and your AI agent has come to the rescue. By applying the steps in this tutorial, you’ll get this broken pet project back into a working state: The Fixed Pet Program Running Successfully You’ll use the newly released Antigravity CLI by Google to see this workflow play out in real time.

But if that’s not your preferred coding agent, that’s fine too, and you can still follow along with the steps. Get Your Code: Click here to download the free sample code you’ll use to debug the broken alien pet project with an AI coding agent. Take the Quiz: Test your knowledge with our interactive “How to Debug Python Code With an AI Agent” quiz. You’ll receive a score upon completion to help you track your learning progress: Interactive Quiz How to Debug Python Code With an AI Agent Test your understanding of debugging Python code with an AI agent, from reproducing a bug with a failing test to verifying the fix.

Prerequisites To work through this tutorial comfortably, you should have the following at your fingertips: Git commands: You should be comfortable setting up Git in your project’s root folder and working with a few basic commands. Python 3.11 or newer: You’ll need Python to run the sample project. A working AI coding agent : You’ll be using one to debug, so being familiar with an agent that can read and write files is important.

This tutorial uses Antigravity CLI , and Step 1 walks through the setup. Python testing with pytest : You should be comfortable reading and writing tests with pytest . You’ll write a failing test to reproduce the bug, then rerun it to confirm your agent’s fix works. With these prerequisites in place, you’re ready to set up the alien pet project and start tracking down the bug that’s keeping your pet unhappy. Remove ads Step 1: Set Up Your Workspace for AI Debugging If you already have a preferred agent set up, or you’ve already installed Antigravity CLI, skip the setup below and scroll to the directory tree to add the required files to your workspace.

To set up Antigravity CLI for this tutorial, you’ll need to have a Google account. If you’re familiar with the just-retired Gemini CLI , you’ll find that the setup processes are quite similar. First, go to the Antigravity CLI installation guide for instructions on installing it. Choose the appropriate installation command for your operating system and run it in your terminal: Windows Linux + macOS Language: Windows PowerShell PS> irm https :// antigravity . google / cli / install . ps1 | iex Language: Shell $ curl -fsSL https://antigravity.google/cli/install.sh | bash Once that’s done, your new AI coding agent is successfully installed.

Next, create your project folder, initialize a virtual environment to safely isolate your dependencies, and activate it: Language: Shell $ mkdir alien-pet-care $ cd alien-pet-care $ python -m venv venv $ source venv/bin/activate Activating the virtual environment ensures that any packages you install moving forward remain self-contained within this project directory. Depending on your operating system, your terminal prompt should now show the (venv) prefix.

Note: If you’re a Windows user running PowerShell, activate your environment using venv\Scripts\activate . If your system blocks the script, you can temporarily permit execution for your current session by running Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process . Now, launch the CLI configuration inside your project directory by typing the following command: Language: Shell (venv) $ agy This triggers a first-launch setup, where you’ll be prompted to log in with your Google account details.

You’ll be redirected to a page or instructed to paste an authorization code in an input field: The Antigravity CLI Authorization Code Prompt After you input the authorization code as shown above, you’ll be logged in. You can also select your color scheme and other options. Once you finish these steps, a prompt box appears, and you’ll be free to ask Antigravity anything. Well, almost anything—for this tutorial, you’re a distraught alien pet owner with a tricky bug that could use some debugging assistance.

To learn about the commands available in Antigravity CLI, have a look at the CLI reference . Back in your terminal, a screen like the one below should appear: The Antigravity CLI Prompt, Ready for Your Request Your screen looks great with the blinking cursor and your agent patiently awaiting your request. For the agent to debug, you need to give it some files to work with. Add the files shown below to your workspace. The directory tree below will make the file structure clear: alien-pet-care/ │ ├── alien_pet_care/ │ ├── __init__.py │ ├── __main__.py │ └── pet.py │ └── tests/ ├── __init__.py └── test_pet.py With your workspace structured like this, you can now install pytest so you can run tests: Language: Shell (venv) $ python -m pip install -U pytest The bug is hidden within the files.

Your agent shouldn’t have access to any extra information, so you can watch the gears turn in real time. Open pet.py and populate it with the code shown below: Language: Python Filename: alien_pet_care/pet.py class AlienPet : def __init__ ( self , name ): self . name = name self . fed_level = 0 self . rested_level = 0 self . MAX_STATUS = 10 def feed ( self ): """Increments the feeding level.""" if self . fed_level <= self .

MAX_STATUS : self . fed_level += 1 def rest ( self ): """Increments the rested level.""" if self . rested_level <= self . MAX_STATUS : self . rested_level += 1 def is_happy ( self ) -> bool : """The pet is genuinely happy only when fully fed and rested.""" return ( self . fed_level == self . MAX_STATUS and self . rested_level == self . MAX_STATUS ) def get_status ( self ) -> str : if self . is_happy (): return "🛸 Happy and thriving!" if self . fed_level < 5 : return "👾 Starving!

Please feed me." return "Doing okay, but still feels a bit empty..." The file pet.py contains the core logic for managing your pet’s vitals. It handles initialization, tracks individual stat levels, and determines whether the pet has reached full happiness. To see this behavior play out on your terminal, you need a way to run the simulation and print the final results. Open __main__.py and add the following CLI runner script to see the bug in action: Language: Python Filename: alien_pet_care/__main__.py import time from alien_pet_care.pet import AlienPet def main (): print ( "--- Welcome to the Alien Pet Incubator ---" ) pet = AlienPet ( name = "Zorg" ) print ( f "Initializing care routine for { pet . name } ..." ) time . sleep ( 0.5 ) for i in range ( 12 ): pet . feed () pet . rest () beam = "=" * ( i + 1 ) print ( f " \r 🛸 { beam } > 👾 Feeding & Resting... (Cycle { i + 1 } /12

The rest of this page is on the live site.