Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: restore token count display in verbose debug logs#4151

Open
ManthanNimodiya wants to merge 3 commits intobrowser-use:mainfrom
ManthanNimodiya:fix/token-count-display-4150
Open

fix: restore token count display in verbose debug logs#4151
ManthanNimodiya wants to merge 3 commits intobrowser-use:mainfrom
ManthanNimodiya:fix/token-count-display-4150

Conversation

@ManthanNimodiya
Copy link

@ManthanNimodiya ManthanNimodiya commented Feb 23, 2026

Summary

  • _log_history_lines() was entirely commented out and returned '', so no message history was ever logged in debug mode
  • _log_format_message_line() referenced message.metadata.tokens but BaseMessage has no metadata attribute — the old wrapper-object data model it relied on no longer exists
  • The function also called _log_extract_message_content() which was removed in a prior refactor

Restores both functions using the current BaseMessage API (.text property + MessageHistory.get_messages()). Token counts are estimated via the standard ~4 chars/token heuristic; the ~ prefix makes the approximation explicit. Actual billed token counts remain tracked by the TokenCost service post-response as before.

Fixes #4150

Changes

  • service.py: add missing import shutil (was referenced in commented-out code but never imported)
  • _log_format_message_line: replace '??? (TODO)' with f'~{len(content) // 4}'.rjust(5)
  • _log_history_lines: fully restored using get_messages() / m.text; dead references to old m.metadata / m.message / _log_extract_message_content removed

Testing

Enable logging.DEBUG and run any agent task. Verbose logs now show:

📜 LLM Message history (3 messages, ~1842 tokens):
🧠[ ~312]: You are a browser automation agent...
💬[~1420]: Current page: github.com | Task: find open...
🔨[ ~110]: I'll click the search bar first...

Summary by cubic

Restores verbose message history logging and shows approximate token counts (~4 chars/token) in debug output. Fixes #4150.

  • Bug Fixes
    • Re-implemented _log_history_lines using history.get_messages() and BaseMessage.text; respects terminal width.
    • Shows "~" token estimates per message and total; actual billing still via TokenCost.
    • Removed stale metadata/helper refs and added missing shutil import.

Written for commit 4dcf983. Summary will update on new commits.

@CLAassistant
Copy link

CLAassistant commented Feb 23, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

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.

Bug: Token count display permanently broken — shows ??? (TODO) in all verbose logs

2 participants