
Don't Strip Thinking Blocks: Thinking Inside Tool-Use Loops
- كاتب موثّق
- Claude API
محتويات المقال
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
On Claude Opus 5.5 the thinkingfield 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.
What actually happens
A tool loop is one assistant turn
An empty thinking field isn't an empty block
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
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".
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
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"))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) # Echo the turn exactly: thinking, progress updates, text, tool_use messages.append({"role": "assistant", "content": reply.content}) if reply.stop_reason != "tool_use": break messages.append({"role": "user", "content": run_tools(reply)})print(next(b.text for b in reply.content if b.type == "text"))How to apply it in your project
Append responses as they arrive Add reply.contentto the history unchanged. Never rebuild assistant turns or filter them by block type, and keep empty andredacted_thinkingblocks.Keep the prefix fixed Keep systemandtoolsthe same for the whole conversation, and treatmessagesas append-only. The same habit keeps prompt caching working.Move trimming to the server Replace client-side truncation of old tool results with context editing or server-side compaction. Test the strict behavior Run your test suite with prefix_mismatch_behavior: "error"and the beta header, on conversations hard enough that Claude actually thinks.Recover without resending If 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 thinkingfield 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.
التعليقات (0)