Skip to content

Fix token estimation: include ToolUse arguments in text_length#881

Merged
jaberjaber23 merged 4 commits intoRightNow-AI:mainfrom
pbranchu:fix/token-estimation-tooluse
Mar 30, 2026
Merged

Fix token estimation: include ToolUse arguments in text_length#881
jaberjaber23 merged 4 commits intoRightNow-AI:mainfrom
pbranchu:fix/token-estimation-tooluse

Conversation

@pbranchu
Copy link
Copy Markdown
Contributor

Summary

  • MessageContent::text_length() returned 0 for ContentBlock::ToolUse blocks, completely ignoring the tool name and JSON input arguments
  • The compactor's estimate_token_count() relies on text_length(), so sessions with large tool call arguments (e.g. web_search, web_fetch) were massively undercounted
  • This caused compaction to never trigger despite the session far exceeding the context window, resulting in Token limit exceeded errors at the LLM provider

Fix

Include name.len() + input.to_string().len() for ToolUse blocks, matching the existing pattern for ToolResult and Text blocks.

Test plan

  • Verify text_length() returns non-zero for messages containing ToolUse blocks
  • Confirm compaction triggers correctly for sessions with heavy tool use
  • cargo test -p openfang-types

🤖 Generated with Claude Code

Philippe Branchu and others added 4 commits March 27, 2026 23:15
`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>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jaberjaber23 jaberjaber23 merged commit 0b59205 into RightNow-AI:main Mar 30, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants