Skip to content

[Kiro] Event stream already carries per-request credit metering — adapter drops meteringEvent #5948

Description

@Smartnewb

Area: Provider adapters

What are you trying to accomplish?

Accurate per-request cost accounting for the kiro provider. Every Kiro request row in usage.jsonl currently records estimated: true token counts and cacheProvenance: "unknown", so there is no real usage/cost signal for Kiro traffic at all — not even the one the upstream already sends.

What prevents this today?

The Kiro event stream already emits a meteringEvent frame carrying the actual credits charged for the request, but KNOWN_EVENT_TYPES in src/adapters/kiro-events.ts does not include it, so parseKiroEvent() returns null and the frame is dropped silently — ocx debug provider on does not log it either.

Live capture (fresh free-tier Builder ID account, opencodex 2.65.0, raw decodeEventStream probe against runtime.us-east-1.kiro.dev):

glm-5:

#1 :event-type=initial-response        {"conversationId": "..."}      <- dropped
#2 :event-type=assistantResponseEvent  {"content": "...", "modelId"}
#3 :event-type=metadataEvent           {"stopReason": "END_TURN"}      <- no tokenUsage
#4 :event-type=contextUsageEvent       {"contextUsagePercentage": 1.94}
#5 :event-type=meteringEvent           {"unit": "credit", "unitPlural": "credits",
                                        "usage": 0.04582331509121062}  <- dropped

claude-sonnet-4.5 (same account/endpoint):

#1 initial-response, #2 assistantResponseEvent, #3 metadataEvent {stopReason},
#4 contextUsageEvent {contextUsagePercentage: 2.03},
#5 meteringEvent {"unit":"credit","usage":0.009624912835820896}

q.us-east-1.amazonaws.com/generateAssistantResponse emits the same meteringEvent shape.

Two facts worth stating plainly:

  • metadataEvent carried no tokenUsage in any observed frame, on either model, on either endpoint — the token-count absence is real upstream behavior, not an adapter miss.
  • The meteringEvent credit value is present every request and is currently invisible to opencodex.

What should OpenCodex do?

  1. Recognize meteringEvent (and defensively usageEvent/metricsEvent/tokenMetrics wrappers) and record the reported credit amount on the request log/usage row — e.g. a creditUsage field alongside OcxUsage, so usage.jsonl carries real spend instead of only heuristic tokens.
  2. Log dropped/unknown :event-type names via debugProviderDiagnostic when provider debug is on — today they leave no trace even in debug mode, which hides exactly this class of upstream data.
  3. Optionally: initial-response also carries conversationId and is dropped; when messageMetadataEvent is absent (it was on both captures above), the conversation id is lost.

Example

Current row:

"usage": {"inputTokens": 8, "outputTokens": 1, "contextTotalTokens": 3701, "estimated": true},
"cacheProvenance": "unknown"

Desired (exact field naming up to you):

"usage": { ..., "estimated": true },
"creditUsage": {"unit": "credit", "amount": 0.04582331509121062}

Alternatives or workarounds

The heuristic estimator (devlog 142/160) is the right answer for input_tokens/output_tokens display and Codex auto-compact — keep it. This proposal only adds the billing signal next to it. Another implementation of the same subscription surface, 0a00/Kiro-Go-Plus (proxy/eventstream_payload.go, proxy/kiro.go), already classifies meteringEvent/usageEvent/metricsEvent frames and recursively hunts nested usage envelopes, including Anthropic-style cacheReadInputTokens/cacheCreation splits when present — useful reference for the priority order.

Additional context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions