Repository navigation
fix(kiro): record token usage and credits from metering and context events - #564
Merged
Merged
Conversation
…vents generateAssistantResponse sends no token counts. It sends contextUsageEvent (share of the input window) and meteringEvent (credits). Parse both: credits come from meteringEvent.usage, input tokens from the context percentage times the model window (1M for Claude 4.6+, 200K otherwise), and output tokens are estimated from the produced text. Estimated counts are logged as 'estimate'. The non-stream path now keeps the usage source from the final chunk.
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
On sgp3, all 666 successful Kiro streaming requests had NULL
prompt_tokens/completion_tokens/credits.A raw capture from a live account (legacy
generateAssistantResponsepath) shows that Kiro sends no token fields at all:The adapter read only
usage.inputTokens/outputTokensandevent["credits"]. All three values came out as 0, so no usage chunk was written and the request log stored NULL.Fix (internal/providers/kiro)
meteringEvent.usageis added up into credits. A stated credit total (CreateResponse) wins, so the two are never added together.contextUsageEvent.contextUsagePercentage× model input window (1M for Claude ≥ 4.6, 200K for ≤ 4.5) gives input tokens. This follows Kiro-Go.usage.source = "estimate". Counts the upstream states are kept and markedprovider.collectOutcomenow keeps the source from the final chunk instead of hard-codingprovider.Validation
go test ./...,go vet ./...,git diff --checkusage_test.go, built from the captured event sequenceChangelog:
changelog/unreleased/kiro-usage-stats.md