Skip to content

fix(agent-core-v2): preserve choice-level OpenAI token usage - #5017

Merged
sailist merged 1 commit into
release/2026-11-16from
bug-388-11-17-openai-choice-usage
Nov 17, 2026
Merged

sailist merged 1 commit into
release/2026-11-16from
bug-388-11-17-openai-choice-usage

Conversation

@sailist

@sailist sailist commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Users of OpenAI-compatible providers that return token usage in choices[0].usage can see missing or zero token accounting even when usage was requested. This PR preserves that usage for both streaming responses and non-streaming replay, while keeping standard top-level usage behavior unchanged.

No external issue is linked; this is a reported compatibility bug fix.

Bug Reproduction Steps

  1. Configure an OpenAI-compatible provider whose Chat Completions chunk or response places usage only in the first choice.
  2. Complete a streaming prompt, or replay the equivalent non-streaming response.
  3. Before this fix, the decoder emits no usage event for that response. After this fix, it emits the reported input and output token usage.

Root Cause

The shared OpenAI decoder read only top-level usage. A choice-level compatibility hoist existed only in the Kimi protocol, leaving other OpenAI-compatible providers without the same fallback.

This is a fundamental fix in the shared decoder rather than another provider-specific workaround. Standard top-level usage retains precedence through nullish fallback semantics.

Code Changes

  • Read choices[0].usage when top-level usage is null or absent, and add the corresponding raw chunk/response types.
  • Remove the now-redundant Kimi-only hoist and decoder wrapper so Kimi inherits the shared behavior.
  • Extend the existing OpenAI usage test in place to cover both a streaming chunk and a non-streaming replay with choice-level usage; the test count remains unchanged.
  • Add a patch changeset for the CLI.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Standard top-level usage Parsed and emitted normally Unchanged Existing OpenAI-compatible providers and CLI token accounting Not needed; existing top-level usage assertions still pass
Choice-only usage in streaming chunks Ignored Parsed and emitted OpenAI-compatible providers using this response shape Not applicable; this restores previously lost data
Choice-only usage in non-streaming replay Ignored Parsed and emitted Non-streaming requests and replay consumers Not applicable
Kimi choice-level usage Hoisted by a Kimi-only decoder wrapper Handled by the shared decoder with the same precedence Kimi provider users Behavior is preserved; no escape hatch needed

Affected modules are the OpenAI and Kimi protocol decoders in agent-core-v2. Coverage includes the extended usage test, the full agent-core-v2 suite, package typecheck, and lint on the changed files.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). No external issue; this is an internal bug report.
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update. No request format, configuration, or public API contract changes require documentation.

@changeset-bot

changeset-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 780efb9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T10:35:29.228273Z 780efb9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@780efb9
npx https://pkg.pr.new/@moonshot-ai/kimi-code@780efb9

commit: 780efb9

@sailist
sailist merged commit c15cc1c into release/2026-11-16 Nov 17, 2026
15 checks passed
@sailist
sailist deleted the bug-388-11-17-openai-choice-usage branch November 17, 2026 11:34
@github-actions github-actions Bot mentioned this pull request Nov 17, 2026
wbxl2000 pushed a commit that referenced this pull request Nov 17, 2026
* fix: resume sessions with removed default models (#4982)

* fix(node-sdk): resume sessions with missing default model

* fix(cli): check auth for the resumed session model

* chore: add changeset for unavailable model recovery

* fix(agent-core-v2): stop rebuilding mismatched session indexes (#4980)

* fix(agent-core-v2): reject repeated model responses (#4977)

* fix(agent-core-v2): reject repeated model responses

* chore: add changeset for repeated model responses

---------

Co-authored-by: 7Sageer <[email protected]>

* fix(agent-core-v2): inline deferred tools without dynamic loading (#4971)

* fix(agent-core-v2): record restored task notifications without model requests (#4949) (#4989)

* fix(agent-core-v2): record restored task notifications without model requests

- queue restored task notifications as prompt-only turns after loop initialization
- preserve interrupted and queued turns during resume and fork
- complete reconciliation before initialization and reset return to keep Wire reads ordered
- run loop and container cleanup when reconciliation fails
- surface restored notification persistence failures

* chore: add changeset for restored task notifications

* fix(agent-core-v2): restore overflow recovery after LLM retries (#4950) (#4990)

* fix(agent-core-v2): restore step state after LLM retries

* test(agent-core-v2): scope retry event to overflow regression

* fix(config): allow sessions with invalid user config (#4951) (#4991)

* fix(agent-core-v2): tolerate config read failures for permission rules

* chore: add changeset for permission config recovery

* docs: document permission config recovery

* docs: qualify permission config recovery scope

* fix(config): surface read error diagnostics

* fix(config): allow sessions with invalid user config

* fix(config): preserve valid values and replay diagnostics

* fix(config): remove startup diagnostic replay

* fix(agent-core-v2): ignore non-user prompts during Tower wake (#4952) (#4992)

* fix(agent-core-v2): ignore non-user prompts during Tower wake

* chore: add Tower notification fix changeset

* fix(agent-core-v2): preserve slash prompts during Tower wake

* fix(agent-core-v2): route foreground shell events by origin (#4954) (#4994)

* fix(agent-core-v2): route foreground shell events by origin

* test(agent-core-v2): expect tool event origins in snapshots

* fix(klient): accept tool events from pre-origin hosts

* fix(agent-core-v2): run external Stop hooks after turn completion (#4956) (#4995)

* fix(agent-core-v2): run external Stop hooks after turn completion

* chore: add changeset for external Stop hooks

* docs: clarify post-turn Stop hook timing

* fix(agent-core-v2): wait for subagent quiescence (#4953) (#4993)

* fix(agent-core-v2): reject incomplete model streams (#5006)

* fix(agent-core-v2): preserve choice-level OpenAI token usage (#5017)

* fix(capability): confirm stopping WebBridge before Windows updates (#4999)

* fix(capability): confirm stopping WebBridge before Windows updates

* fix(capability): harden WebBridge update recovery

* fix(capability): restore WebBridge after startup failure

* fix(capability): wait for WebBridge process exit

* fix(capability): handle WebBridge confirmation edge cases

---------

Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>

* chore: sync release 2.6.3 back to main

* chore: finish release 2.6.3 backsync

* chore: sync node-sdk lockfile importer

* fix(sdk): preserve main core type dependencies

* fix: complete release 2.6.3 backsync

---------

Co-authored-by: Haozhe <[email protected]>
Co-authored-by: 7Sageer <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
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.

2 participants