A robot points and says Bug Found beside panels labeled AI Agent, Debug, and Verify, a code screen, a meter of happy-to-sad faces, a Python logo chip, and a green bug creature in a glass case.

How to Debug Python Code With an AI Agent

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:

Terminal output showing successful execution of pet program after debugging session.
Terminal Output Showing Successful Execution of the Pet Program After Debugging

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.

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.

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:

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.

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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Amanda Adoyi

Amanda is a Software Developer, writer and all-round nerd who loves to tinker with code and build projects in her free time. She also goes by Lonercode.

» More about Amanda

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: intermediate ai tools