Chain-of-Thought Prompting
Step-by-step reasoning and extended thinking
Skills taught: llms.cot
Chain-of-Thought (CoT) Prompting
Asking the model to reason step by step before giving an answer dramatically improves accuracy on complex tasks. The model uses the intermediate tokens as a scratchpad, working through sub-problems before committing to a final answer.
import anthropic
client = anthropic.Anthropic()
# Without CoT — often wrong on multi-step problems
direct = client.messages.create(
model="claude-haiku-5-5",
max_tokens=50,
temperature=0,
messages=[{"role": "user", "content":
"I have $240. I spend 1/4 on food, then half of what's left on rent. How much do I have?"
}]
)
print("Direct:", direct.content[0].text)
# With CoT — accurate
cot = client.messages.create(
model="claude-haiku-5-5",
max_tokens=200,
temperature=0,
messages=[{"role": "user", "content":
"""Solve step by step:
I have $240. I spend 1/4 on food, then half of what's left on rent. How much do I have?
Step-by-step reasoning:"""
}]
)
print("CoT:", cot.content[0].text)
# Step 1: 1/4 of $240 = $60 on food. Remaining: $240 - $60 = $180
# Step 2: Half of $180 = $90 on rent. Remaining: $180 - $90 = $90
# Answer: $90
Zero-Shot vs Few-Shot CoT
Zero-Shot CoT vs Few-Shot CoT
- Zero-Shot CoT: Simply append 'Let's think step by step.' to any prompt — no examples needed.
- Few-Shot CoT: Provide 2-3 complete reasoning chain examples before the actual problem.
few_shot_prompt = """Solve math word problems. Show your reasoning step by step.
Problem: A store has 48 apples. They sell 1/3 and then receive a delivery of 20 more. How many apples?
Reasoning:
1. Sold: 48 × (1/3) = 16 apples
2. Remaining: 48 - 16 = 32 apples
3. After delivery: 32 + 20 = 52 apples
Answer: 52
Problem: A train travels 60 mph for 2.5 hours, then 80 mph for 1 hour. What total distance?
Reasoning:
1. First leg: 60 × 2.5 = 150 miles
2. Second leg: 80 × 1 = 80 miles
3. Total: 150 + 80 = 230 miles
Answer: 230
Problem: {problem}
Reasoning:"""
import anthropic
client = anthropic.Anthropic()
problem = "Sarah earns $15/hr and works 8h on Monday and 6h on Tuesday. She spends 40% of her earnings. How much does she keep?"
response = client.messages.create(
model="claude-haiku-5-5",
max_tokens=300,
temperature=0,
messages=[{"role": "user", "content": few_shot_prompt.format(problem=problem)}]
)
print(response.content[0].text)
Extended Thinking (Claude)
Claude supports an Extended Thinking API that provides a dedicated reasoning budget. The model reasons privately in a hidden scratchpad, then returns a polished answer. This is similar to OpenAI's o1/o3 models.
import anthropic
client = anthropic.Anthropic()
# Extended thinking for hard problems (uses more tokens but much more accurate)
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=16000,
thinking={
"type": "enabled",
"budget_tokens": 10000 # tokens allocated for private reasoning
},
messages=[{"role": "user", "content":
"""A cryptarithmetic puzzle:
SEND + MORE = MONEY
Each letter represents a unique digit 0-9. Solve for all digits."""
}]
)
for block in response.content:
if block.type == "thinking":
print(f"Thinking ({len(block.thinking)} chars)...")
elif block.type == "text":
print("Answer:", block.text)
Tip: For Claude, enable Extended Thinking with thinking: {type: 'enabled', budget_tokens: 5000} for the hardest reasoning tasks. For cheaper tasks, zero-shot CoT ('Let's think step by step') is free.
Warning: CoT increases token usage and latency. For simple tasks (factual lookup, yes/no questions, single-label classification), skip CoT entirely — it wastes tokens without improving accuracy.
Frequently Asked Questions
Does CoT always help?
No. For simple factual retrieval, CoT wastes tokens. Use it for multi-step reasoning, math, and planning tasks. Research shows CoT helps most on problems that require >3 reasoning steps.
What is self-consistency?
A technique that generates multiple CoT reasoning chains (e.g. 5 runs) and votes on the final answer. It improves accuracy further at the cost of 5x API calls. Use when accuracy is critical.
What is Claude's Extended Thinking?
An API feature that gives Claude a private token budget to reason before answering. The thinking tokens are not shown in the response (or shown as a separate block). Similar to OpenAI's o1/o3 reasoning models.