MoonshotAI / MoonshotAI/kimi-code

bug: max_tokens exceeds provider output limit on first turn in v0.22.3 — main loop still unfixed

Open
#1,377 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.5k
Forks
1.2k
Avg merge
11h 53m
Merged PRs (30d)
350

Description

Bug: max_tokens exceeds provider output limit on first turn in v0.22.3 — main loop still ignores output limits

What version of Kimi Code is running?

0.22.3

Which model were you using?

ollama-cloud/nemotron-3-ultra (also reproducible with ollama-cloud/deepseek-v4-pro, ollama-cloud/...)

What platform is your computer?

Windows 11 x64

What issue are you seeing?

The very first turn fails immediately with:

Error: [provider.api_error] 400 "max_tokens (128000) exceeds model's maximum output tokens (65536) for model nemotron-3-ultra"

The session is completely blocked. No retry, no compaction, no recovery.

Why previous fixes did not resolve it

Multiple releases tried to address related problems, but only in the compaction path:

Release Fix Scope
0.20.2 #1129 "Fix compaction ignoring the configured max output size" Compaction only
0.20.2 #1156 "Cap compaction output at 128k tokens" Compaction only
0.21.0 #1214 Rework conversation compaction Compaction only
0.22.x #1283 Improve compaction handoff summaries Compaction only

None of these affect the main request loop, where the first LLM call is made. That is where this error occurs (turnStep=0.1).

Root cause

For OpenAI-compatible providers, the main loop computes max_tokens from the remaining context window (and possibly max_context_size) instead of clamping to:

  1. The configured max_output_size
  2. The provider's actual output limit

When the remaining context window (128000) is larger than the provider limit (65536), the request fails.

Steps to reproduce
  1. Configure any OpenAI-compatible model where the provider's actual output limit is smaller than the remaining context window:
[providers.ollama-cloud]
type = "openai"
api_key = "..."
base_url = "https://ollama.com/v1"

[models."ollama-cloud/nemotron-3-ultra"]
provider = "ollama-cloud"
model = "nemotron-3-ultra"
max_context_size = 262144
max_output_size = 128000
capabilities = [ "thinking", "tool_use" ]
  1. Set as default and start any session.
  2. Send any prompt → immediate 400.
Expected behavior

Kimi Code should clamp max_tokens to the smaller of:

  • configured max_output_size
  • provider-reported or discovered output limit

On receiving a 400 like this, it should back off max_tokens automatically rather than permanently failing the turn.

Workaround
export KIMI_MODEL_MAX_COMPLETION_TOKENS=65536

This env var acts as a hard cap and is the only reliable fix today.

Related issues
  • #306 — max_output_size not consumed for openai providers (open since v0.7.0)
  • #834 — same bug in compaction path
  • #476 — max_context_size sent as output during compaction
  • #1148 — earlier report of the same main-loop bug
Why this is important

The bug makes Kimi Code unusable with many third-party providers (Ollama Cloud, OpenRouter, etc.) for any model whose actual output limit is below the remaining context window. Users have to discover and set an undocumented environment variable.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the main request loop around the first call (turnStep=0.1) and trace how max_tokens is derived from the remaining context window, max_context_size, and max_output_size. Reproduce with the Ollama Cloud configuration and verify that the request respects the configured or provider output limit instead of failing on the first turn.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.