0Pricing
AI Agents · Lesson

Step-Through Debugging Techniques

Adding breakpoints, intermediate prints, and using debugger in agent code.

Debugging Agents Is Different from Debugging Functions

A function has a clear input and output. An agent has a loop with LLM calls, tool executions, and history mutations — all of which can go wrong in subtle ways.

Step-through debugging lets you pause at each step, inspect the agent's state, and understand exactly what went wrong.

Python's Built-In Debugger: pdb

The Python debugger pdb lets you pause execution, inspect variables, and step through code line by line. Insert import pdb; pdb.set_trace() at any point in the agent loop to drop into an interactive debug session.

import pdb

def run_agent_loop(query: str):
    history = []
    for step in range(1, 21):
        action = decide_action(query, history)

        # Drop into debugger at step 3 to inspect state
        if step == 3:
            import pdb; pdb.set_trace()
            # At this point you can:
            # (Pdb) print(action)       -- inspect current action
            # (Pdb) print(history)      -- inspect full history
            # (Pdb) n                   -- next line
            # (Pdb) c                   -- continue execution
            # (Pdb) q                   -- quit

        result = execute_tool(action['tool'], action['args'])
        history.append({'tool': action['tool'], 'result': result})

All lessons in this course

  1. Common Agent Loop Failures
  2. Trace Logging for Agent Steps
  3. Detecting and Breaking Infinite Loops
  4. Step-Through Debugging Techniques
← Back to AI Agents