MoonshotAI / MoonshotAI/kimi-code
bug: max_tokens not clamped to max_output_size for OpenAI-compatible providers — 400 on models with output limit < context window (v0.20.1)
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 not clamped to max_output_size for OpenAI-compatible providers — causes 400 on models with output limits lower than context window
What version of Kimi Code is running?
0.20.1
Which open platform/subscription were you using?
Self-configured third-party provider (Ollama Cloud — OpenAI-compatible API)
Which model were you using?
ollama-cloud/deepseek-v4-pro
What platform is your computer?
Windows 11 x64
What issue are you seeing?
The LLM request fails immediately with a 400 error on the very first turn:
400 "max_tokens (131072) exceeds model's maximum output tokens (65536) for model deepseek-v4-pro"
The session is completely blocked — no retry can recover because the max_tokens value is computed incorrectly every time.
What steps can reproduce the bug?
- Configure a model via an OpenAI-compatible provider where the model's actual max output tokens (enforced server-side by the API) is less than the remaining context window:
[providers.ollama-cloud]
type = "openai"
api_key = "..."
base_url = "https://ollama.com/v1"
[models."ollama-cloud/deepseek-v4-pro"]
provider = "ollama-cloud"
model = "deepseek-v4-pro"
max_context_size = 1048576
max_output_size = 1048576
capabilities = [ "thinking", "tool_use" ]
- Set this model as default:
default_model = "ollama-cloud/deepseek-v4-pro" - Start a new session and send any prompt.
- The request fails with 400 because Kimi Code sends
max_tokens=131072while the API enforces a hard limit of 65536 for this model.
Root Cause Analysis
This is the same underlying bug tracked in #306 and #834, persisting through v0.20.1 despite the partial fix in #1131.
Factor 1: max_output_size is ignored for openai providers (#306)
In provider-manager.ts, maxOutputSize is passed to anthropic providers as defaultMaxTokens, but not to openai providers. So the configured max_output_size has zero effect on the max_tokens sent to the API.
Factor 2: The #1131 fix caps to remaining context window, not to max_output_size
The fix in v0.20.1 (f1c8175) caps completion tokens to the remaining context window. However, when max_output_size is not threaded through (Factor 1), the cap falls back to max_context_tokens (1,048,576 in this case). The remaining context window (131,072) is still larger than the API's actual output limit (65,536).
The result: max_tokens = min(remaining_context, max_context_size) = 131072, which exceeds the API's hard limit of 65536.
Expected Behavior
Kimi Code should clamp max_tokens to min(max_output_size, remaining_context_window) for all provider types, including openai. If max_output_size is configured, it should be the hard cap.
Additionally, Kimi Code should gracefully handle the case where the configured max_output_size itself exceeds the API's actual limit — ideally by catching the 400 and retrying with a smaller value, or by clamping max_tokens more conservatively.
Impact
- All OpenAI-compatible models where the actual API output limit is less than the configured
max_context_sizeare affected. - This is increasingly common with third-party providers (Ollama Cloud, OpenRouter, etc.) that aggregate models with varying output limits.
- The session is completely blocked — the error is deterministic and no retry helps.
Workaround
Set the environment variable:
export KIMI_MODEL_MAX_COMPLETION_TOKENS=65536
This acts as a hardCap and overrides the broken max_output_size path.
Alternatively, there is no config-only workaround because max_output_size is ignored for openai providers.
Related Issues
- #306 — Original bug report:
max_output_sizenot consumed foropenaiproviders. Open since v0.7.0. - #834 — Same bug in the compaction path. Open since v0.16.0.
- #476 — Related:
max_context_sizebeing sent asmax_output_sizeduring compaction. - #1131 (v0.20.1) — Partial fix: caps to remaining context window but does not address the
max_output_sizeignoring bug for openai providers.
Session Evidence
Session log (kimi-code.log):
2026-06-26T16:12:51.345Z INFO llm config provider=openai model=deepseek-v4-pro modelAlias=ollama-cloud/deepseek-v4-pro thinkingEffort=high systemPromptChars=36103 toolCount=460
2026-06-26T16:12:51.345Z INFO llm request turnStep=0.1
2026-06-26T16:12:52.446Z WARN llm request failed attempt=1/3 model=deepseek-v4-pro errorName=APIStatusError errorMessage="400 \"max_tokens (131072) exceeds model's maximum output tokens (65536)\"" statusCode=400
2026-06-26T16:12:52.446Z ERROR turn failed turnId=0
APIStatusError: 400 "max_tokens (131072) exceeds model's maximum output tokens (65536) for model deepseek-v4-pro"
at normalizeAPIStatusError (.../intermediates/main.cjs:1185:9)
at convertOpenAIError (.../intermediates/main.cjs:52368:10)
at OpenAILegacyChatProvider.generate (.../intermediates/main.cjs:53249:10)
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 provider-manager.ts and inspect how maxOutputSize is passed to OpenAI-compatible providers, then trace the v0.20.1 change from f1c8175 that caps against the remaining context window. Reproduce the Ollama Cloud configuration from the issue and verify that max_tokens is clamped to the smaller of max_output_size and the remaining context window for OpenAI providers.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100