Add token metrics to DummyOptimizer find_tool responses#3821
Open
aponcedeleonch wants to merge 1 commit intomainfrom
Open
Add token metrics to DummyOptimizer find_tool responses#3821aponcedeleonch wants to merge 1 commit intomainfrom
aponcedeleonch wants to merge 1 commit intomainfrom
Conversation
Add TokenCounter abstraction and wire it into DummyOptimizer so that find_tool responses include TokenMetrics (baseline, returned, savings%). Token counts are precomputed per-tool at optimizer construction time and the baseline total is cached as an immutable field, avoiding redundant summation on every FindTool call. The computeTokenMetrics helper operates entirely on optimizer-local state—no store involvement needed. The TokenCounter interface lives in the optimizer package (not the internal types package) since only the optimizer layer uses it. CharDivTokenCounter provides the default heuristic (JSON byte length / 4). Factory functions (NewDummyOptimizerFactory, NewDummyOptimizerFactoryWithStore) accept or create a TokenCounter and pass it through to NewDummyOptimizer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3821 +/- ##
==========================================
+ Coverage 66.84% 66.85% +0.01%
==========================================
Files 439 440 +1
Lines 43509 43538 +29
==========================================
+ Hits 29083 29108 +25
- Misses 12175 12178 +3
- Partials 2251 2252 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
jerm-dro
requested changes
Feb 13, 2026
Contributor
jerm-dro
left a comment
There was a problem hiding this comment.
LGTM but let's simplify the tests
| tools map[string]server.ServerTool | ||
|
|
||
| // tokenCounts holds precomputed per-tool token estimates, indexed by tool name. | ||
| tokenCounts map[string]int |
Contributor
There was a problem hiding this comment.
question, non-blocking: is this also immutable? Add a comment to explain.
Comment on lines
46
to
60
| tests := []struct { | ||
| name string | ||
| tools []server.ServerTool | ||
| searchFunc func(ctx context.Context, query string, allowedTools []string) ([]ToolMatch, error) | ||
| upsertFunc func(ctx context.Context, tools []server.ServerTool) error | ||
| input FindToolInput | ||
| expectedNames []string | ||
| expectErr bool | ||
| errContains string | ||
| expectCreate bool // if false, expect NewDummyOptimizer to fail | ||
| createErr string | ||
| name string | ||
| tools []server.ServerTool | ||
| searchFunc func(ctx context.Context, query string, allowedTools []string) ([]ToolMatch, error) | ||
| upsertFunc func(ctx context.Context, tools []server.ServerTool) error | ||
| input FindToolInput | ||
| expectedNames []string | ||
| expectErr bool | ||
| errContains string | ||
| expectCreate bool // if false, expect NewDummyOptimizer to fail | ||
| createErr string | ||
| checkMetrics bool | ||
| expectBaseline int | ||
| expectReturned int | ||
| }{ |
Contributor
There was a problem hiding this comment.
Blocker: This table is getting quite complex 😵
Can we break it up into more focused tests so it's easier to read?
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.
Closes: #3820
Add TokenCounter abstraction and wire it into DummyOptimizer so that find_tool responses include TokenMetrics (baseline, returned, savings%).
Token counts are precomputed per-tool at optimizer construction time and the baseline total is cached as an immutable field, avoiding redundant summation on every FindTool call. The computeTokenMetrics helper operates entirely on optimizer-local state—no store involvement needed.
The TokenCounter interface lives in the optimizer package (not the internal types package) since only the optimizer layer uses it. CharDivTokenCounter provides the default heuristic (JSON byte length / 4).
Factory functions (NewDummyOptimizerFactory, NewDummyOptimizerFactoryWithStore) accept or create a TokenCounter and pass it through to NewDummyOptimizer.