From 55395c80dbdabb309fca55f17f790c66e42f479f Mon Sep 17 00:00:00 2001 From: Philippe Branchu Date: Fri, 27 Mar 2026 23:15:57 +0000 Subject: [PATCH] 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) --- crates/openfang-types/src/message.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/openfang-types/src/message.rs b/crates/openfang-types/src/message.rs index d99a69c6..7216852c 100644 --- a/crates/openfang-types/src/message.rs +++ b/crates/openfang-types/src/message.rs @@ -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(),