Skip to content
AgenticAIWiki
  1. Learn
  2. Part 2
  3. 13. Chain of thought and output steering
  4. 13.2

Lesson 13.2 · 2 of 6 · 4 min read · Reviewed 24 Nov 2026

Separating reasoning from the final output

By the end, you can

  • Separate reasoning from the final output with tags.
  • Extract only the answer in code.

Reasoning helps accuracy, but users rarely want to read it. Separate the two with tags:

Work through the refund policy in <thinking> tags.
Then write the reply to the customer in <answer> tags.

In code, pull out only the answer:

const answer = text.match(/<answer>([\s\S]*?)<\/answer>/)?.[1]?.trim();

Keep the reasoning

Log the <thinking> part. When an answer is wrong, the reasoning usually shows where it went off track: a rule misread, a number copied wrong. That’s gold for improving the prompt.

With extended thinking

If you use extended thinkingExtended thinking: A mode in which the model reasons step by step in thinking blocks before giving its final answer., the API already returns thinking in its own content blockContent block: One typed piece of a message, such as text, image, document, tool_use or tool_result., separate from the text. You get the same split without tags.

Key points

  • Ask for reasoning in <thinking> tags and the result in <answer> tags.
  • Your code shows users or downstream systems only the <answer> part.
  • Keeping reasoning lets you debug wrong answers later.
Hands-on labRuns in the Playground

Split reasoning from the shown answer

Write the code that shows users only the tagged answer and treats a missing answer as a failure.

You will have: An answer_only function and a small report of what was shown, logged and failed for five replies.

Steps

  1. Look at the five sample replies, including one with no <answer> tags and one with two.
  2. Complete answer_only so it returns the stripped text inside the first <answer> pair, or None if there is none.
  3. Complete thinking_only so the reasoning can be logged.
  4. Run the report and check no reply with missing tags is ever shown.

Done when

  • Replies without <answer> tags return None and count as failed, never shown.
  • The report prints 'shown: 4, failed: 1'.
  • Logged reasoning is kept for every reply that has <thinking> tags.

Open in the Playground No API key needed.

Starter code (Python)
import re

REPLIES = [
    "<thinking>30-day window, delivered 12 days ago.</thinking><answer>Yes, you can return it.</answer>",
    "<thinking>Final-sale item.</thinking><answer>Sorry, this item cannot be returned.</answer>",
    "Policy 4.2 says 30 days, so yes, you can return it.",
    "<thinking>Two items.</thinking><answer>The boots can go back.</answer> <answer>The socks cannot.</answer>",
    "<thinking>\nMulti-line\nreasoning.\n</thinking>\n<answer>\nYes, within the window.\n</answer>",
]

def answer_only(reply):
    # TODO: return the text inside the first <answer>...</answer>, stripped. Return None if missing.
    # Hint: re.search with re.S so the match can span lines.
    return reply

def thinking_only(reply):
    # TODO: return the text inside <thinking>...</thinking>, or "" if missing.
    return ""

shown = failed = 0
for r in REPLIES:
    a = answer_only(r)
    if a is None:
        failed += 1
        print("FAILED: send fallback message, log for review")
    else:
        shown += 1
        print("SHOW:", a, "| LOG:", thinking_only(r).strip()[:30])
print(f"shown: {shown}, failed: {failed}")
Show a solution
import re

REPLIES = [
    "<thinking>30-day window, delivered 12 days ago.</thinking><answer>Yes, you can return it.</answer>",
    "<thinking>Final-sale item.</thinking><answer>Sorry, this item cannot be returned.</answer>",
    "Policy 4.2 says 30 days, so yes, you can return it.",
    "<thinking>Two items.</thinking><answer>The boots can go back.</answer> <answer>The socks cannot.</answer>",
    "<thinking>\nMulti-line\nreasoning.\n</thinking>\n<answer>\nYes, within the window.\n</answer>",
]

def answer_only(reply):
    m = re.search(r"<answer>(.*?)</answer>", reply, re.S)
    return m.group(1).strip() if m else None

def thinking_only(reply):
    m = re.search(r"<thinking>(.*?)</thinking>", reply, re.S)
    return m.group(1) if m else ""

shown = failed = 0
for r in REPLIES:
    a = answer_only(r)
    if a is None:
        failed += 1
        print("FAILED: send fallback message, log for review")
    else:
        shown += 1
        print("SHOW:", a, "| LOG:", thinking_only(r).strip()[:30])
print(f"shown: {shown}, failed: {failed}")
Quick checkYour app shows Claude's output to customers but you want reasoning for accuracy. What's the cleanest setup?
How sure are you?
Pick an answer and how sure you are