Skip to content

feat: emit token usage events in stdout stream-json output #3080

Description

@dommaker

When running kimi with stream-json (print) output, there is currently no way to obtain token usage from stdout. The PromptJsonWriter (src/cli/prompt-render.ts) only writes 5 JSON line types (system.version, session.resume_hint, turn.step.retrying, assistant, tool), and turn.step.completed events carrying usage are explicitly dropped in the print path. Verified on 0.37.2 — the word "usage" does not appear anywhere in the print/prompt-render code.

Usage data does exist internally: usage.record durable events are written to the session wire log (~/.kimi-code/sessions/<wd-slug>/<session_id>/agents/main/wire.jsonl) with full input/output/cache-read/cache-creation buckets. But consumers driving kimi headlessly (CI pipelines, orchestration platforms wrapping the CLI) must locate and tail this out-of-band file to get usage — fragile and racy while the session is still writing.

Request: emit a usage JSON line on stdout in stream-json mode, e.g. {"role":"meta","type":"turn.usage","usage":{"inputTokens":...,"outputTokens":...,"cacheReadTokens":...,"cacheCreationTokens":...}}, either per-turn or at session end. claude-code already does this (result event with usage), and it makes headless token accounting trivial.

Use case: we run kimi/opencode/codex/claude through a unified ledger and reconcile reported tokens per provider; kimi is the only one with no stdout usage outlet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions