D4 · Prompt Engineering › Cluster 5 · Chain of Thought & Extended Thinking

5.2 · Structured Thinking with Tags

<thinking> then <answer>

CONCEPT Separate the reasoning from the deliverable

Plain CoT (5.1) improves accuracy but leaves the reasoning mixed into the visible response. That's fine when a human reads the whole thing — but in an application you usually want to ship the answer and drop the scratch work. Structured CoT solves this: instruct Claude to do its reasoning inside <thinking> tags and put the final result inside <answer> tags. Now the two are on clearly labelled seams, so your code can parse out the answer and strip the reasoning.

🔑 The tags are a contract between prompt and parser. The model gets its reasoning room; your pipeline gets a clean, extractable deliverable. Same request, two consumers.

CONCEPT Why XML tags specifically

Claude is trained to respect XML-style tags as structural delimiters, so a <thinking>…</thinking> block is unambiguous to a regex or XML parser — far more robust than trying to guess where "the reasoning" ends and "the answer" begins in free prose. The same tag discipline shows up in few-shot examples: wrap the reasoning of each example in <thinking> and Claude generalizes that pattern to its own responses.

Design move: name the tags in the prompt, and make your extractor read only the <answer> block. The <thinking> block is retained for debugging/logging but never shown to the user.
⚠️ Structured-tag CoT is prompted reasoning in the normal response body — it is not the native thinking parameter. Native extended thinking (5.3) returns reasoning in separate content blocks you never have to tag or regex out. Don't conflate the two.

DIAGRAM One response, two extraction paths

flowchart TD P["Prompt: 'reason in <thinking>,
answer in <answer>'"] --> R["Model response
<thinking>…</thinking><answer>…</answer>"] R --> PARSE{"Parser splits
on tags"} PARSE -->|"<answer> block"| SHIP["🟢 Ship to user"] PARSE -->|"<thinking> block"| LOG["🔵 Log / debug only
(stripped from output)"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef mid fill:#cfe2ff,stroke:#0d6efd,color:#084298; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; class SHIP ok; class LOG mid; class PARSE gate;

CODE Prompt + extract the answer, drop the reasoning

The prompt fixes the output shape; a small parser reads the <answer> block and discards the rest.

# Prompt tells Claude exactly where to reason and where to answer.
system = """Solve the problem. Do all reasoning inside <thinking> tags.
Put ONLY the final result inside <answer> tags. Nothing outside the tags."""

resp = client.messages.create(
    model="claude-opus-4-8", max_tokens=2000,
    system=system,
    messages=[{"role": "user", "content": "A train leaves at 2pm going 60mph..."}],
)
text = resp.content[0].text

# Strip the reasoning: keep only what's inside <answer>…</answer>.
import re
m = re.search(r"<answer>(.*?)</answer>", text, re.DOTALL)
final = m.group(1).strip() if m else text   # ship 'final'; log the thinking block

TRADE-OFFS Structured tags vs. plain CoT vs. native thinking

Plain CoT (5.1)Structured tags (5.2)Extended thinking (5.3)
Reasoning locationMixed into responseIn <thinking> in the response bodySeparate thinking content blocks
How you strip itHard — no clean seamParse/regex the tagsIgnore the thinking blocks — already separate
Extra tokens billedYes (reasoning)Yes (reasoning)Yes (thinking tokens)
Requires prompt engineeringYesYes (tag contract)No — native thinking param

EXAM TRAPS

Trap: Believing <thinking> tags equal the native thinking feature. → They're a prompting convention in the normal response. Native extended thinking uses the thinking parameter and returns separate blocks.
Trap: "The tags hide the reasoning from the user automatically." → No. The reasoning is still generated and billed; you must parse it out and choose not to display it.
Trap: Putting text outside the tags. → Instruct the model to emit nothing outside <thinking>/<answer>, or your parser will miss stray preamble.
Trap: Using structured tags on a trivial task. → If there's no complex reasoning to strip, the tags add tokens for nothing — this pattern earns its keep only when CoT itself does (5.1).

PRACTICE

Q1 · Why wrap reasoning in <thinking> and the result in <answer>?

So the two are on labelled seams your code can parse. You extract the <answer> block to ship and strip the <thinking> block from what the user sees — reasoning room for the model, a clean deliverable for the app.

Q2 · Does the tag pattern stop the reasoning tokens from being billed?

No. The reasoning is still generated and billed as output tokens; the tags only make it easy to separate and discard from the shown response. To keep reasoning in dedicated blocks entirely, use native extended thinking (5.3).

Q3 · How is structured-tag CoT different from the thinking parameter?

Structured tags are prompted — reasoning lives in the normal response body inside XML tags you defined. The thinking parameter is a native capability that returns reasoning in separate thinking content blocks, no tagging or regex required.