From 8971a322b387a9e277841d317bb7b1314495ef85 Mon Sep 17 00:00:00 2001 From: RTCartist Date: Thu, 4 Jun 2026 23:51:32 +0800 Subject: [PATCH 1/3] fix: forward maxOutputSize to OpenAI-compatible providers The `toKosongProviderConfig` function forwarded `maxOutputSize` as `defaultMaxTokens` for Anthropic providers but omitted it for `openai` and `openai_responses` providers. This caused OpenAI-compatible providers like DeepSeek to receive the full `max_context_size` (e.g. 1,000,000) as `max_tokens`, exceeding the model's actual limit and triggering a 400 error. Forward `maxOutputSize` as `maxTokens` for the `openai` provider and as `maxOutputTokens` for the `openai_responses` provider, matching the constructor interfaces of their respective chat provider classes. Closes #306 --- .../src/session/provider-manager.ts | 2 + .../test/harness/runtime-provider.test.ts | 95 +++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/packages/agent-core/src/session/provider-manager.ts b/packages/agent-core/src/session/provider-manager.ts index 38d3e1cd1f..dd6de62c92 100644 --- a/packages/agent-core/src/session/provider-manager.ts +++ b/packages/agent-core/src/session/provider-manager.ts @@ -236,6 +236,7 @@ function toKosongProviderConfig( baseUrl: providerValue(provider.baseUrl, provider.env, 'OPENAI_BASE_URL'), apiKey: providerApiKey(provider), reasoningKey, + ...(maxOutputSize !== undefined ? { maxTokens: maxOutputSize } : {}), ...defaultHeadersField(provider.customHeaders), }; case 'kimi': @@ -259,6 +260,7 @@ function toKosongProviderConfig( model, baseUrl: providerValue(provider.baseUrl, provider.env, 'OPENAI_BASE_URL'), apiKey: providerApiKey(provider), + ...(maxOutputSize !== undefined ? { maxOutputTokens: maxOutputSize } : {}), ...defaultHeadersField(provider.customHeaders), }; case 'vertexai': { diff --git a/packages/agent-core/test/harness/runtime-provider.test.ts b/packages/agent-core/test/harness/runtime-provider.test.ts index 986357bf87..de3e5b8434 100644 --- a/packages/agent-core/test/harness/runtime-provider.test.ts +++ b/packages/agent-core/test/harness/runtime-provider.test.ts @@ -338,6 +338,101 @@ describe('resolveRuntimeProvider maxOutputSize forwarding', () => { }); }); + it('forwards alias.maxOutputSize to the openai provider config as maxTokens', () => { + const resolved = resolveRuntimeProvider({ + config: { + ...BASE_CONFIG, + providers: { + ...BASE_CONFIG.providers, + openai: { + type: 'openai', + apiKey: 'sk-openai', + baseUrl: 'https://openai.example/v1', + }, + }, + models: { + ...BASE_CONFIG.models!, + 'deepseek-alias': { + provider: 'openai', + model: 'deepseek-chat', + maxContextSize: 1_000_000, + maxOutputSize: 393216, + }, + }, + }, + model: 'deepseek-alias', + }); + + expect(resolved.provider).toMatchObject({ + type: 'openai', + model: 'deepseek-chat', + maxTokens: 393216, + }); + }); + + it('omits maxTokens from the openai provider config when alias.maxOutputSize is unset', () => { + const resolved = resolveRuntimeProvider({ + config: { + ...BASE_CONFIG, + providers: { + ...BASE_CONFIG.providers, + openai: { + type: 'openai', + apiKey: 'sk-openai', + baseUrl: 'https://openai.example/v1', + }, + }, + models: { + ...BASE_CONFIG.models!, + 'gpt-alias': { + provider: 'openai', + model: 'gpt-4o', + maxContextSize: 128000, + }, + }, + }, + model: 'gpt-alias', + }); + + expect(resolved.provider).toMatchObject({ + type: 'openai', + model: 'gpt-4o', + }); + expect('maxTokens' in resolved.provider).toBe(false); + }); + + it('forwards alias.maxOutputSize to the openai_responses provider config as maxOutputTokens', () => { + const resolved = resolveRuntimeProvider({ + config: { + ...BASE_CONFIG, + providers: { + ...BASE_CONFIG.providers, + openai_resp: { + type: 'openai_responses', + apiKey: 'sk-openai', + baseUrl: 'https://openai.example/v1', + }, + }, + models: { + ...BASE_CONFIG.models!, + 'o3-alias': { + provider: 'openai_resp', + model: 'o3', + maxContextSize: 200000, + maxOutputSize: 100000, + }, + }, + }, + model: 'o3-alias', + }); + + expect(resolved.provider).toMatchObject({ + type: 'openai_responses', + model: 'o3', + maxOutputTokens: 100000, + }); + }); + it('omits adaptiveThinking when alias.adaptiveThinking is unset', () => { const resolved = resolveRuntimeProvider({ config: { From f5049bb250978cda4a121759c6b06e8be463c3b5 Mon Sep 17 00:00:00 2001 From: RTCartist Date: Thu, 4 Jun 2026 23:51:45 +0800 Subject: [PATCH 2/3] chore: add changeset for OpenAI provider maxOutputSize fix --- .changeset/openai-max-output-size.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/openai-max-output-size.md diff --git a/.changeset/openai-max-output-size.md b/.changeset/openai-max-output-size.md new file mode 100644 index 0000000000..948734f4c0 --- /dev/null +++ b/.changeset/openai-max-output-size.md @@ -0,0 +1,5 @@ +--- +"@moonshot-ai/kimi-code": patch +--- + +Fix 400 "Invalid max_tokens value" error when using DeepSeek and other OpenAI-compatible providers with `max_output_size` configured From 305e9629e28a3009c15c03c24640bfa5075d2d55 Mon Sep 17 00:00:00 2001 From: RTCartist Date: Fri, 5 Jun 2026 02:04:24 +0800 Subject: [PATCH 3/3] chore(changeset): include @moonshot-ai/agent-core in maxOutputSize fix Source changes live in `packages/agent-core/src/session/provider-manager.ts`, so the agent-core package needs to bump alongside `@moonshot-ai/kimi-code` to match the repo convention used by prior internal-package changes. --- .changeset/openai-max-output-size.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/openai-max-output-size.md b/.changeset/openai-max-output-size.md index 948734f4c0..8ccf53567f 100644 --- a/.changeset/openai-max-output-size.md +++ b/.changeset/openai-max-output-size.md @@ -1,4 +1,5 @@ --- +"@moonshot-ai/agent-core": patch "@moonshot-ai/kimi-code": patch ---