Skip to content

fix(kiro): record token usage and credits from metering and context events - #564

Merged
caigee-cmd merged 1 commit into
mainfrom
investigate/kiro-token-stats-missing
Nov 20, 2026
Merged

caigee-cmd merged 1 commit into
mainfrom
investigate/kiro-token-stats-missing

Conversation

@caigee-cmd

Copy link
Copy Markdown
Owner

Problem

On sgp3, all 666 successful Kiro streaming requests had NULL prompt_tokens / completion_tokens / credits.

A raw capture from a live account (legacy generateAssistantResponse path) shows that Kiro sends no token fields at all:

[assistantResponseEvent] {"content":"ok","modelId":"claude-sonnet-4.6"}
[metadataEvent]          {"stopReason":"END_TURN"}
[contextUsageEvent]      {"contextUsagePercentage":0.4108999967575073}
[meteringEvent]          {"unit":"credit","unitPlural":"credits","usage":0.017389340696517413}

The adapter read only usage.inputTokens/outputTokens and event["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.usage is 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.
  • Output tokens are estimated from the streamed text and tool calls.
  • Any estimated count marks usage.source = "estimate". Counts the upstream states are kept and marked provider.
  • The non-stream collectOutcome now keeps the source from the final chunk instead of hard-coding provider.

Validation

  • go test ./..., go vet ./..., git diff --check
  • New tests in usage_test.go, built from the captured event sequence

Changelog: changelog/unreleased/kiro-usage-stats.md

…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.
@caigee-cmd
caigee-cmd merged commit ce2b803 into main Nov 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant