emrg: count request-level tool schemas in usage estimator (issue #1090) - #1091
Merged
Conversation
The usage estimator (_estimate_tokens) counted message content and embedded tool_calls but ignored the request-level `tools` array that the API bills as part of prompt_tokens. Mid-session tool-set growth (skills/dynamic tools loaded between rounds) was therefore invisible to the auto-compact projection, which silently drifts low as more tools are added. Add _estimate_tools(), JSON-char-counting each tool schema +3 overhead (mirroring message metadata). Thread the tool-loop's tools_openai through _estimate_tokens and _refresh_usage_anchor so the anchor and projection reflect exactly what was sent. +5 tests (incl. mid-session tool-growth visibility). Agent.md Python count 1195 -> 1200.
Collaborator
|
I tested this PR against the root cause of issue #1090 (which I filed from the Dev.to 3dom2 / izgorodin finding) and verified the fix end to end:
Non-blocking observations (for the record):
No issues found; the implementation closes the #1090 blind spot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes issue #1090 (community finding by pm25coder via Dev.to 3dom2 / izgorodin).
Root cause:
_estimate_tokens()counted message content and embeddedtool_callsbut ignored the request-leveltoolsarray that the API bills as part ofprompt_tokens. Mid-session tool-set growth (skills/dynamic tools loaded between rounds) was therefore invisible to the auto-compact projection — it silently drifts low as more tools are added.Changes
_estimate_tools(): JSON-char-counting each tool schema +3 per-tool overhead (mirroring how message metadata is counted).tools_openaithrough_estimate_tokensand_refresh_usage_anchorso the anchor and auto-compact projection reflect exactly what was sent.Verification
uv run pytest tests/: 1199 passed + 1 skipped (was 1194+1); import + CLI green.