Lesson 19 of 30 ~15 min
Course progress
0%

Debugging with Claude

Efektivní debugging workflow - jak kombinovat Claude s tradičními debugging nástroji.

Debugging je kde Opus 4.5 skutečně exceluje. Naučte se ho efektivně kombinovat s vašimi nástroji.

Debugging workflow

Fáze 1: Shromáždění kontextu

# Připravte kontext pro Claude
cat << 'EOF' > debug-context.md
## Error
$(cat error.log | tail -50)

## Stack Trace
$(cat stacktrace.txt)

## Relevant Code
$(cat src/problematic-file.ts)

## Recent Changes
$(git log --oneline -10)
$(git diff HEAD~5 src/problematic-file.ts)
EOF

Fáze 2: Claude analýza

from anthropic import Anthropic

client = Anthropic()

with open("debug-context.md") as f:
    context = f.read()

response = client.messages.create(
    model="claude-opus-4-5-20250101",
    max_tokens=4000,
    thinking={"type": "enabled", "budget_tokens": 20000},
    messages=[{
        "role": "user",
        "content": f"""Debug this issue:

{context}

1. What is the root cause?
2. Why is it happening?
3. How to fix it?
4. How to prevent similar issues?"""
    }]
)

Fáze 3: Validace hypotézy

Nikdy slepě nevěřte Claude diagnóze. Vždy validujte:

# Claude říká: "Bug je na řádku 45, proměnná je null"

# Validace:
# 1. Přidejte logging
print(f"DEBUG: variable = {variable}, type = {type(variable)}")

# 2. Použijte debugger
import pdb; pdb.set_trace()

# 3. Napište test
def test_hypothesis():
    result = function_under_test(edge_case_input)
    assert result is not None  # Should fail if Claude is right

Kombinace s tradičními nástroji

Chrome DevTools + Claude

// 1. Zachyťte problém v DevTools
// Console: copy(JSON.stringify(errorObject))

// 2. Pošlete do Claude
const prompt = `
Debug this JavaScript error:
${JSON.stringify(errorObject, null, 2)}

Browser: Chrome 120
Page: /checkout
User action: Click "Submit Order"
`;

Python pdb + Claude

# Při debugování s pdb, exportujte stav
import json

def debug_snapshot():
    """Call from pdb: debug_snapshot()"""
    import inspect
    frame = inspect.currentframe().f_back
    
    snapshot = {
        "locals": {k: repr(v) for k, v in frame.f_locals.items()},
        "file": frame.f_code.co_filename,
        "line": frame.f_lineno,
        "function": frame.f_code.co_name
    }
    
    with open("/tmp/debug-snapshot.json", "w") as f:
        json.dump(snapshot, f, indent=2)
    
    print("Snapshot saved to /tmp/debug-snapshot.json")

VS Code Debugger + Claude

// .vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug with Context Export",
      "type": "node",
      "request": "launch",
      "program": "${file}",
      "postDebugTask": "Export Debug Context"
    }
  ]
}

Efektivní debugging prompty

Template: Crash Analysis

## Crash Analysis Request

**Error Message:**
[paste exact error]

**Stack Trace:**
[paste stack trace]

**Code Context:**
[relevant functions/classes]

**Steps to Reproduce:**
1. [step]
2. [step]

**What I've Already Tried:**
- [attempt 1]
- [attempt 2]

**Environment:**
- OS: 
- Runtime version:
- Dependencies:

Template: Performance Issue

## Performance Analysis Request

**Symptom:**
[what's slow, how slow]

**Profiler Output:**
[paste profiler data]

**Hot Path Code:**
[code that shows up in profiler]

**Expected vs Actual:**
- Expected: X ms
- Actual: Y ms

**Scale:**
- Data size: N records
- Concurrent users: M

Iterativní debugging

class DebuggingSession:
    def __init__(self):
        self.client = Anthropic()
        self.history = []
    
    def analyze(self, new_info):
        self.history.append({
            "role": "user",
            "content": new_info
        })
        
        response = self.client.messages.create(
            model="claude-opus-4-5-20250101",
            max_tokens=2000,
            system="""You are debugging a software issue.
Build on previous analysis. Ask for specific information if needed.""",
            messages=self.history
        )
        
        assistant_msg = response.content[0].text
        self.history.append({
            "role": "assistant",
            "content": assistant_msg
        })
        
        return assistant_msg

# Použití
session = DebuggingSession()

print(session.analyze("Error: NullPointerException at line 45"))
# Claude: "Can you show me line 45 and surrounding context?"

print(session.analyze("[code snippet]"))
# Claude: "The variable 'user' could be null. Can you show initialization?"

print(session.analyze("[initialization code]"))
# Claude: "Found it! The issue is..."

Efektivní debugging kombinuje sílu Claude s vašimi tradičními nástroji.