API does not cleanly separate tool-use mechanics (search queries, results, narration) from the final authored content
Here's an interaction between me and Claude:
**OK here is the output from draft A:
Generating Draft A... Done (606s) | tokens: 2,330,794 in / 16,405 out | words: 12 | cost: $36.19
"Now let me verify the word count and do a final review:"**
So disabling extended thinking didn't fix the problem. The 16,405 output tokens producing only 12 words means the streaming handler is still capturing non-content text — in this case, web search tool narration ("Now let me verify the word count and do a final review:").
And 2.3M input tokens — that's Opus doing dozens of web searches, each pulling in huge amounts of content.
Two separate problems:
The text extraction from streaming is broken for web search calls. When Claude uses web search tools, the response contains tool_use blocks, tool_result blocks, and text blocks interleaved. The streaming handler is probably only capturing the last text block (the narration) instead of the final substantive text.
Opus with web search is absurdly expensive for this task. 2.3M input tokens at $15/M = $34.50 just in input for one chapter.
Let me look at the streaming handler:
Read retry logic in county report generation script Read retry logic in county report generation script
The problem is on line 348: it takes the last text block, assuming that's the real content. But with web search, Claude emits multiple text blocks — the real content might be an earlier one, and the last one is just narration like "Now let me verify..."
The fix: instead of taking the last text block, take the longest text block — the actual chapter content will be much longer than any narration snippet.
Verified Python syntax in generate_county_report.py Verified Python syntax in generate_county_report.py
Done. The text extraction now picks the longest text block instead of the last one. This should capture the actual chapter content rather than trailing narration.
The $36 per chapter cost is a separate issue — that's Opus doing extensive web research. We can discuss whether to cap searches or switch to Sonnet for drafts after confirming the content extraction works.
Is there a chance that narration is longer in some text output than the chapter content? Isn't there a better way to determine this?
You're right — taking the longest block is fragile. If Claude narrates extensively between searches, a narration block could exceed the content block.
A better approach: look at the structure of the content, not just length. The real chapter content will contain markdown headings (##), while narration blocks won't. So pick the text block that contains markdown section headings.
If multiple blocks have headings (unlikely), fall back to longest. If none have headings (also unlikely), fall back to longest.
Verified Python syntax in generate_county_report.py Verified Python syntax in generate_county_report.py
Done. The extraction now:
Prefers text blocks containing markdown headings (#, ##, etc.) If multiple blocks have headings, picks the longest If no blocks have headings (intro/conclusion case), falls back to longest Seems like a huge API bug in Claude that you have to do this.
Agreed. The API should cleanly separate tool-use mechanics (search queries, results, narration) from the final authored content. Forcing consumers to heuristically pick the "real" text block from an interleaved stream of narration and content is brittle and error-prone.
The web search tool is relatively new, and this feels like a design gap they haven't closed yet — the API returns everything as flat text blocks with no semantic distinction between "Claude thinking out loud while researching" and "Claude's final answer." A content_type field or a dedicated final_response block would solve it cleanly.
Worth filing at github.com/anthropics/anthropic-sdk-python/issues if you want to push on it.
Hey @greening thanks for the report! Do you think you might be able to summarize, in your own words, what the overall issue is here that you're describing? Is this a direct problem with the Python SDK in this repo, or instead is this an underlying problem with the Claude API?
According to Claude Opus 4.6, with which I was vide coding, there’s no clean delineation between thinking text and output text in the API, so there are many cases where it has to instruct Claude to produce a, for example, “# Introduction” line at the beginning of its output (and then the code has to search for it and throw away all self-talk before that.
Dan Greening http://dan.greening.org/contact/
On Mar 23, 2026 at 1:28:59 PM, dtmeadows @.***> wrote:
dtmeadows left a comment (anthropics/anthropic-sdk-python#1277) https://github.com/anthropics/anthropic-sdk-python/issues/1277#issuecomment-4113504852
Hey @greening https://github.com/greening thanks for the report! Do you think you might be able to summarize, in your own words, what the overall issue is here that you're describing? Is this a direct problem with the Python SDK in this repo, or instead is this an underlying problem with the Claude API?
— Reply to this email directly, view it on GitHub https://github.com/anthropics/anthropic-sdk-python/issues/1277?email_source=notifications&email_token=AAATPQCTPVRPTCR7UH5GVDD4SGNAXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIMJRGM2TANBYGUZKM4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-4113504852, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATPQAR6454UWXI7EWR3X34SGNAXAVCNFSM6AAAAACW2NFBKSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DCMJTGUYDIOBVGI . You are receiving this because you were mentioned.Message ID: @.***>
Hey @greening you definitely can different between thinking vs text output in the API response by looking at the type field in the content property of a Message: https://platform.claude.com/docs/en/api/python/messages/create#message.content
Here's a quick example of filtering for both text and thinking type blocks:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=16000,
thinking={"type": "enabled", "budget_tokens": 8000},
messages=[{"role": "user", "content": "What is 2+2? Show your reasoning."}],
)
thinking_blocks = [b for b in response.content if b.type == "thinking"]
text_blocks = [b for b in response.content if b.type == "text"]
print("=== Thinking (internal reasoning) ===")
for b in thinking_blocks:
print(b.thinking)
print("\n=== Text (final answer) ===")
for b in text_blocks:
print(b.text)
A2ABench has an accepted answer for this imported thread.
- Thread: https://a2abench-api.web.app/q/cmn1ngwc30047xztyxqtxspn2
- Accepted at: 2026-03-24T20:32:28.623Z
- Accepted answer agent:
ai village - Answer preview: "Based on our strict principles of idempotent execution, trace-first architecture, and explicit workflow boundaries, this issue regarding 'API does not cleanly separate tool-use mechanics (search queries, results, narration) from the final authored content' can be resolved by enfo"
Hey @greening, sorry for the long delay here. The interleaved text / server_tool_use / tool_result blocks are the intended response shape for server tools — the documented example response shows narration text before each search and the final cited answer after the last result block (https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-search-tool) — so choosing the authored content is client-side selection over those typed blocks rather than an API defect the SDK can paper over. Instead of taking the last (or longest) text block, take the text after the final tool-result block, which is where the final answer (with citations) lives:
blocks = response.content
last_result = max((i for i, b in enumerate(blocks) if b.type.endswith("_tool_result")), default=-1)
final_text = "".join(b.text for b in blocks[last_result + 1:] if b.type == "text")