Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Objectives: LO-6.1, LO-6.2, LO-6.3
Prerequisites: Module 5, and the content blocks and cost formula from Module 2
Time: about 6 hours: 2 for this reading, 2.5 for Lab 6, and 1.5 for the practice

Totaling a repair estimate needs intermediate work before the answer; routing a plain ticket needs little. This module covers three ways to give a model room for that work and keep the result checkable. The first is thinking, with the effort setting that controls it. The second is an explanation in tagged sections, and the third is a chain of separate requests. In Lab 6 you choose an effort level and write an extractor that fails loudly. Then you build a three-step chain with a stand-in model and compute how its errors compound.

Why reasoning before the answer helps

Module 1 showed that a model writes one token at a time, and each token joins the context for the next. A model that writes its answer first commits to it before any intermediate work. A model that writes the steps first can use them when it writes the answer.

Wei and colleagues called those steps a chain of thought. They put a few worked examples with intermediate steps in the prompt, and large models did better on arithmetic, commonsense, and symbolic reasoning tasks. A model with 540 billion parameters, given eight such examples, reached state-of-the-art accuracy on GSM8K, a set of math word problems. The authors describe the ability as emerging in sufficiently large models.

Current Claude models do this work themselves. Anthropic’s thinking documentation says that when thinking is active, Claude works through the problem before answering, in thinking content blocks that come before the text blocks. Thinking tokens are billed as output tokens, even when the thinking text isn’t returned, and they count toward max_tokens.

On Claude Opus 5.5, Sonnet 5.5, and Haiku 5.5, a request with no thinking field gets adaptive thinking: the model decides when and how much to think. On Opus 5.5 it can’t be turned off. The Opus 5.5 migration guide says that a request setting thinking: {"type": "disabled"} returns a 400 error, which means the API rejects it. It also warns that code reading the answer from content[0] breaks, because a thinking block can come first. Pick out the text blocks by type, as Module 2 did.

Worked example: what a thinking block costs

Here are the main fields of an illustrative response from Opus 5.5, in the documented shape. A person wrote it for this course; no model produced it.

{
    "content": [
        {"type": "thinking", "thinking": "", "signature": "..."},
        {"type": "text", "text": "<answer>billing</answer>"},
    ],
    "stop_reason": "end_turn",
    "usage": {"input_tokens": 220, "output_tokens": 310},
}

The thinking field is empty because current models default to display: "omitted"; the signature holds an encrypted copy of the thinking, which later turns pass back unchanged. The visible answer is a few tokens, yet usage reports 310 output tokens, almost all of them thinking. At Opus 5.5’s $4 input and $20 output prices per million tokens, Module 2’s formula gives

C=220×4+310×201,000,000=880+6,2001,000,000=0.00708 dollars.C = \frac{220 \times 4 + 310 \times 20}{1{,}000{,}000} = \frac{880 + 6{,}200}{1{,}000{,}000} = 0.00708 \text{ dollars}.

Output, mostly thinking, is 6,200 of those 7,080 millionths of a dollar. And response["content"][0]["text"] raises a KeyError: the first block has no text.

Choose an effort level from measurements

Effort is a request setting, output_config.effort, that tells the model how many tokens to spend on its response. Anthropic’s effort documentation lists five levels: low, medium, high, xhigh, and max. Opus 5.5 and Haiku 5.5 default to medium; most other models, Sonnet 5.5 among them, default to high. The page also says:

To spend fewer thinking tokens on Opus 5.5, the migration guide says, lower the effort. Here is a request that sets effort:

request = {
    "model": "claude-sonnet-5-5",
    "max_tokens": 4000,
    "output_config": {"effort": "medium"},
    "messages": [{"role": "user", "content": "Total this work order: ..."}],
}

An effort sweep is a small experiment. Write its rules before you see results, so you can’t pick the level you hoped for and then find a reason:

  1. Write each requirement as a threshold, such as a minimum accuracy and a maximum median latency. Latency is the time to the full response; half the requests beat the median.

  2. Write the rule for choosing among the levels that pass, such as least cost, with ties broken by lower latency.

  3. Measure every level on the same labeled cases.

  4. Drop each level that misses a threshold, then apply the rule. If none is left, report that. Then change the prompt, the model, or the requirement on purpose.

Worked example: effort for repair estimates

Handbook section H07 says a work order names the requested service and parts. Suppose a program asks Sonnet 5.5 to total each work order for the customer. The thresholds are a correct total on at least 95% of labeled work orders and a median latency of at most 6 seconds. The rule picks the cheapest level that meets both. The table is illustrative, written for the course; no model was measured. Each request has 800 input tokens, and the output tokens include thinking.

