Back to agents
Agent #2

šŸ› Bug Detection & Fixing Agent

This agent analyzes code and test output to identify bugs, understand root causes, and generate fixes. It can work with error logs, stack traces, and failing tests to pinpoint issues and implement solutions. python 02_bug_detection.py /path/to/file.py python 02_bug_detection.py "Error: TypeError at line 42"

Key Features

  • āœ“Root cause analysis
  • āœ“Error trace interpretation
  • āœ“Automatic fix generation
  • āœ“Test case creation
  • āœ“Prevention strategy

Implementation

Download .py
02_bug_detection.py
#!/usr/bin/env python3
"""
Bug Detection & Fixing Agent

This agent analyzes code and test output to identify bugs, understand
root causes, and generate fixes. It can work with error logs, stack traces,
and failing tests to pinpoint issues and implement solutions.

Usage:
    python 02_bug_detection.py /path/to/file.py
    python 02_bug_detection.py "Error: TypeError at line 42"
"""

import asyncio
import sys
from anthropic import Anthropic

def main():
    if len(sys.argv) < 2:
        print("Usage: python 02_bug_detection.py <file_path_or_error>")
        sys.exit(1)

    target = " ".join(sys.argv[1:])

    client = Anthropic()

    system_prompt = """You are an expert bug detective. Your role is to:
1. Analyze error messages, stack traces, and code to find bugs
2. Identify the root cause of failures
3. Provide step-by-step debugging explanation
4. Generate a fix with test cases
5. Explain how to prevent similar bugs

Be systematic: trace the execution flow, identify assumptions that failed,
and provide comprehensive solutions with prevention strategies."""

    task = f"""Analyze the following issue and provide a complete bug fix:

{target}

Please provide:
1. Root cause analysis
2. Step-by-step explanation of the bug
3. Complete fixed code
4. Test case to verify the fix
5. Prevention strategy"""

    print("šŸ› Starting Bug Detection Agent...\n")
    print("=" * 60)

    try:
        with client.messages.stream(
            model="claude-opus-4-7",
            max_tokens=2500,
            system=system_prompt,
            messages=[{"role": "user", "content": task}],
        ) as stream:
            for text in stream.text_stream:
                print(text, end="", flush=True)

        print("\n" + "=" * 60)
        print("\nāœ… Bug detection complete!")

    except Exception as e:
        print(f"\nāŒ Error: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()

How to Use

This agent is designed to be run from the command line:

python 02_bug_detection.py <input>

The agent will stream its response to the console as it works through the task. Use Ctrl+C to interrupt at any time.

Quick Download

02_bug_detection.py
File
02_bug_detection.py
Language
Python 3.8+
Dependencies
anthropic

Setup

  1. 1. Install: pip install anthropic
  2. 2. Download the .py file
  3. 3. Set ANTHROPIC_API_KEY
  4. 4. Run: python 02_bug_detection.py