الانتقال إلى المحتوى
Claude وأدوات الـ AI للبرمجةClaude API و Tool Use· 7 دقائق للقراءة

Don't Strip Thinking Blocks: Thinking Inside Tool-Use Loops

A thinking block is part of the response Claude is still building. Filter or edit it, or the history before it, and the next request fails.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • Claude API
محتويات المقال

Your incident agent runs on Claude Opus 5.5 with two tools, query_logs and list_deploys. To keep requests small, a helper "cleans" the history before every call: it keeps only the text and tool_use blocks of each assistant turn, since the thinking blocks come back with an empty thinking field anyway, and it cuts old log results to 2,000 characters. Every test passes on your API key: the test incidents are simple, and adaptive thinking can skip thinking on simple requests. Then a customer runs the agent on a real outage, and the second request fails with a 400: thinking blocks "in the latest assistant message cannot be modified".

The common misconception

"Thinking blocks are disposable output, so you can drop or edit them in the history to save tokens during tool use." It sounds like ordinary context hygiene:

  • On Claude Opus 5.5 the thinking field is empty by default, so the block looks like dead weight.
  • The thinking is about steps Claude already took; the tool calls and the answer seem to be what matters.
  • Outside tool use, the docs do allow leaving out earlier turns' thinking.
  • Long histories cost input tokens, and trimming old content is a standard way to keep an agent cheap.

The half-truth: outside a tool loop you may omit earlier turns' thinking, and the API already filters what each model needs. But inside a tool loop the thinking block belongs to an unfinished response, and on current models every block is tied to the exact history that came before it.

What actually happens

A tool loop is one assistant turn

From the model's side, the assistant turn ends only when Claude finishes its full response, and every tool call and result in between belongs to that turn. The docs explain why the reasoning must come back: "When Claude calls a tool, it pauses construction of its response to await external information. When you return the tool result, Claude continues building that same response, so its earlier reasoning must still be present." Their rule of thumb has three levels: within a tool-use turn, passing back is required; across turns, passing everything back is recommended; outside tool use, omitting prior turns' thinking is allowed. The same turn also has to run in one thinking mode, so change thinking settings between turns, not inside a loop.

An empty thinking field isn't an empty block

On Claude Opus 5.5, display defaults to "omitted": the block's thinking field is an empty string, but "the signature field still carries the encrypted full thinking for multi-turn continuity". You also paid for that reasoning already: "You're still charged for the full thinking tokens. Omitting reduces latency, not cost." Opus 5.5 can also write short progress updates between tool calls, and each one arrives as another thinking block with its own signature. All of them go back with the turn.

The API checks what you send back

Two checks catch an edited history. First, the latest assistant message must match what the API returned: you can't rearrange, edit or partially drop its thinking blocks, redacted_thinking included. A message rebuilt by your code or filtered by block type fails with a 400 whose message contains "thinking or redacted_thinking blocks in the latest assistant message cannot be modified".

Second, on Claude 5.1 and later models the API checks each block's prefix: the system prompt, the tools, and every message before the block. If any of them changed since the block was produced, that block and every later one fail. Shortening an old tool_result is exactly such an edit. On Claude Opus 5.5, an account created on or after August 31, 2026 gets a 400 that says the block "is bound to a different conversation"; an older account gets no error. Retrying the same body fails the same way every time.

Fixing the loop

The first version filters each assistant turn and trims old results. The second sends every turn back as it came:

Python incident_agent.py
import anthropicclient = anthropic.Anthropic()messages = [{"role": "user", "content": INCIDENT}]while True:    reply = client.messages.create(model="claude-opus-5-5", max_tokens=16000,                                   tools=TOOLS, messages=messages)    # "Thinking comes back empty on Opus 5.5: keep only what matters"    kept = [b for b in reply.content if b.type in ("text", "tool_use")]    messages.append({"role": "assistant", "content": kept})    if reply.stop_reason != "tool_use":        break    shorten_old_tool_results(messages, limit=2000)  # "save tokens"    messages.append({"role": "user", "content": run_tools(reply)})print(next(b.text for b in reply.content if b.type == "text"))

On a hard incident, the first version fails on its second request: Claude thought before calling a tool, and the filtered turn no longer matches the one the API sent. Even if it didn't, the trimmed log would invalidate every later thinking block on newer accounts.

Change to the historyResult on Claude Opus 5.5
Append each turn exactly as receivedValid: reasoning carries on, and thinking blocks are cached with tool results
Rebuild or filter the latest assistant message400: thinking blocks "cannot be modified"
Edit the text of a thinking block400, except text placed in an empty omitted block, which is ignored
Leave out earlier turns' thinking, oldest first or all of it, outside the current tool turnValid, but the model loses that reasoning
Remove a block from the middle, or shorten an old tool resultLater blocks fail the prefix check: 400 on accounts created on or after August 31, 2026
Clear content with server-side context editing or compactionValid: on Claude 5.1+, it never invalidates thinking

How to apply it in your project

  1. Append responses as they arriveAdd reply.content to the history unchanged. Never rebuild assistant turns or filter them by block type, and keep empty and redacted_thinking blocks.
  2. Keep the prefix fixedKeep system and tools the same for the whole conversation, and treat messages as append-only. The same habit keeps prompt caching working.
  3. Move trimming to the serverReplace client-side truncation of old tool results with context editing or server-side compaction.
  4. Test the strict behaviorRun your test suite with prefix_mismatch_behavior: "error" and the beta header, on conversations hard enough that Claude actually thinks.
  5. Recover without resendingIf a live conversation hits the signature 400, don't resend the same body. Retry with "drop_block", or strip the named block and every later thinking block, then fix the edit. Dropping blocks can raise output tokens, so treat it as a stopgap.

Key takeaways

  • In a tool loop, the thinking block is part of the response Claude is still writing: send it back with the tool result, unchanged.
  • An empty thinking field isn't an empty block; the signature carries the full reasoning you already paid for.
  • Rebuilding or filtering the latest assistant message returns a 400, whatever your account.
  • On Claude 5.1 and later models, editing the system prompt, tools or earlier messages invalidates later thinking blocks, and newer accounts get a 400.
  • Keep your history append-only and do trimming with server-side context editing or compaction.

📚 المصادر

  1. Thinking in tool and multi-turn workflows — Claude API Docs
  2. Building with thinking — Claude API Docs
  3. Preserved thinking — Claude API Docs
  4. Troubleshooting thinking — Claude API Docs
  5. Context editing — Claude API Docs

التعليقات (0)