MoonshotAI / MoonshotAI/kimi-code
feat: emit token usage events in stdout stream-json output
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 7.5k
- Forks
- 1.2k
- Avg merge
- 11h 53m
- Merged PRs (30d)
- 350
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/cli/prompt-render.ts at PromptJsonWriter and trace how turn.step.completed events are handled in the print path. Compare those events with usage.record entries in the session wire.jsonl log, then verify that stream-json stdout emits a turn.usage JSON line with the requested token buckets per turn or at session end.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100