By AI Engineering Team

Plan-and-Execute Agent - Debugging and Testing

Intermediate 4 min
Agentic AIPythonDebugging

Why Debugging Matters

Agents are complex. They make decisions, call tools, and loop. When something goes wrong, you need to understand:

  • What did the agent decide to do?
  • Which tools did it call?
  • What results did it get?
  • Why did it make that choice?

Good debugging tools make this easy.

Debugging Technique 1: Print Tool Calls

The simplest approach: print everything.

🐍 Python Agent with Logging
📟 Console Output
Run code to see output...

Output example:

============================================================
AGENT START: Create a weekly digest from ./notes
============================================================

============================================================
ITERATION 1
============================================================

📝 Agent says:
   I'll create a plan to generate your weekly digest...

🔧 Tool calls: 1
   1. list_files({'directory': './notes'})

   ↳ Result: {'files': ['2026-01-20.md', '2026-01-21.md', '2026-01-22.md'], 'count': 3}

============================================================
ITERATION 2
============================================================
...

Debugging Technique 2: JSONL Logging

Save the full conversation to a file for later analysis:

🐍 Python JSONL Conversation Logger
📟 Console Output
Run code to see output...

Why JSONL?

  • Each line is a complete JSON object
  • Easy to parse and analyze
  • Can stream large logs without loading everything into memory
  • Standard format for ML training data

Debugging Technique 3: Replay Runs

Load a logged conversation and replay it:

🐍 Python Replay Agent Runs
📟 Console Output
Run code to see output...

Debugging Technique 4: Conversation Snapshots

Save the full conversation state at each iteration:

🐍 Python Conversation Snapshots
📟 Console Output
Run code to see output...

Common Issues and Solutions


              **Symptom:** Agent keeps calling the same tool repeatedly

**Causes:**
- Tool returns an error, agent retries indefinitely
- Tool result doesn't contain expected information
- Agent doesn't understand when it's done

**Solutions:**
1. Check tool implementations - make sure they return useful results
2. Add better error messages in tool outputs
3. Lower MAX_ITERS to catch loops faster
4. Add explicit "done" conditions in instructions

**Debug:**
```python
# Add this to see if agent is looping
if iteration > 0 and message.tool_calls:
  prev_call = input_list[-2].get("tool_calls", [])
  if prev_call and prev_call[0].function.name == message.tool_calls[0].function.name:
      print("⚠️  WARNING: Agent calling same tool again")
```
            

Testing Your Agent

Create a test suite to validate agent behavior:

🐍 Python Agent Test Suite
📟 Console Output
Run code to see output...

Debugging Checklist

When your agent misbehaves, check:

  • Tool implementations - Do they return the right format?
  • Tool schemas - Are descriptions clear?
  • Instructions - Do they guide the agent properly?
  • MAX_ITERS - Is it too high or too low?
  • Budget - Is the agent hitting limits?
  • Logs - What does the conversation history show?
  • Dry-run - Does it work in simulation?

Key Takeaways

You now know how to:

  1. ✅ Print tool calls for quick debugging
  2. ✅ Log to JSONL for detailed analysis
  3. ✅ Replay runs to understand past behavior
  4. ✅ Save snapshots of conversation state
  5. ✅ Troubleshoot common agent issues
  6. ✅ Test agent components

What’s Next?

In the final page, we’ll wrap up with exercises and next steps: how to extend your agent, scale to production, and explore advanced patterns.

Discussion

Join the conversation and share your thoughts

Discussion

0 / 5000