Skip to content

Use buffered token count for tool output tracking#2822

Open
onmete wants to merge 1 commit intoopenshift:mainfrom
onmete:fix/consistent-tool-token-accounting
Open

Use buffered token count for tool output tracking#2822
onmete wants to merge 1 commit intoopenshift:mainfrom
onmete:fix/consistent-tool-token-accounting

Conversation

@onmete
Copy link
Contributor

@onmete onmete commented Mar 17, 2026

Description

Tool output tokens were counted with len(content_tokens) (raw token count) while tool definitions and AIMessage tokens were counted with TokenHandler._get_token_count(), which applies a 10% TOKEN_BUFFER_WEIGHT. This inconsistency caused tool outputs to be slightly under-counted (~10%), making the remaining budget appear slightly larger than it actually was.

Fix: apply _get_token_count() for tool output token tracking so all budget items use the same accounting:

# before
tool_tokens_used += len(content_tokens)

# after
tool_tokens_used += TokenHandler._get_token_count(content_tokens)

This is a small but correctness-oriented change that makes the budget uniform.

Type of change

  • Bug fix

Related Tickets & Documents

  • Related Issue #

Checklist before requesting a review

  • I have performed a self-review of my code.
  • PR has passed all pre-merge test jobs.
  • If it is a core feature, I have added thorough tests.

Testing

  • Added test_tool_output_token_tracking_uses_buffer_weight which spies on TokenHandler._get_token_count and asserts it is called at least 3 times during a single tool call round (tool definitions + AIMessage + tool output), confirming the buffered path is taken for tool outputs.
  • All 885 unit tests pass (make test-unit).
  • make verify passes (black, ruff, pylint 10/10, mypy clean).

Made with Cursor

Tool output tokens were counted with raw len(tokens) while tool
definitions and AIMessage tokens used _get_token_count() which
applies a 10% TOKEN_BUFFER_WEIGHT. This inconsistency caused tool
outputs to be slightly under-counted, making the remaining budget
appear larger than it actually was.

Fix: use TokenHandler._get_token_count() for tool output tokens so
all budget items are accounted for uniformly.

Made-with: Cursor
@openshift-ci
Copy link

openshift-ci bot commented Mar 17, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign bparees for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci
Copy link

openshift-ci bot commented Mar 17, 2026

@onmete: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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.

1 participant