Skip to content

Restore Kimi tool calling and enforce strict thinking behavior#5722

Open
Neonsy wants to merge 3 commits intoKilo-Org:mainfrom
Neonsy:fix/moonshot-kimi-k2-5
Open

Restore Kimi tool calling and enforce strict thinking behavior#5722
Neonsy wants to merge 3 commits intoKilo-Org:mainfrom
Neonsy:fix/moonshot-kimi-k2-5

Conversation

@Neonsy
Copy link
Contributor

@Neonsy Neonsy commented Feb 6, 2026

Here we go again

I would appreciate proper validation this time around 😄

Context

This PR fixes the Moonshot/Kimi regression in #5719.

After the AI-SDK migration, users on Moonshot (kimi-k2.5 and kimi-for-coding) could hit a tool-calling loop with MODEL_NO_TOOLS_USED. In practice, Kilo would often output literal <read_file>-style text instead of executing native tool calls.

We made this fix to re-align our Moonshot/Kimi behavior with Moonshot API expectations and restore reliable native tool execution, without rolling back the migration or changing behavior for unrelated providers.

Root causes addressed:

  • kimi-k2.5 metadata no longer clearly advertised native tool support.
  • Moonshot can stream tool arguments via tool-input-* events, but we did not always convert those into executable tool_call chunks.
  • Strict Kimi thinking contract (binary thinking + fixed temperatures) was not enforced consistently.
  • Reasoning continuity (reasoning_content) needed to be preserved across AI-SDK message conversion.

Why we did not do a Moonshot-only adapter fix

We considered patching only MoonshotHandler, but the primary break happened in the shared AI-SDK/OpenAI-compatible stream layer (tool-input-* handling), not only in Moonshot-specific policy code.

A Moonshot-only patch would have:

  • duplicated stream-conversion logic in a provider adapter, or
  • introduced fragile provider-specific special casing in the wrong layer.

Instead, this PR:

  • fixes message/tool stream conversion in the shared OpenAI-compatible base handler (where the regression actually lives),
  • keeps Moonshot-specific behavior (thinking toggle + strict temperature policy) in MoonshotHandler.

This keeps architecture clean, reduces duplication, and lowers long-term merge/regression risk.

Implementation

1) Restore strict Kimi model capabilities

Updated packages/types/src/providers/moonshot.ts for kimi-k2.5 and kimi-for-coding:

  • native tools enabled (supportsNativeTools: true, defaultToolProtocol: "native")
  • reasoning preservation enabled (preserveReasoning: true)
  • binary/adaptive reasoning metadata enabled
  • temperature control disabled for UI/runtime (supportsTemperature: false, defaultTemperature: 0.6)

Why: ensure runtime and UI choose the correct native tool protocol and strict model behavior.

2) Convert Moonshot tool-input-* streams into executable tool calls

In src/api/providers/openai-compatible.ts:

  • added accumulation for tool-input-start + tool-input-delta
  • emit final tool_call chunk on tool-input-end
  • flush pending tool input at stream end
  • prevent duplicate emission when direct tool-call is also present

Why: Moonshot may stream tool input incrementally; Task execution requires finalized tool_call chunks.

3) Enforce strict Kimi thinking contract in Moonshot handler

In src/api/providers/moonshot.ts, for strict Kimi models only:

  • thinking enabled (enableReasoningEffort !== false) => force temperature = 1.0
  • thinking disabled => force temperature = 0.6
  • ignore arbitrary custom modelTemperature for these models
  • always send Moonshot provider thinking option (enabled/disabled)

Why: keep requests compliant with strict Kimi expectations and avoid invalid mixed settings.

4) Preserve reasoning continuity in AI-SDK conversion

In src/api/transform/ai-sdk.ts:

  • preserve user text/tool-result ordering
  • preserve assistant text/tool-call ordering
  • map assistant reasoning blocks to providerOptions.openaiCompatible.reasoning_content

Why: avoid reasoning context loss across turns in the OpenAI-compatible path.

5) UI alignment for binary reasoning models

In webview-ui/src/components/settings/ThinkingBudget.tsx:

  • binary reasoning checkbox now defaults to enabled when unset (enableReasoningEffort ?? true)

Why: UI default now matches backend behavior for strict Kimi models.

6) Regression coverage

Added/updated tests:

  • src/api/providers/__tests__/moonshot.spec.ts
  • src/api/transform/__tests__/ai-sdk.spec.ts
  • src/api/transform/__tests__/model-params.spec.ts
  • webview-ui/src/components/settings/__tests__/ThinkingBudget.spec.tsx

Added changeset:

  • .changeset/green-geese-warn.md

Screenshots

image

How to Test

  1. Run targeted regression tests:
  • cd src && pnpm test api/providers/__tests__/moonshot.spec.ts api/transform/__tests__/ai-sdk.spec.ts api/transform/__tests__/model-params.spec.ts
  • cd webview-ui && pnpm test src/components/settings/__tests__/ThinkingBudget.spec.tsx
  1. Manual provider check:
  • Provider: moonshot
  • Base URL: https://api.kimi.com/coding/v1
  • Model: kimi-k2.5
  • Ask a tool-heavy prompt (for example: read/summarize files)
  • Confirm native tool calls execute and it no longer loops with MODEL_NO_TOOLS_USED
  1. Repeat manual check for:
  • Model: kimi-for-coding
  1. Verify strict thinking behavior:
  • Thinking ON => request uses temperature 1.0 and Moonshot thinking enabled
  • Thinking OFF => request uses temperature 0.6 and Moonshot thinking disabled

Caution

This PR relied heavily on AI assistance. I had already investigated this area in a previous PR attempt and used AI to accelerate implementation while keeping scope tight.

@Neonsy
Copy link
Contributor Author

