Extended Thinking
Table of contents
- Setup
- Basic example
- Streaming with extended thinking
- Token counting and context window management
- Understanding redacted thinking
- Handling error cases
This notebook demonstrates how to use Claude's extended thinking feature on Claude Sonnet 5.5 with various examples and edge cases.
Extended thinking gives Claude enhanced reasoning capabilities for complex tasks, while also providing transparency into its step-by-step thought process before it delivers its final answer. When extended thinking is turned on, Claude creates thinking content blocks where it outputs its internal reasoning. Claude incorporates insights from this reasoning before crafting a final response.
On Claude Sonnet 5.5, thinking is adaptive: you pass thinking={"type": "adaptive"} (this is also the default when thinking is omitted) and the model decides how much to think for each request. You steer how hard it thinks with output_config={"effort": ...} ("low", "medium", "high", "xhigh", or "max"; the default is "high") rather than with a token budget. The older {"type": "enabled", "budget_tokens": N} form is not accepted on this model. For more information on extended thinking, see our documentation(opens in new tab).
Setup
First, let's install the necessary packages and set up our environment.
Basic example
Let's start with a basic example to show extended thinking in action.
One detail to note before we start: on Claude Sonnet 5.5 the display setting for thinking defaults to "omitted", which returns thinking blocks with empty text (you still get the signature, and you are still billed for the thinking tokens). Because this notebook prints the reasoning, every request sets "display": "summarized" so the API returns a summary of Claude's thinking.
Adaptive also means Claude can decide a prompt needs no thinking at all, in which case the response simply has no thinking block. That is most likely on easy prompts and at lower effort. The examples below use a small constraint-satisfaction puzzle and "effort": "xhigh" so you reliably see thinking; the token-counting section later shows how the amount of thinking changes as you move effort from "low" to "xhigh".
==== FULL RESPONSE ==== 🧠 THINKING BLOCK: I'm working through the constraints: D must come before A, which comes before C, E must sit adjacent to C and be in positions 2-4, and B can't be next to D. I'm now systematically trying placements of D to narrow down valid orderings. Checking the other two candidates confirms all constraints hold: D before A, A before C, C adjacent E, and no B next to D. So the three valid orderings are DABEC, DAECB, and DACEB. [Signature available: True] [Signature (first 50 chars): CAQS8RIKEAgSGAI4AUIIdGhpbmtpbmcSDC9PApxA+kqk+axotR...] ✓ FINAL ANSWER: **Valid orderings (slots 1→5):** 1. D A B E C 2. D A E C B 3. D A C E B **Why there are no others** 1. Combining "D before A" and "A before C" gives the chain D < A < C. 2. E must be next to C, so E is either immediately before C (block "EC") or immediately after C (block "CE"). A cannot be E, so in either case A comes before the whole block. That leaves D, A, [block] in that fixed order, with B free to go in any of 4 places: before D, between D and A, between A and the block, or after the block. **Case 1: block EC.** The four placements of B give: - B D A E C: B is next to D, so this is invalid. - D B A E C: B is next to D, so this is invalid. - D A B E C: valid. E is in slot 4, and B (slot 3) is not next to D (slot 1). - D A E C B: valid. E is in slot 3, and B (slot 5) is not next to D (slot 1). **Case 2: block CE.** The four placements of B give: - B D A C E: E is last, so this is invalid. - D B A C E: E is last, so this is invalid. - D A B C E: E is last, so this is invalid. - D A C E B: valid. E is in slot 4, and B (slot 5) is not next to D (slot 1). Every arrangement satisfying the ordering and adjacency constraints appears among these 8 candidates, and only 3 survive the "B not adjacent to D" and "E not first or last" constraints. So the three orderings above are the only valid ones. ==== END RESPONSE ====
Streaming with extended thinking
This example shows how to handle streaming with thinking:
--- Starting thinking block --- I need to work through the constraints: D before A before C, E adjacent to C, and B not adjacent to D. I'm trying to build the ordering by placing D, A, C first then inserting E next to C and checking where B can go without violating the adjacency rule with D. Testing "D A C E B" works since D and B aren't adjacent and E sits at position 4, not the ends. Now I'm checking the other case where E comes immediately before C, inserting B in each of the four gaps and verifying the adjacency and positional rules each time. After going through DBAEC, DABEC, and DAECB, I find that DABEC and DAECB both satisfy every condition while DBAEC fails because B ends up next to D. Combined with the earlier valid case, that gives three total valid orderings: DACEB, DABEC, and DAECB. [Completed thinking block, 777 characters] --- Finished thinking block --- --- Starting text block --- **Valid orderings (3):** 1. D A C E B 2. D A B E C 3. D A E C B **Why there are no others** 1. **Chain.** "D before A" and "A before C" force the relative order D < A < C. 2. **Treat C and E as a block.** E must be immediately before or after C, so C and E form a two-letter block, either "CE" or "EC". The remaining pieces are D, A, the block, and B. D, A and the block must appear in that order, since the block contains C. B can go in any of 4 places: before D, between D and A, between A and the block, or after the block. That gives 4 candidates for each block orientation, 8 in total. 3. **Block "CE"** (the order is D, A, CE, with B inserted): - B D A C E: E is last, so it is invalid. - D B A C E: E is last, so it is invalid. - D A B C E: E is last, so it is invalid. - **D A C E B:** E is 4th, and B (5th) is not next to D (1st). Valid. 4. **Block "EC"** (the order is D, A, EC, with B inserted): - B D A E C: B and D are adjacent, so it is invalid. - D B A E C: B and D are adjacent, so it is invalid. - **D A B E C:** B is 3rd and D is 1st, so they are not adjacent. E is 4th. Valid. - **D A E C B:** E is 3rd, and B (5th) is not next to D (1st). Valid. 5. **Completeness.** Every ordering satisfying the "D < A < C" and "C adjacent to E" constraints appears among those 8 candidates. The "E not first or last" and "B not adjacent to D" constraints eliminate exactly 5 of them, leaving the three above.--- Finished text block --- --- Message complete ---
Token counting and context window management
This example demonstrates how to track token usage with extended thinking, and how the effort setting changes how much Claude thinks:
Base token count (input only): 102 Input tokens (from usage): 102 Output tokens (from usage, thinking + answer): 1595 Estimated final answer tokens: ~518 So roughly ~1077 output tokens went to thinking Thinking summary returned: 457 characters Input tokens + max_tokens = 16102 (this total must fit in the model's context window) With effort='low': Output tokens (thinking + answer): 1043 Thinking summary length: 0 characters Final answer preview: Constraints: 1. A before C 2. B not adjacent to D 3. E in slot 2, 3, or 4 4. C adjacent to E 5. D before A From 1 and 5... With effort='medium': Output tokens (thinking + answer): 794 Thinking summary length: 0 characters Final answer preview: Constraints: 1. A before C 2. B not adjacent to D 3. E in slot 2, 3, or 4 4. C adjacent to E 5. D before A From 1 and 5... With effort='high': Output tokens (thinking + answer): 1150 Thinking summary length: 423 characters Final answer preview: **Valid orderings (slots 1–5):** 1. D A C E B 2. D A E C B 3. D A B E C **Why there are no others** The constraints D... With effort='xhigh': Output tokens (thinking + answer): 1332 Thinking summary length: 813 characters Final answer preview: **Valid orderings (slots 1–5):** 1. D A C E B 2. D A B E C 3. D A E C B **Why there are no others** 1. **Chain.** "D b...
Understanding redacted thinking blocks
Occasionally Claude's internal reasoning will be flagged by safety systems. When this occurs, we encrypt some or all of the thinking block and return it to you as a redacted_thinking block. These redacted thinking blocks are decrypted when passed back to the API, allowing Claude to continue its response without losing context.
This example demonstrates working with redacted thinking blocks using a special test string that triggers them:
[RedactedThinkingBlock(data='CAQSlgcKEAgSGAI4AUIIdGhpbmtpbmcSDAvmF1hXsbzdc6DDlRoMIy9+bFB/la3LWaH1IjC+I73fnh6GNH2OQ+TidhopDIDmbIW4mmtPmzkCaQ8UTME/PoGMzIXaO/eVST+TaIoqswZLLsrmRNXVM/3MhNxyw8jVP+FUnkD7uGtVa42A2hGhP9K3EULP3DSzs1CO/iPrA/4LJChjLBrcwxnQEfHQW5zPbhujyTxBQtmM6jGXhZCD1O941b2NVBz35R2EPIcLMIO23k6F1p3g8UtucM8AvfbJ8qyO+JMK1/lAKsRjEuE+w0HUzYV1J526sVbUuqCYlHDsVnNHdVTPm802tuEZJo6NdR3On14JGpl4jmq0auTenXRSFCqgyiAefctT6uTFKzvzUYgYiaHGTxfxtHrN6/JPWwGhCBDPdqF8tr1dk4pzYhTY5a4cmmHGi4f++OMknWHM0Vp7OKWZZRbVvrbnm2Quwcv1gjDP2/+cUWyVg5urBpy39bU6mPNasiQkrPg0yjB/VpBLOjQ5EE+zAZILCEo4zLY7K2WD1+UHfmTi59+mfnDcqaFLjhVRK3mqY5Ut0C9IO5JFKYXyZGITljOhADq3amYPthG4ES+wgh3I4HAVNj9wo0kh2KiS7h1HDh20vF1o+Q4RpF9XPKL3OYcLPQDHxX7ZHzHLGzERnTsNGDQnxYAy95AAzn1lcnOk/rPDK+q+uqptheVgraDCxqXPtZrYyI/P7qlirMGbCoDmyi4IzAC4n1ej09Li0G0pGe8B9C49nSwn5Czn1L23/BSX7XLzddaf+/aEHOhAD7h7w+ru/NI0WBBmbyiXh8XrzRwtFmRfhhSrWAGOnFc2VtCHUijFz4XTGf6CNG0QLTw7XlT6fS+02kai2aNcIjqhhcD32cEmo8Hj9ZRDhWvfPJuHAAv9PVF14W9F+tFGSc6iW/PyQqMRRVDc4yBSUJv/va3UpBXm8Sopl3ayXgdK/OtU/dvp0FB2L+zgaFheVFARV7Y8xx4nWlSzIygix0nhPyh7+tLREn8aAfFQl3as4loRV8kcVbAI0r3KiRZzzUWshhZNfpAmnv+LGqi0dZ+dTpJPCoxPV4gt4U5fInVs/coz3Q5Yepa9SLfAvbusvwN3NoC/swXfY6pro5+6N6PKGPRSZcnj8HCVN7+SHM4LILMRuRicwBA+eJtzYmbDraTAiPg7tvpI202sh3yTgu/9PHFdQN/eQ4oYAQ==', type='redacted_thinking'), TextBlock(citations=None, text="That message looks like a test string or placeholder, not a question or request. It doesn't trigger any special behavior on my end.\n\nWhat would you like to talk about or work on? I can help with writing, coding, analysis, brainstorming, or answering questions.", type='text')] Response includes 2 total blocks: - 1 redacted thinking blocks - 0 regular thinking blocks - 1 text blocks Redacted thinking blocks contain encrypted data: Block 1 data preview: CAQSlgcKEAgSGAI4AUIIdGhpbmtpbmcSDAvmF1hXsbzdc6DDlR... Final text response: That message looks like a test string or placeholder, not a question or request. It doesn't trigger any special behavior on my end. What would you like to talk about or work on? I can help with writing, coding, analysis, brainstorming, or answering questions.
Handling error cases
When using extended thinking, keep in mind:
-
Effort, not budgets: Current models have no
budget_tokens. Useoutput_config={"effort": ...}to control how hard Claude thinks: start at the default ("high"), drop to"medium"or"low"when latency and cost matter more than depth, and reach for"xhigh"or"max"on the hardest problems. Sending the legacythinking={"type": "enabled", "budget_tokens": N}form to Claude Sonnet 5.5 returns a 400 error. -
Incompatible features: Non-default
temperature,top_p, ortop_kvalues are rejected on Claude Sonnet 5.5 (with or without thinking), and you cannot pre-fill assistant responses. -
Pricing and limits: Thinking tokens count towards
max_tokensand the context window, and are billed as output tokens (you are billed for the full thinking, not the summary you get back). They also count towards your rate limits. Keepmax_tokensgenerous and use streaming for very large values.
For more details on extended thinking with tool use, see the "Extended Thinking with Tool Use" notebook.
Error with legacy budget_tokens thinking config: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': '"thinking.type.enabled" is not supported for this model. Use "thinking.type.adaptive" and "output_config.effort" to control thinking behavior.'}, 'request_id': 'req_011Cfdh4WYGPktVi5kjF5NAt'}
Error from exceeding context window: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'prompt is too long: 1800065 tokens > 1000000 maximum'}, 'request_id': 'req_011Cfdh4XWok6x352LDwYdg6'}