🔥 0
0
Lesson 10 of 10 15 min +100 XP

Extended Thinking

What is Extended Thinking?

Extended thinking (also called "chain of thought" or "scratchpad") is a feature in Claude 3.5 Sonnet, Claude 3.7, and Claude 4 that allows the model to reason through problems step-by-step before providing an answer.

As Anthropic explains:

> "Claude 4 is a hybrid reasoning model, meaning that there's extended thinking at your disposal. You can use extended thinking as a crutch for your prompt engineering."

How It Works

When enabled, the model generates "thinking" content that shows its reasoning process:

<thinking>
Let me analyze this form step by step.

First, looking at Vehicle A's column:
- Row 1 (parked): Not checked
- Row 2 (starting to move): Not checked
- Row 8 (striking rear): CHECKED
...

Now Vehicle B's column:
- Row 1 (parked): CHECKED
- Row 2-17: Not checked

So Vehicle A was striking the rear of Vehicle B,
while Vehicle B was parked. This means...
</thinking>

Based on my analysis, Vehicle A is at fault because...

Enabling Extended Thinking

API Usage

const response = await anthropic.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 8000,
  thinking: {
    type: "enabled",
    budget_tokens: 5000  // Tokens allocated for thinking
  },
  messages: [...]
});

// Access thinking content
const thinking = response.content.find(c => c.type === "thinking");
const answer = response.content.find(c => c.type === "text");

Extended Thinking for Debugging

Understanding Model Reasoning

When a prompt fails, enable thinking to see why:

// Model gives wrong answer
// Enable thinking to debug

const response = await anthropic.messages.create({
  // ...
  thinking: { type: "enabled", budget_tokens: 3000 }
});

console.log("Model's reasoning:", response.content[0].text);
// Now you can see WHERE the reasoning went wrong

Example: Debugging Form Analysis

Problem: Model misidentifies which boxes are checked. With thinking enabled:
<thinking>
Looking at row 8 for Vehicle A...
The marking is faint, could be:
- A light X
- A stray mark
- Not a checkbox marking at all

I'll interpret this as checked because there appears
to be something in that box...
</thinking>
Insight: The model is uncertain about faint markings. Solution: Add guidance about handling unclear markings.

Using Thinking to Improve Prompts

Step 1: Run with Thinking

const response = await claude.complete({
  prompt: yourPrompt,
  thinking: { type: "enabled" }
});

Step 2: Analyze the Reasoning

Look for:

  • Where does reasoning match expectations?
  • Where does it diverge?
  • What assumptions does the model make?
  • What information seems to be missing?

Step 3: Update Your Prompt

Based on findings:

  • Add missing context
  • Clarify ambiguous instructions
  • Provide examples for tricky cases
  • Adjust step order if reasoning is out of sequence

Step 4: Test Again

Run without thinking to verify improvement, then retest with thinking to confirm reasoning is sound.

Thinking as a Development Tool

When to Use

PhaseExtended Thinking
DevelopmentEnabled - see reasoning
DebuggingEnabled - find issues
TestingBoth - verify behavior
ProductionUsually disabled - save tokens

Token Efficiency

Extended thinking uses tokens. For production:

// Development: See reasoning
const devResponse = await claude.complete({
  thinking: { type: "enabled", budget_tokens: 5000 }
});

// Production: Skip thinking
const prodResponse = await claude.complete({
  // No thinking parameter
});

Baking Reasoning into Prompts

Once you understand how the model should reason, encode it:

Before (Relying on Extended Thinking)

Analyze this form and determine fault.

After (Reasoning Baked In)

<instructions>
  <step>
    First, examine each checkbox row methodically.
    For each row, check both Vehicle A and B columns.
    Note the marking type (X, checkmark, circle).
    If a marking is unclear, note it as "uncertain".
  </step>

  <step>
    Based on checked boxes, determine what each vehicle
    was doing (using the row definitions provided).
  </step>

  <step>
    Examine the sketch to corroborate or clarify
    your understanding from the form.
  </step>

  <step>
    Determine fault based on traffic laws and evidence.
    Cite specific checkboxes and sketch observations.
  </step>
</instructions>

The prompt now guides the model's reasoning explicitly.

Extended Thinking Patterns

Pattern 1: Analysis Tasks

Let the model think through each component:

When analyzing this document, think through:
1. What facts are present?
2. What's missing or unclear?
3. What conclusions can be drawn?
4. What's the confidence level?

Pattern 2: Problem Solving

Break down this problem:
1. Identify the key constraints
2. Consider possible approaches
3. Evaluate trade-offs
4. Select the best solution

Pattern 3: Decision Making

When making this decision:
1. List all options
2. Evaluate each against criteria
3. Consider edge cases
4. Recommend with justification

Key Takeaways

  • Extended thinking shows reasoning - See how the model approaches problems
  • Use for debugging - Understand where prompts fail
  • Analyze then optimize - Study thinking, then bake reasoning into prompts
  • Development vs. production - Enable for development, disable for efficiency
  • Token budget matters - Allocate enough tokens for complex reasoning

Course Summary

Congratulations! You've completed Prompt Engineering 101. You now understand:

  • The fundamentals - What prompt engineering is and why it matters
  • Prompt structure - The 10-point framework for effective prompts
  • Context setting - Task and tone context for clear communication
  • System prompts - Background information and caching
  • XML tags - Structuring information for clarity
  • Few-shot learning - Teaching with examples
  • Step-by-step - Guiding model reasoning
  • Output formatting - Designing for parsing
  • Prefill - Starting responses for format control
  • Extended thinking - Debugging and optimization
Next steps:
  • Practice with real prompts
  • Build a prompt library for common tasks
  • Experiment with different models
  • Join the Anthropic developer community

Happy prompting!

Pre-fill Responses