Neonsy commented Feb 6, 2026

Who in the (insert confusion here)

I'm pretty sure when I made the PR to add Kimi K2.5, image was turned true.

Why is it false now...

I love it when code gets changed without oversight 🙂

image

@changeset-bot
Copy link

changeset-bot bot commented Feb 6, 2026

🦋 Changeset detected

Latest commit: 78dbcfa

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

This PR includes changesets to release 1 package
Name Type
kilo-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

@Neonsy Neonsy marked this pull request as ready for review February 6, 2026 15:20
@Neonsy
Copy link
Contributor Author

Neonsy commented Feb 6, 2026

I would love feedback and testing

As stated in the PR body, Kimi K2.5 thinking is confirmed with the coding plan endpoint (kimi for coding should work the same), but IDK about the "normal" API endpoints.

@smetanokr
@CaiDingxian
@mikij
@kevinvandijk

@CaiDingxian
Copy link
Contributor

CaiDingxian commented Feb 6, 2026

Thank you for your dedication, we really need Kilo's support for Kimi K2.5, I'm glad you've rekindled your spirit.
Tomorrow I'll update this branch, run long-range tests on actual projects, and report back.

Meanwhile, I hope to get help from @MoonshotAl official @RealKai42

@Neonsy
Copy link
Contributor Author

Neonsy commented Feb 6, 2026

Thank you for your dedication, we really need Kilo's support for Kimi K2.5, I'm glad you've rekindled your spirit. Tomorrow I'll update this branch, run long-range tests on actual projects, and report back.

Meanwhile, I hope to get help from @MoonshotAl official @RealKai42

@CaiDingxian I wouldn't say it's rekindled (or needed to be), I just knew I could help fast enough and didn't want to let everyone who is still suffering down.

It's just that my priority has changed to trying to create a desktop experience, powered by Kilo with the same foundational spirit as the extension.

@CoinAnole
Copy link

Tested this with the Moonshot API Provider (api.kimi.com/coding/v1 as entrypoint) and the Kilo Gateway for Kimi K2.5 and they both worked.

@Neonsy
Copy link
Contributor Author

Neonsy commented Feb 6, 2026

Tested this with the Moonshot API Provider (api.kimi.com/coding/v1 as entrypoint) and the Kilo Gateway for Kimi K2.5 and they both worked.

I need someone to test the classic API's (either one is fine)

Don't have a key for that.

@CoinAnole
Copy link

"classic API's" means api.moonshot.ai and api.moonshot.cn?

@Neonsy
Copy link
Contributor Author

Neonsy commented Feb 6, 2026

"classic API's" means api.moonshot.ai and api.moonshot.cn?

Yes

Testing one of them should be enough.

@Neonsy Neonsy force-pushed the fix/moonshot-kimi-k2-5 branch from 0600435 to 197f2f8 Compare February 6, 2026 20:30
@CoinAnole
Copy link

Rebuilt the extension with your most recent changes.

api.moonshot.ai works with kimi-k2.5 and kimi-k2-thinking but not kimi-for-coding. I have not tested any other models.

(kimi-for-coding works fine with api.kimi.com/coding/v1 though )

Date/time: 2026-02-07T00:52:36.837Z
Extension version: 5.4.1
Provider: moonshot
Model: kimi-for-coding

Provider ended the request: No output generated. Check the stream for errors.

Date/time: 2026-02-07T00:52:36.839Z
Extension version: 5.4.1
Provider: moonshot
Model: kimi-for-coding

No output generated. Check the stream for errors.

@CaiDingxian
Copy link
Contributor

Rebuilt the extension with your most recent changes.

api.moonshot.ai works with kimi-k2.5 and kimi-k2-thinking but not kimi-for-coding. I have not tested any other models.

(kimi-for-coding works fine with api.kimi.com/coding/v1 though )

Date/time: 2026-02-07T00:52:36.837Z
Extension version: 5.4.1
Provider: moonshot
Model: kimi-for-coding

Provider ended the request: No output generated. Check the stream for errors.

Date/time: 2026-02-07T00:52:36.839Z
Extension version: 5.4.1
Provider: moonshot
Model: kimi-for-coding

No output generated. Check the stream for errors.

This is a normal performance, kimi-for-coding only supports coding endpoint.

@Neonsy
Copy link
Contributor Author

Neonsy commented Feb 7, 2026

This is a normal performance, kimi-for-coding only supports coding endpoint.

I will make it so you can't select it with the other endpoints.

The get API key button now changes URL for the coding endpoint.

@Neonsy Neonsy marked this pull request as draft February 7, 2026 01:21
@Neonsy Neonsy force-pushed the fix/moonshot-kimi-k2-5 branch from d1044cd to 191b0aa Compare February 7, 2026 02:04
@Neonsy Neonsy marked this pull request as ready for review February 7, 2026 02:04
@CaiDingxian
Copy link
Contributor

I have tested the Kimi-K2.5 model from api.moonshot.cn using this pr code in my actual project, and it performed excellently with good stability. k2.5(thinking mode) even solved problems that GPT-5.3-Codex couldn't handle, which I find impressive.
@Neonsy nice!

…tract

- restore native tool metadata for kimi-k2.5 and kimi-for-coding
- map AI SDK tool-input-* streams into executable tool_call chunks
- enforce strict Kimi thinking behavior (thinking=1.0, non-thinking=0.6) and send moonshot thinking options
- preserve assistant reasoning_content in AI-SDK message conversion
- add regression tests for moonshot provider, ai-sdk transform, model params, and ThinkingBudget UI
@Neonsy Neonsy force-pushed the fix/moonshot-kimi-k2-5 branch from 191b0aa to 78dbcfa Compare February 9, 2026 06:17
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.

3 participants