← LearnPrompting · Reasoning

Claude Reasoning Prompts for Code Debugging and Self-Correction

To use Claude reasoning prompts for code debugging, add 'think step by step before answering' to your prompt or ask Claude to trace execution inside <thinking> tags. This activates chain-of-thought reasoning, so Claude identifies where output diverges from expected behavior and explains the fix rather than guessing at a patch.

To use Claude reasoning prompts for code debugging and self-correction, include a phrase like 'think step by step before answering' in your prompt, or ask Claude to place its reasoning inside <thinking> tags before giving a final answer. With chain-of-thought (CoT) prompting active, Claude traces execution line by line, identifies where output diverges from expected behavior, and explains the fix — rather than guessing at a patch. This works on every plan, including Free, and requires no special API parameters.

What Is Chain-of-Thought Prompting and Why Does It Help With Debugging?

Chain-of-thought (CoT) prompting is a technique that instructs Claude to break a complex problem into a sequence of intermediate reasoning steps before producing a final answer. Rather than jumping directly to a conclusion, Claude is guided to "show its work," which reduces logical leaps and unsupported analytical claims.

For code debugging specifically, this matters because bugs are almost always caused by a gap between what a developer expects the code to do and what it actually does. When Claude reasons step by step, it surfaces that gap explicitly — making the diagnosis auditable and the fix trustworthy. Without CoT, Claude may produce a plausible-looking patch that addresses a symptom rather than the root cause.

According to Anthropic's chain-of-thought prompting documentation, structured deliberation produces more accurate, nuanced, and verifiable outputs than immediate text generation — a property that is especially valuable when the cost of a wrong answer is a broken production system.

How Do You Write a Claude Reasoning Prompt for Code Debugging?

There are two practical approaches, and you can combine them.

Approach 1: Natural-Language Instruction

Add a reasoning instruction directly to your prompt. This works in the Claude.ai chat interface and via the API with no parameter changes:

Here is a Python function that is supposed to return the second-largest number in a list, but it returns the wrong result for duplicate values. Think step by step before giving your final answer.

def second_largest(nums):
    nums.sort(reverse=True)
    return nums[1]

Claude will trace the execution, identify that nums[1] returns the second element of the sorted list (which may be a duplicate of the largest), and explain a corrected approach — all before stating the fix.

Approach 2: Structured XML Tags

Ask Claude to separate its reasoning from its final answer using XML tags. This lets you inspect the diagnosis independently from the corrected code:

You are a senior software engineer. Analyze the bug in the function below.
In <thinking> tags, trace the execution step by step and identify the root cause.
In <answer> tags, provide the corrected function with a brief explanation.

def second_largest(nums):
    nums.sort(reverse=True)
    return nums[1]

Claude's response will look roughly like this:

<thinking>
Step 1: The function sorts the list in descending order.
Step 2: It returns index 1 — the second element after sorting.
Step 3: If the list is [5, 5, 3], sorted descending is [5, 5, 3].
         Index 1 is 5, not 3. The function returns a duplicate of the largest, not the true second-largest.
Root cause: The function does not deduplicate before indexing.
</thinking>

<answer>
def second_largest(nums):
    unique = sorted(set(nums), reverse=True)
    return unique[1]

# Explanation: Converting to a set removes duplicates before sorting,
# so index 1 is always the true second-largest distinct value.
</answer>

Separating reasoning from output with XML tags lets you catch a flawed diagnosis before you even read the proposed fix — a significant advantage when debugging security-sensitive or business-critical code.

How Do You Enable Extended or Adaptive Thinking for Deeper Debugging?

For complex bugs — race conditions, subtle type coercions, multi-file dependency issues — you may want Claude to allocate dedicated compute to reasoning rather than relying on a prompt instruction alone. Anthropic's Extended Thinking feature provides this at the API level.

The approach depends on which model you are using:

  • In the Claude.ai UI: Select a model that supports extended thinking (such as Claude Sonnet 4.6), enable the "Extended thinking" toggle, then send your debugging prompt as normal.
  • Via the API on newer models (Opus 4.6+, Sonnet 4.6+, and later): Use Adaptive Thinking, where Claude dynamically decides how much to reason based on query complexity and a developer-chosen effort level of low, medium, or high. Do not set a manual token budget on these models — doing so causes an API error.
  • Via the API on Claude 3.7 Sonnet: Use the budget_tokens approach to set a fixed thinking token ceiling. The budget must always be less than max_tokens.

When Adaptive Thinking is active, remove explicit "think step by step" instructions from your prompt. The model's native thinking capability is designed to work without them, and redundant instructions can interfere with the reasoning process.

