Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ai-edition): floor Anthropic-wire maxTokens at 16k for unknown models (#181)

Etienne Lescotddb59d5243fb
diff --git a/electron/ai-edition/deep-agent/chat-model.test.ts b/electron/ai-edition/deep-agent/chat-model.test.ts
index d659c41e9..cde99f6b1 100644
--- a/electron/ai-edition/deep-agent/chat-model.test.ts
+++ b/electron/ai-edition/deep-agent/chat-model.test.ts
@@ -5,7 +5,11 @@
// providers in 1.8.0 — see provider-registry.ts.
import { describe, expect, it } from "vitest";
-import { createOpenScreenChatModel, messageContentToText } from "./chat-model";
+import {
+ ANTHROPIC_API_MAX_OUTPUT_TOKENS,
+ createOpenScreenChatModel,
+ messageContentToText,
+} from "./chat-model";
/** ChatOpenAI keeps the `configuration` bag it was constructed with on
* `clientConfig`; that is where the base URL and default headers land. */
@@ -35,6 +39,57 @@ describe("createOpenScreenChatModel — provider aliases", () => {
});
});
+describe("createOpenScreenChatModel — Anthropic-wire output budget", () => {
+ // Regression for #181: ChatAnthropic's default maxTokens table only knows
+ // Claude slugs (16k); anything else — MiniMax-M3 included — falls back to
+ // 4096. With adaptive thinking on, a cold-start turn can spend that whole
+ // budget on reasoning and truncate before any text block, surfacing as
+ // "Empty response from model" on the first call only.
+ function maxTokens(model: unknown): number | undefined {
+ return (model as { maxTokens?: number }).maxTokens;
+ }
+
+ for (const provider of ["minimax", "minimax-token-plan"]) {
+ it(`sets an explicit maxTokens on the ${provider} ChatAnthropic`, async () => {
+ const model = await createOpenScreenChatModel({
+ provider,
+ model: "MiniMax-M3",
+ apiKey: "test-key",
+ });
+ expect(model.constructor.name).toBe("ChatAnthropic");
+ expect(maxTokens(model)).toBe(ANTHROPIC_API_MAX_OUTPUT_TOKENS);
+ });
+ }
+
+ it("floors maxTokens for non-Claude models on the anthropic provider", async () => {
+ const model = await createOpenScreenChatModel({
+ provider: "anthropic",
+ model: "some-self-hosted-model",
+ apiKey: "sk-ant-test",
+ baseUrl: "https://anthropic.example.internal",
+ });
+ expect(maxTokens(model)).toBe(ANTHROPIC_API_MAX_OUTPUT_TOKENS);
+ });
+
+ it("keeps LangChain's per-model default for known Claude slugs", async () => {
+ // claude-3-haiku's hard output limit is 4096 — overriding it with 16k
+ // would make the API reject every request for this model.
+ const legacy = await createOpenScreenChatModel({
+ provider: "anthropic",
+ model: "claude-3-haiku-20240307",
+ apiKey: "sk-ant-test",
+ });
+ expect(maxTokens(legacy)).toBe(4096);
+
+ const current = await createOpenScreenChatModel({
+ provider: "anthropic",
+ model: "claude-haiku-4-5",
+ apiKey: "sk-ant-test",
+ });
+ expect(maxTokens(current)).toBe(ANTHROPIC_API_MAX_OUTPUT_TOKENS);
+ });
+});
+
describe("messageContentToText", () => {
it("passes a plain string through", () => {
expect(messageContentToText("hello")).toBe("hello");
diff --git a/electron/ai-edition/deep-agent/chat-model.ts b/electron/ai-edition/deep-agent/chat-model.ts
index 80e1be757..4f9917482 100644
--- a/electron/ai-edition/deep-agent/chat-model.ts
+++ b/electron/ai-edition/deep-agent/chat-model.ts
@@ -28,6 +28,33 @@ export interface OpenScreenChatModelConfig {
// that don't actually authenticate (same as axcut's OPENAI_COMPATIBLE_NO_AUTH).
export const OPENAI_COMPATIBLE_NO_AUTH_API_KEY = "openscreen-openai-compatible-no-auth";
+// ponytail: explicit output budget for the Anthropic-wire providers
+// (`anthropic`, `minimax`, `minimax-token-plan`). ChatAnthropic picks its
+// default `maxTokens` from a table of known Claude models (16k for 4.x/5.x)
+// and falls back to 4096 for anything else — including every MiniMax slug and
+// any self-hosted model name. With thinking on, a cold-start turn can spend
+// the entire 4096-token budget on reasoning and truncate with
+// `stop_reason: "max_tokens"` before emitting a single text block — the
+// "first call returns an empty response" bug (#181). 16384 matches what the
+// known Claude models get.
+//
+// This only applies to the Anthropic Messages API path, where `max_tokens`
+// is mandatory: the OpenAI-shaped transports (ChatOpenAI, ChatMistralAI)
+// send no cap by default, so there is nothing to fix — and imposing one
+// would truncate outputs that are uncapped today.
+export const ANTHROPIC_API_MAX_OUTPUT_TOKENS = 16_384;
+
+// ponytail: LangChain's default-maxTokens table knows every released
+// claude-* slug with its real per-model limit (4096 for claude-3-haiku,
+// 16384 for 4.x/5.x) — trust it. Overriding with a flat 16k would exceed a
+// legacy model's hard limit and turn the request into a 400. Anything NOT
+// claude-shaped on the anthropic branch is a self-hosted Anthropic-compatible
+// endpoint behind `baseUrl`, which LangChain can't know — floor those at
+// ANTHROPIC_API_MAX_OUTPUT_TOKENS like the MiniMax path.
+function isKnownClaudeSlug(model: string): boolean {
+ return model.trim().toLowerCase().startsWith("claude-");
+}
+
export function resolveOpenAIChatApiKey(provider: string, apiKey?: string): string | undefined {
if (apiKey) return apiKey;
return provider === "openai-compatible" ? OPENAI_COMPATIBLE_NO_AUTH_API_KEY : undefined;
@@ -85,6 +112,7 @@ export async function createOpenScreenChatModel(
// ponytail: ChatAnthropic accepts `anthropicApiUrl` for self-hosted
// Anthropic-compatible endpoints — MiniMax uses this on the wire path.
...(config.baseUrl ? { anthropicApiUrl: config.baseUrl } : {}),
+ ...(isKnownClaudeSlug(config.model) ? {} : { maxTokens: ANTHROPIC_API_MAX_OUTPUT_TOKENS }),
...(reasoningOptions.thinking ? { thinking: reasoningOptions.thinking as never } : {}),
...(reasoningOptions.outputConfig
? { outputConfig: reasoningOptions.outputConfig as never }
@@ -138,6 +166,7 @@ async function createLocalProviderChatModel(
apiKey: config.apiKey,
model: config.model,
anthropicApiUrl: config.baseUrl ?? "https://api.minimax.io/anthropic",
+ maxTokens: ANTHROPIC_API_MAX_OUTPUT_TOKENS,
...(reasoningOptions.thinking ? { thinking: reasoningOptions.thinking as never } : {}),
});
default:
diff --git a/technical-documentation/architecture/llm-providers.md b/technical-documentation/architecture/llm-providers.md
index 6ed19b255..236c247e3 100644
--- a/technical-documentation/architecture/llm-providers.md
+++ b/technical-documentation/architecture/llm-providers.md
@@ -67,11 +67,13 @@ Renderer snapshots expose connection summaries, never raw credential values.
`createOpenScreenChatModel({provider, model, apiKey, baseUrl, reasoningEffort})` normalizes the provider id, builds the reasoning options, and returns a `BaseChatModel`:
-- `minimax` / `minimax-token-plan` → `ChatAnthropic` with `anthropicApiUrl` pointed at the MiniMax base.
-- `anthropic` → `ChatAnthropic`, plus `thinking` / `outputConfig` when reasoning is on.
+- `minimax` / `minimax-token-plan` → `ChatAnthropic` with `anthropicApiUrl` pointed at the MiniMax base and an explicit `maxTokens` (`ANTHROPIC_API_MAX_OUTPUT_TOKENS`, 16384). ChatAnthropic's default-output table only knows Claude slugs (16k) and falls back to 4096 for anything else — with adaptive thinking on, a cold-start MiniMax turn could spend that entire budget on reasoning and truncate before any text block (the "first call returns empty" bug, #181).
+- `anthropic` → `ChatAnthropic`, plus `thinking` / `outputConfig` when reasoning is on. Known `claude-*` slugs keep LangChain's per-model `maxTokens` default (overriding it would exceed legacy limits like claude-3-haiku's 4096); non-Claude model names — a self-hosted Anthropic-compatible endpoint behind `baseUrl` — get the same 16384 floor as MiniMax.
- `mistral` → `ChatMistralAI`.
- everything else (`openai`, `google`, `openrouter`, `openai-compatible`) → `ChatOpenAI`, with the base URL defaulted per provider and `disableStreaming` set for Gemini 3, whose OpenAI-compat path cannot stream and tool-call at the same time.
+The `maxTokens` floor is Anthropic-wire-only by design: the OpenAI-shaped transports send no `max_tokens` by default (the provider's own limit applies), so there is no 4096 fallback to fix — and adding a cap would truncate outputs that are uncapped today.
+
An unrecognised provider reaching `createLocalProviderChatModel` throws rather than silently defaulting to OpenAI.
Three call sites share that factory: