feat: optimize tool responses to reduce token consumption#71
Open
AdamGustavsson wants to merge 5 commits intogoogleanalytics:mainfrom
Open
feat: optimize tool responses to reduce token consumption#71AdamGustavsson wants to merge 5 commits intogoogleanalytics:mainfrom
AdamGustavsson wants to merge 5 commits intogoogleanalytics:mainfrom
Conversation
MCP tool responses are directly consumed by LLMs, making token count a critical
cost factor. This commit significantly reduces token usage while maintaining
full functionality.
Token Optimization Strategy:
- Eliminate repetition in array responses (property_type, parent fields)
- Use compact row format with simple arrays vs wrapped objects
- Conditionally include fields only when populated (metadata, totals, etc.)
- Strip redundant parent resource names
- Add optional include_descriptions parameter (default: false)
Response Format Changes:
- get_account_summaries: Return compact format with simple IDs
Savings: ~40% (eliminates repeated property_type/parent for each property)
- run_report/run_realtime_report: Compact rows, conditional field inclusion
Savings: ~30-50% (less wrapper objects, no empty fields)
- get_custom_dimensions_and_metrics: Cleaner field names, optional descriptions
Savings: ~25% (descriptions excluded by default)
Schema Simplification:
- Change property_id parameters to accept only numeric strings (e.g. '213025502')
- Remove support for full resource names ('properties/12345')
- This creates consistency: tools return IDs that other tools accept
- Update construct_property_rn() to enforce numeric string format only
Additional Improvements:
- Add default limit=100 to run_report and run_realtime_report
Prevents accidentally requesting massive responses
- Add automatic quota warning when API usage exceeds 90%
Helps prevent hitting quota limits unexpectedly
- Fix bug: Remove offset parameter from run_realtime_report
The Realtime API doesn't support pagination via offset. Attempting to use
offset results in 'Unknown field for RunRealtimeReportRequest: offset' error.
Tests:
- Add comprehensive quota warning tests (6 test cases)
- Update construct_property_rn validation tests
- All tests passing
Breaking Changes:
- property_id parameters now require numeric strings only
- Response formats are more compact (but contain same data)
- Custom dimensions/metrics return different field names (api_name vs apiName)
- run_realtime_report no longer accepts offset parameter
Member
|
Thanks for the pull request! Could you run |
Contributor
Author
|
Sorry, I haven´t used this type of formatters before. I hope it will pass now. |
Member
|
@AdamGustavsson , apologies for the delay on this review. I haven't forgotten about it and hope to get to it soon! |
Member
|
@AdamGustavsson I'm taking over this review. If you can resolve the conflicts, I'll review and get it merged. |
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.
MCP tool responses are directly consumed by LLMs, making token count a critical factor. This commit significantly reduces token usage while maintaining full functionality.
Token Optimization Strategy:
Response Format Changes:
Schema Simplification:
Additional Improvements:
Tests:
Breaking Changes:
Fixes #69 and similar issues for other tool calls