Claude and the Anthropic SDK · Chapter 16 of 21

Thinking blocks in a tool loop

By the end of this chapter you can run a tool loop that passes Claude's thinking blocks back exactly as they came, explain what stripping them costs, recognise the two 400s an edited history causes (an edited thinking block, an edited earlier turn), fix them the append-only way, and name the documented escape for when a history must change. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Cost
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.

Why you need this

Sahay's tool loop from chapter 6 works. Now Arjun wants it tidy: the history is stored after every turn, and he would like to strip the thinking blocks ("nobody reads them"), trim the summaries in the log, and, when Kamla adds a detail, fix up her first message instead of adding a new one. Each of those looks like housekeeping.

On the current models, each of them either costs money or fails. A thinking block is not a note for you: it is part of the conversation, signed by the API, and Claude reads it back on the next turn. Opus 5.5, Sonnet 5.5 and Haiku 5.5 check that the conversation you send is the one each block was made in. The rule that keeps everything working is short: the history is append-only.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    keep_the_blocks.py: keep the thinking blocks

    Run Sahay's loop for QUESTION (given), at most 6 turns: model "claude-opus-5-5", max_tokens 4096, system=SYSTEM, tools=TOOLS, the messages, and a top-level cache_control={"type": "ephemeral"}. After each reply print turn N: STOP, T thinking blocks, R read (usage.cache_read_input_tokens), then append {"role": "assistant", "content": reply.content} unchanged.

    On "tool_use", run every tool_use with run_tool and append one user message of tool_result blocks; otherwise print the text of the reply's text blocks and stop. Every turn must read more from the cache than the one before.

  2. Task 2

    follow_up.py: a follow-up, with and without the thinking

    history = answer_claim(QUESTION) (given). Send FOLLOW_UP (given) as a new user message twice, with the loop's request (model "claude-opus-5-5", max_tokens 4096, system=SYSTEM, tools=TOOLS, top-level cache_control): first after tidy(history) (given), then after history unchanged.

    Print stripped: I in, W written, R read, $COST, then kept: the same, COST with PRICES["claude-opus-5-5"] (input, 5-minute writes, reads, output; 6 decimals).

  3. Task 3

    edited_history.py: an edited first message

    history = answer_claim(QUESTION) (given). Send a new list whose first message is EDITED_FIRST (given), then the rest of history, then FOLLOW_UP (given) as a new user message (model "claude-opus-5-5", max_tokens 4096, system=SYSTEM, tools=TOOLS); catch the anthropic.BadRequestError and print edited: STATUS TYPE.

    Then send history unchanged plus one new user message, NOTE, a blank line, then FOLLOW_UP. Print appended: STOP, then the text of the reply's text blocks.

  4. Task 4 · interview question

    Interview: trimmed_thinking.py

    "A logger shortens thinking summaries, and the next request fails. Why?" Send QUESTION (given) to "claude-opus-5-5" (max_tokens 4096, system=SYSTEM, tools=TOOLS, thinking={"type": "adaptive", "display": "summarized"}); it asks for a tool. Build a tool_result for every tool_use with run_tool.

    Send the next turn with the assistant content passed through trim_for_log (given); catch the anthropic.BadRequestError and print trimmed: STATUS TYPE. Then send it with reply.content unchanged and print unchanged: STOP.

What you will learn

  • Thinking in a tool loop: a block before each tool call, passed back on the next turn.
  • What stripping costs: the cache misses from the first stripped turn.
  • The signature 400: an edited thinking block.
  • The "bound to a different conversation" 400: an edited earlier turn.
  • Append-only history, and drop_block, the documented escape.

More in Cost

Build "Thinking blocks in a tool loop" yourself

Parchi, a Jaipur startup, helps families claim health insurance. You build Sahay, a claims assistant that reads bills and policies with Claude, and measure what caching, models and effort cost. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.