From 026cf8870667ba4e508610abf1aa9d1a89ab631a Mon Sep 17 00:00:00 2001 From: yorha9e Date: Wed, 22 Jul 2026 20:09:49 +0800 Subject: [PATCH 1/2] fix(kosong): conservative max_tokens fallback for custom Anthropic-compatible endpoints Custom Anthropic-compatible endpoints (e.g., Volcano Ark, DeepSeek) often enforce a lower output token limit than the 128000 FALLBACK_MAX_TOKENS designed for official Claude models. This causes immediate 400 rejections for any non-Claude model on these endpoints. Two-layer fix: 1. Conservative fallback: resolveDefaultMaxTokens now accepts an optional baseUrl parameter. When the endpoint is custom (non-Anthropic) and the model is not a recognized Claude, the fallback drops from 128000 to 32768 (the most common ceiling among compatible providers). 2. Error-driven recovery: when a 400 response mentions a max_tokens limit, parseMaxTokensLimit extracts the provider's declared ceiling, and the v2 request loop clamps the completion budget and retries once. The discovered limit is cached per model so subsequent requests start pre-clamped. Closes #2062 --- .changeset/anthropic-max-tokens-fallback.md | 5 + .../agent/llmRequester/llmRequesterService.ts | 43 ++++++++- .../src/kosong/contract/errors.ts | 30 ++++++ .../provider/bases/anthropic/anthropic.ts | 19 +++- .../llmRequester/llmRequesterService.test.ts | 92 +++++++++++++++++++ .../test/kosong/contract/errors.test.ts | 23 +++++ .../test/kosong/provider/composition.test.ts | 35 +++++++ packages/kosong/src/errors.ts | 30 ++++++ packages/kosong/src/providers/anthropic.ts | 21 ++++- packages/kosong/test/anthropic.test.ts | 26 ++++++ packages/kosong/test/errors.test.ts | 33 +++++++ 11 files changed, 344 insertions(+), 13 deletions(-) create mode 100644 .changeset/anthropic-max-tokens-fallback.md diff --git a/.changeset/anthropic-max-tokens-fallback.md b/.changeset/anthropic-max-tokens-fallback.md new file mode 100644 index 0000000000..71c9e64db6 --- /dev/null +++ b/.changeset/anthropic-max-tokens-fallback.md @@ -0,0 +1,5 @@ +--- +"@moonshot-ai/kimi-code": patch +--- + +Fix 400 rejections from Anthropic-compatible providers that enforce a lower max_tokens output limit than the default 128000 fallback. Custom endpoints now use a conservative 32768 default, and a rejected request is automatically retried with the provider's declared limit. diff --git a/packages/agent-core-v2/src/agent/llmRequester/llmRequesterService.ts b/packages/agent-core-v2/src/agent/llmRequester/llmRequesterService.ts index e8589fdd74..07c2d0e8c0 100644 --- a/packages/agent-core-v2/src/agent/llmRequester/llmRequesterService.ts +++ b/packages/agent-core-v2/src/agent/llmRequester/llmRequesterService.ts @@ -8,8 +8,10 @@ * params, then drives a bounded request chain through the `ModelRequester` * resolved from `IModelCatalog`: one primary `requester.request(input, signal, * params)` attempt plus projection rebuilds for request structure or media - * compatibility; general retry policy remains in the loop's `stepRetry` - * plugin. Before each request the projected messages pass through `media`'s + * compatibility and a one-shot completion-budget clamp when the provider + * rejects `max_tokens` (the server-declared ceiling is remembered per model, + * so each model pays for at most one rejected-and-resent request); general + * retry policy remains in the loop's `stepRetry` plugin. Before each request the projected messages pass through `media`'s * video resolver, which rewrites every `kimi-file://` prompt-video reference * to a provider-acceptable part (uploaded `ms://`, inline base64, or a * `