feat: add token savings estimation to tool responses#61
Open
halindrome wants to merge 7 commits intoDeusData:mainfrom
Open
feat: add token savings estimation to tool responses#61halindrome wants to merge 7 commits intoDeusData:mainfrom
halindrome wants to merge 7 commits intoDeusData:mainfrom
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Extract metrics block into searchResultWithMeta to reduce cognitive complexity. Add TestGetCodeSnippet_MetaField to snippet_test.go. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Adds TestSearchGraph_MetaField in internal/tools/search_test.go - Verifies _meta field is present in search_graph response when metricsTracker is attached - Asserts tokens_saved >= 0, baseline_tokens >= 0 (may be 0 for test fixtures), response_tokens > 0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
11 tasks
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
Adds
_metatoken savings estimation metadata to MCP tool responses, inspired by jcodemunch. This gives users visibility into how many tokens (and dollars) are saved by using structured graph queries instead of reading entire source files.internal/metricspackage —TokenMetadatastruct,EstimateTokens(char/4 heuristic),CalculateSavings, andTrackerwith atomic JSON persistence to~/.cache/codebase-memory-mcp/savings.jsonmetrics_enabled(default: true),pricing_model(claude-sonnet/claude-opus/gpt-4o/custom),custom_price_per_token, plusGetFloat64helper onConfigStoreget_code_snippetinstrumented — baseline = full source file size viaos.Stat(the file the user would have read)search_graphinstrumented — baseline = sum of unique source file sizes in results (all files the user would scan to find those symbols)os.Statfails, responses fall back to standardjsonResultwith no_meta(no regression)Example
_metain response{ "_meta": { "tokens_saved": 900, "baseline_tokens": 1000, "response_tokens": 100, "cost_avoided": 0.0135, "compression_ratio": 0.1 } }Test plan
go test ./internal/metrics/... -count=1— 8 tests (calculations, tracker persistence, load existing, missing dir)go test ./internal/tools/... -count=1— includesTestGetCodeSnippet_MetaFieldandTestSearchGraph_MetaFieldgo test ./internal/store/... -count=1— config constants andGetFloat64golangci-lint run ./...— 0 new issues🤖 Generated with Claude Code