Step-by-Step: Debugging a Function With Claude CoT Prompting

  1. Paste the broken function into your prompt along with a description of the incorrect behavior and a concrete failing test case.
  2. Add a reasoning instruction — either "Think step by step before answering" or ask Claude to use <thinking> and <answer> tags.
  3. Send the prompt. Claude will trace execution, identify the divergence point, and propose a fix.
  4. Review the thinking block first. Confirm that Claude's diagnosis matches your understanding of the bug before reading the corrected code.
  5. Test the fix against your failing case and any edge cases Claude surfaced during reasoning.
  6. Iterate if needed. If the fix is still wrong, paste the new output back with the same CoT instruction. Claude will re-examine its own reasoning — this is the self-correction loop.

When Should You Use CoT Prompting vs. Extended Thinking for Debugging?

Scenario Best Approach Why
Simple logic bug in a single function Prompt-based CoT (think step by step) Fast, works on all plans, no API changes needed
Subtle bug requiring deep trace (e.g., async race condition) Extended or Adaptive Thinking via API Dedicated compute for reasoning; structured thinking blocks in response
Multi-file or multi-service debugging in an agentic loop Adaptive Thinking with tool use Claude reasons between tool calls, adjusting its plan as new information arrives
Simple factual lookup (e.g., what does a built-in function return?) Standard prompting, no CoT CoT adds latency and token cost with no accuracy benefit for trivial queries

What Are the Most Common Mistakes When Using Claude Reasoning Prompts for Debugging?

Forgetting to ask for reasoning at all

Without an explicit instruction — either a natural-language phrase or the API thinking parameter — CoT does not activate. Claude will produce an answer, but it may skip steps and miss the root cause. Always include a reasoning instruction for any non-trivial bug.

Adding "think step by step" when Extended Thinking is already enabled

When the API thinking parameter is active, remove explicit CoT instructions from your prompt. Redundant instructions can interfere with the reasoning process. Observe Claude's natural thinking output first, then adjust prompts based on what you see.

Stripping thinking blocks in agentic debugging loops

If you are running Claude in a multi-turn loop — for example, having it read a file, run a test, interpret the output, and propose a fix — you must include the model's previous thinking blocks unchanged when passing tool results back. Removing them breaks reasoning continuity and degrades output quality on subsequent turns. See Anthropic's guidance on thinking with tool use for details.

Using CoT for every query regardless of complexity

CoT increases output length and, with API thinking enabled, token usage. Reserve it for high-stakes or complex debugging tasks. For simple factual queries, skip CoT entirely to keep latency and costs low.

Is Claude CoT Prompting for Debugging Worth It?

Yes — for any bug where the cause is not immediately obvious. The core value is auditability: when Claude shows its reasoning, you can verify the diagnosis before trusting the fix. This is especially important in production codebases where a wrong patch can introduce a new bug or a security vulnerability.

For straightforward issues — a typo, a missing import, an off-by-one in a simple loop — standard prompting is faster and cheaper. But for anything involving state, concurrency, type coercion, or cross-module interactions, chain-of-thought reasoning consistently produces more reliable diagnoses than immediate answers.

The technique is available on every Claude plan (Free, Pro, Max, Team, Enterprise) and via the API, so there is no barrier to trying it on your next debugging session.

Frequently asked questions

What is the simplest Claude reasoning prompt for code debugging?

Add 'Think step by step before giving your final answer' to the end of your prompt. This works in the Claude.ai chat interface and via the API on any plan, with no settings changes required.

How do I make Claude show its reasoning separately from the fixed code?

Ask Claude to place its diagnosis inside <thinking> tags and the corrected code inside <answer> tags. This lets you review the reasoning independently before reading the proposed fix.

Does chain-of-thought prompting work on the free Claude plan?

Yes. Prompt-based CoT — using phrases like 'think step by step' or XML reasoning tags — works on all plans including Free. Extended Thinking via the API requires API access.

When should I use Adaptive Thinking instead of a prompt instruction for debugging?

Use Adaptive Thinking via the API when you need structured thinking blocks in the response, dynamic compute allocation based on bug complexity, or interleaved reasoning with tool calls in an agentic debugging loop. It requires a supported model such as claude-sonnet-4-6 or claude-opus-4-6.

Can Claude correct its own mistakes when debugging?

Yes. If Claude's first fix is still wrong, paste the new output back with the same CoT instruction. Claude will re-examine its reasoning against the new evidence — this iterative self-correction loop is one of the main practical benefits of chain-of-thought prompting.

Should I include 'think step by step' when Extended Thinking is enabled via the API?

No. When the API thinking parameter is active, remove explicit CoT instructions from your prompt. Redundant instructions can interfere with the model's native reasoning process.

Go deeper

Chain-of-thought is one of 85 features in Claude Master — the independent, continuously updated manual with worked examples, the pitfalls, and the workflows that put Claude to work.

Get Claude Master — founding price →

Independent product. Not affiliated with or endorsed by Anthropic. "Claude" is a trademark of Anthropic, used here only to describe the subject of this guide.