EffortCorrect totalsMedian latencyOutput tokens per requestCost per 1,000 requests
low81%1.9 s150$3.10
medium93%3.8 s450$6.10
high96%5.2 s900$10.60
xhigh97%9.6 s2,000$21.60

The costs use Sonnet 5.5’s $2 input and $10 output prices: for high, one request costs (800 × 2 + 900 × 10) / 1,000,000 = 0.0106 dollars.

  1. low and medium miss the 95% floor.

  2. xhigh is the most accurate, but its 9.6-second median misses the 6-second ceiling.

  3. high is the only level left, so the program uses it.

Two cautions remain. On 200 work orders, 96% and 93% are 192 and 186 correct; Module 7 shows how to tell whether a gap that size is real. And the measurements belong to one model and one prompt, so change either and the sweep runs again. Lab 6’s Exercise L6.1 applies these steps to another table, and practice Exercise P6.1 changes a requirement.

Ask for an explanation you can check

Sometimes you need to see why the model chose its answer: a reviewer must approve it, or a test failed. Two different things can answer “why”:

Don’t ask a current model to copy its internal reasoning into the answer. Anthropic’s refusals page lists Opus 5.5, Sonnet 5.5, and Haiku 5.5 among the models that may refuse such a request, with stop_reason "refusal" and stop_details.category "reasoning_extraction". A common trigger, it says, is a <thinking> or <reasoning> section filled in before the answer. The page advises asking instead for a short explanation or the evidence behind a result. Anthropic’s prompting best practices keep the older technique, reasoning written out before an <answer> section, as a fallback for when thinking is off.

Worked example: evidence and an answer

Ticket T12 in data/tickets.json reads “The quote and the final charge differ,” and its label is billing. A prompt that asks for checkable evidence:

Route the ticket to the queue that owns the customer's current request: repair, billing, or
account.

<ticket>The quote and the final charge differ</ticket>

Reply with exactly two sections and nothing else:
<evidence>the words from the ticket that decide the queue, copied exactly</evidence>
<answer>one queue name: repair, billing, or account</answer>

An illustrative reply, written for the course:

<evidence>the final charge differ</evidence>
<answer>billing</answer>

Code can check both parts: the evidence appears in the ticket, and the answer is a queue name that matches the label. A reviewer reads four words instead of guessing.

An extractor has a narrower job: find exactly one answer section and return its text. It should reject each of these illustrative replies:

ReplyWhy the extractor rejects it
<evidence>…</evidence> and then Billing.No answer section, so nothing is where code looks
<answer> with only spaces before </answer>The section is empty
<answer>billing</answer> and later <answer>account</answer>Code can’t tell which section the model meant
<answer>billing with no closing tagUnclosed, often because the reply hit max_tokens

Fail loudly: raise an exception with a message, never return a guess. Suppose the extractor returned "repair" whenever it found no answer. A malformed reply to a billing ticket would become a well-formed wrong label. An evaluation would then count a content error and hide the format problem, which Module 3 tracks separately. An exception lets the caller retry, log the reply, or send the ticket to a person.

Check stop_reason before extracting, as Module 2 did. And extraction checks shape only: <answer>account</answer> for T12 extracts cleanly and is wrong. Module 8’s structured outputs can replace this parsing for a fixed output contract. Lab 6’s Exercise L6.2 builds the extractor, and practice Exercise P6.2 asks you to break one.

Split a task into a chain

A chain is a sequence of requests in which each step’s output becomes part of the next step’s input. The prompting best practices say that with adaptive thinking, Claude handles most multistep reasoning in one request. Chaining still helps when you need to inspect intermediate outputs or enforce a fixed sequence of steps. Their most common pattern is self-correction: draft, review the draft against criteria, then refine. Each step is a separate call, so a program can log, evaluate, or branch between steps.

Give each step one job and its own output contract, such as an <answer> section. Keep data in tags, as Module 4 did. Extract and check each output before the next step uses it. And pass forward the original input along with the earlier results.

Worked example: a three-step approval request

Handbook section H07 says to ask for approval before adding any paid service. A work order reads: “Customer asked for a brake adjustment. The mechanic found a worn chain.” The shop wants a message asking the customer to approve a chain replacement.

  1. Extract. Given the work order, list the requested and the additional services. Illustrative output: <answer>requested: brake adjustment; additional: chain replacement</answer>.

  2. Draft. Given the work order, the policy, and step 1’s list, write the message.

  3. Review. Given the work order, the policy, the list, and the draft, return approved, or rejected with a reason.

