Skip to content

fix(kosong): clamp uncached OpenAI input usage#2160

Open
morluto wants to merge 1 commit into
MoonshotAI:mainfrom
morluto:codex/fix-negative-input-other
Open

fix(kosong): clamp uncached OpenAI input usage#2160
morluto wants to merge 1 commit into
MoonshotAI:mainfrom
morluto:codex/fix-negative-input-other

Conversation

@morluto

@morluto morluto commented Jul 24, 2026

Copy link
Copy Markdown

Related Issue

No linked issue. This is a focused fix for a reproducible OpenAI-compatible usage edge case.

Problem

When a compatible endpoint reports cached-token details but omits the aggregate prompt/input token count, both OpenAI adapters default the aggregate to zero and then subtract the cache count. That produces a negative inputOther, which cancels the reported cache usage when total input is calculated.

Usage projection Before After
Reported aggregate input 0 0
Reported cache read 10 10
Derived uncached input -10 0
Calculated total input 0 10

Official OpenAI usage objects include the aggregate token fields, so this primarily hardens compatibility with partial responses from OpenAI-compatible endpoints.

What changed

  • Clamp derived uncached input tokens at zero for Chat Completions and Responses usage parsing.
  • Apply the same correction to the legacy and v2 provider implementations.
  • Add regressions for cache details reported without aggregate prompt/input totals.
  • Preserve the provider-reported cache and output counts unchanged.

Test plan

  • pnpm vitest run packages/kosong/test/kimi.test.ts packages/kosong/test/openai-responses.test.ts packages/agent-core-v2/test/kosong/provider/composition.test.ts (239 passed)
  • pnpm vitest run --project kosong (1,336 passed)
  • pnpm vitest run packages/agent-core-v2/test/agent/mcp/output.test.ts (39 passed)
  • pnpm --filter @moonshot-ai/kosong typecheck
  • pnpm --filter @moonshot-ai/agent-core-v2 typecheck
  • pnpm --filter @moonshot-ai/agent-core-v2 lint:domain
  • pnpm --filter @moonshot-ai/kosong build
  • pnpm --filter @moonshot-ai/agent-core-v2 build

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fe59b40

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

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