Code debugging & decision analysis

Reasoning is perfect for debugging. The AI can trace through buggy code step-by-step: understand the intent, trace execution with sample inputs, find where the logic breaks, and explain the fix.

Debug with reasoning

Systematic debugging with reasoning models

They complement each other. Running code catches runtime errors, but AI reasoning can catch logic bugs that produce wrong results without errors. The best approach is both: run the code to see what happens, then use AI reasoning to understand why and find the root cause.

06-reasoning-models.ipynb
python
buggy_code = """
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-3)  # Bug!
"""

debug_prompt = f"""Find and explain the bug:
{buggy_code}

1. What is the code trying to do?
2. Trace through fibonacci(5)
3. Where is the bug?
4. Provide the corrected version."""

solution = solve_with_reasoning(debug_prompt, temperature=0.3)
# Step 1: This implements Fibonacci sequence
# Step 2: Tracing fibonacci(5):
#   fibonacci(5) = fibonacci(4) + fibonacci(2)  ← should be n-2!
#   fibonacci(n-3) is WRONG
# Step 3: Bug is on the return line: n-3 should be n-2
# Fix: return fibonacci(n-1) + fibonacci(n-2)

The AI traces execution step by step and catches that n-3 should be n-2. Without reasoning, it might just guess at the fix.

Beyond finding known bugs, reasoning is also powerful for code review. The AI can systematically check for edge cases, missing error handling, and potential issues a human reviewer might miss.

06-reasoning-models.ipynb
python
code_to_review = """
def divide_list(numbers, divisor):
    results = []
    for num in numbers:
        results.append(num / divisor)
    return results
"""

review_prompt = f"""Review this code for potential issues:
{code_to_review}

Check for: bugs, edge cases, error handling, performance."""

review = solve_with_reasoning(review_prompt, temperature=0.3)
# Issues found:
# 1. ZeroDivisionError if divisor is 0
# 2. TypeError if numbers contains non-numeric values
# 3. No input validation
# Suggestion: Add try/except and input checking

Code review with reasoning: the AI systematically checks for bugs, edge cases, and missing error handling.

Reasoning also shines for decision analysis. When weighing multiple factors with tradeoffs, step-by-step thinking helps the AI provide balanced, structured advice.

06-reasoning-models.ipynb
python
decision = """
I'm deciding between two job offers:

Job A: $120K salary, San Francisco (high COL), startup (20 people), 0.5% equity
Job B: $100K salary, Austin (moderate COL), established company (500 people), no equity
"""

analysis = solve_with_reasoning(
    f"{decision}\nAnalyze this decision with pros/cons for each.",
    temperature=0.5  # Slightly higher for nuanced analysis
)
# Systematic comparison: salary, cost of living,
# risk/reward, growth potential, stability, equity value

Decision analysis uses medium temperature (0.5) for balanced reasoning. The AI weighs multiple factors systematically.

Absolutely! That is one of the most powerful combinations. You can use Chain-of-Thought prompting within a function-calling agent so the AI reasons about which tool to call and why. For example, a debugging agent could reason through the code, decide it needs to run a test, call a tool, then reason about the results.

Flashcards: Flashcards

Loading practice…

Checkpoint: Reasoning concepts

Loading practice…

You have seen how reasoning transforms debugging, code review, and complex decisions. Next up is the course finale where you will bring all 6 pillars together in one final assessment.