Step 2’s prompt carries the original work order along with step 1’s result:

<work_order>Customer asked for a brake adjustment. The mechanic found a worn chain.</work_order>
<policy>Ask for approval before adding any paid service.</policy>
<services>requested: brake adjustment; additional: chain replacement</services>

Write a two-sentence message to the customer that asks them to approve each additional service.
Put the message in <answer> tags.

Why does every step get the work order? Suppose step 1 misses the chain and lists no additional services. Step 2 writes a message that asks for nothing, and a review step that sees only the list and the draft finds them consistent. A review step that sees the work order can notice the worn chain and reject. Lab 6’s chain has the same shape with other steps: classify, draft, and check.

A chain also costs more. In an illustrative plan on Sonnet 5.5, one combined call takes 500 input and 300 output tokens, for $0.004. The three steps take 500 and 40, 700 and 260, and 950 and 30, because each resends the work order. Together they cost (2,150 × 2 + 330 × 10) / 1,000,000 = $0.0076. The calls run one after another, so their latencies add too.

How errors compound

A chain is right only if every step is right. A step’s conditional success rate is the share of runs it gets right among the runs in which every earlier step was right. The chance that all steps are right is the product of those rates.

Worked example. Three steps succeed at conditional rates 0.98, 0.95, and 0.90. After step 1, 0.98 of runs are right; after step 2, 0.98 × 0.95 = 0.931; after step 3, 0.931 × 0.90 = 0.8379. The chain is right on about 84% of runs, less than its weakest step’s 90%.

The rates must be conditional. In a two-step chain, say step 1 is right 95% of the time. Step 2 is right 90% of the time after a correct step 1, and 20% after a wrong one, because the original input sometimes lets it recover. Over all runs, step 2 is right 0.95 × 0.90 + 0.05 × 0.20 = 0.865 of the time. Both steps are right 0.95 × 0.90 = 0.855 of the time. Multiplying the overall rates, 0.95 × 0.865 ≈ 0.822, is wrong, because step 2’s failures depend on step 1’s. To measure a conditional rate, give the step labeled inputs whose earlier steps are known to be right.

Now add a check that rejects 60% of bad runs and, for this example, accepts every good one. Of all runs, 1 − 0.8379 = 0.1621 are bad. The check rejects 0.1621 × 0.6 ≈ 0.097 of all runs, and 0.1621 × 0.4 ≈ 0.065 slip through. No run became correct: about 84% are still right, and about 10% now need a retry or a person. A real check also rejects some good runs, and Module 7 measures both rates. Lab 6’s Exercise L6.4 computes these numbers, and practice Exercises P6.3 and P6.4 apply them.

Common misconceptions

Summary

Self-check

Answer each question before you open its answer.

  1. LO-6.1. What would rule out the high level in the worked example?

    Answer

    Any requirement it misses: a latency ceiling below 5.2 seconds, an accuracy floor above 96%, or a cost cap below $10.60 per 1,000 requests. Each would leave no level at all, because high is the only one that met both original requirements. The right report is then “no level qualifies”, not the nearest miss.

  2. LO-6.2. What if there are two final answer tags?

    Answer

    Reject the result as ambiguous by raising an exception. The two sections may disagree, and code can’t know which one the model meant, so taking the first would hide the problem.

  3. LO-6.3. If a two-stage chain has conditional success rates 0.9 and 0.8, what is its all-correct rate?

    Answer

    0.9 × 0.8 = 0.72. Multiplying is right because 0.8 is the second stage’s rate on runs where the first stage was right.

  4. LO-6.1. A response from Opus 5.5 shows a one-word answer but reports 400 output tokens. Is the count wrong?

    Answer

    No. Thinking tokens are billed as output even when the thinking text is omitted, so most of the 400 are thinking. To spend fewer, lower the effort.

  5. LO-6.2, LO-6.3. A prompt for Sonnet 5.5 asks the model to fill in a <reasoning> section before answering, and a chain’s review step sees only the draft. What would you change?

    Answer

    Ask for the evidence behind the answer instead of its reasoning, because the model may refuse with the category "reasoning_extraction". Give the review step the original input too: only the input shows what the draft should have said.

Further reading

References
  1. Wei, J., & others. (2022). Chain-of-Thought Prompting Elicits Reasoning in Large Language Models. Advances in Neural Information Processing Systems (NeurIPS), 35. 10.52202/068431-1800