Fix token estimation: include ToolUse arguments in text_length

`MessageContent::text_length()` returned 0 for `ToolUse` blocks,
ignoring the tool name and JSON input arguments. This caused the
compactor's `estimate_token_count()` (which uses `text_length()`)
to massively undercount tokens when conversations contained tool
calls with large arguments (e.g. web_search results, page content).

The result: compaction never triggered despite the session exceeding
the context window, leading to "Token limit exceeded" errors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Philippe Branchu
2026-03-27 23:15:57 +00:00
co-authored by Claude Opus 4.6
parent 64631a31e6
commit 55395c80db
+4 -2
View File
@@ -142,8 +142,10 @@ impl MessageContent {
ContentBlock::Text { text, .. } => text.len(),
ContentBlock::ToolResult { content, .. } => content.len(),
ContentBlock::Thinking { thinking } => thinking.len(),
ContentBlock::ToolUse { .. }
| ContentBlock::Image { .. }
ContentBlock::ToolUse { name, input, .. } => {
name.len() + input.to_string().len()
}
ContentBlock::Image { .. }
| ContentBlock::Unknown => 0,
})
.sum(),