fix: use full Kiro payload for prompt token counting#102
Open
juslintek wants to merge 1 commit intojwadow:mainfrom
Open
fix: use full Kiro payload for prompt token counting#102juslintek wants to merge 1 commit intojwadow:mainfrom
juslintek wants to merge 1 commit intojwadow:mainfrom
Conversation
The prompt_tokens reported to clients (used by Claude Code's /context command) were wildly inaccurate because they were derived from Kiro's contextUsagePercentage, which returns unreliable values. Instead, count tokens from the complete serialized Kiro request payload using tiktoken. This includes system prompt, messages, tools, and all other payload fields — matching what actually gets sent to the API. - Replace request_messages/request_tools params with pre-counted prompt_tokens across all streaming functions - Count tokens from full kiro_request_body in both OpenAI and Anthropic route handlers - Remove dependency on contextUsagePercentage for token counting - Update tests to match new function signatures
| Thanks for the PR! 🎉 Before merge, we need a one-time CLA confirmation. Full CLA text: Please reply once with: You need to write once, all further messages from me can be ignored. |
This was referenced Mar 20, 2026
Author
| I have read the CLA and I accept its terms |
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.
Problem
The
prompt_tokensreported to clients (used by Claude Code's/contextcommand) were wildly inaccurate because they were derived from Kiro'scontextUsagePercentage, which returns unreliable values.Solution
Count tokens from the complete serialized Kiro request payload using tiktoken. This includes system prompt, messages, tools, and all other payload fields — matching what actually gets sent to the API.
Changes
request_messages/request_toolsparams with pre-countedprompt_tokensacross all streaming functionskiro_request_bodyin both OpenAI and Anthropic route handlerscontextUsagePercentagefor token counting