Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): read Perplexity and MiniMax max_tokens ceilings

xpeng5278-web683f3dbda44d
diff --git a/packages/agent-runtime/src/provider-retry.test.ts b/packages/agent-runtime/src/provider-retry.test.ts
index 060201852..beee1d86a 100644
--- a/packages/agent-runtime/src/provider-retry.test.ts
+++ b/packages/agent-runtime/src/provider-retry.test.ts
@@ -18,6 +18,7 @@ import {
STREAM_IDLE_TIMEOUT_DEFAULT_MS,
STREAM_IDLE_TIMEOUT_FLOOR_MS,
streamIdleTimeoutMs,
+ isOutputLimitRejection,
outputTokenCeiling,
stripOutputLimitFields,
withoutDerivedOutputLimit,
@@ -25,7 +26,7 @@ import {
} from "./provider-retry.js";
import { activeNodeTransportRoute } from "./node-proxy.js";
import { describeProviderFetchFailure } from "./provider-transport-recovery.js";
-import type { ClassifiedAgentError } from "./agent-errors.js";
+import { classifyAgentError, type ClassifiedAgentError } from "./agent-errors.js";
const model = {
id: "model",
@@ -1122,6 +1123,10 @@ const DEEPSEEK_OUTPUT_CEILING =
"400 Invalid max_tokens value, the valid range of max_tokens is [1, 8192]";
const GROQ_OUTPUT_CEILING =
"`max_tokens` must be less than or equal to `16384`, the maximum value for `max_tokens` is less than the `context_window` for this model";
+const PERPLEXITY_OUTPUT_CEILING =
+ '400: {"error":{"message":"max_tokens must be at most 128000","type":"invalid_request","code":400,"param":"max_tokens"}}';
+const MINIMAX_OUTPUT_CEILING =
+ "400: invalid params, model[MiniMax-M2] does not support max tokens > 196608 (2013)";
describe("outputTokenCeiling", () => {
it("reads the upper bound of a DeepSeek max_tokens range", () => {
@@ -1164,9 +1169,57 @@ describe("outputTokenCeiling", () => {
).toBe(16384);
});
+ it("reads a Perplexity at-most ceiling with or without backticks", () => {
+ expect(outputTokenCeiling("max_tokens must be at most 128000")).toBe(128000);
+ expect(outputTokenCeiling("`max_tokens` must be at most 128000")).toBe(128000);
+ expect(outputTokenCeiling(PERPLEXITY_OUTPUT_CEILING)).toBe(128000);
+ expect(
+ outputTokenCeiling("InvalidRequestError: max_tokens must be at most 4096"),
+ ).toBe(4096);
+ expect(
+ outputTokenCeiling("max_completion_tokens must be at most 8,192"),
+ ).toBe(8192);
+ expect(
+ outputTokenCeiling("`max_output_tokens` must be at most `4096`"),
+ ).toBe(4096);
+ });
+
+ it("reads a MiniMax max tokens ceiling and ignores the business code", () => {
+ expect(
+ outputTokenCeiling(
+ "invalid params, model[MiniMax-M2] does not support max tokens > 196608 (2013)",
+ ),
+ ).toBe(196608);
+ expect(outputTokenCeiling(MINIMAX_OUTPUT_CEILING)).toBe(196608);
+ });
+
+ it("treats those 400s as output-limit rejections", () => {
+ expect(
+ isOutputLimitRejection(
+ classifyAgentError("400: max_tokens must be at most 128000"),
+ ),
+ ).toBe(true);
+ expect(
+ isOutputLimitRejection(classifyAgentError(MINIMAX_OUTPUT_CEILING)),
+ ).toBe(true);
+ expect(
+ isOutputLimitRejection(
+ classifyAgentError(
+ "400: Prompt tokens plus max_tokens must be at most 131,072",
+ ),
+ ),
+ ).toBe(false);
+ });
+
it("does not treat a context-length message as an output ceiling", () => {
expect(outputTokenCeiling("maximum context length is 131072 tokens")).toBeUndefined();
expect(outputTokenCeiling("Range of input length should be [1, 30720]")).toBeUndefined();
+ expect(
+ outputTokenCeiling("Prompt tokens plus max_tokens must be at most 131,072"),
+ ).toBeUndefined();
+ expect(
+ outputTokenCeiling("Prompt tokens plus `max_tokens` must be at most 131,072"),
+ ).toBeUndefined();
});
});
@@ -1280,6 +1333,46 @@ describe("explicit output-limit repair", () => {
});
});
+ it("clamps a Perplexity at-most ceiling instead of failing the turn", async () => {
+ const result = await repairPayload(PERPLEXITY_OUTPUT_CEILING, {
+ max_tokens: 200000,
+ stream: true,
+ });
+
+ expect(result.attempts).toBe(2);
+ expect(result.claim).not.toHaveBeenCalled();
+ expect(result.sleep).not.toHaveBeenCalled();
+ expect(result.repaired).toEqual({
+ marked: true,
+ max_tokens: 128000,
+ stream: true,
+ });
+ });
+
+ it("clamps a MiniMax max tokens ceiling and ignores the business code", async () => {
+ const result = await repairPayload(MINIMAX_OUTPUT_CEILING, {
+ max_tokens: 200000,
+ });
+
+ expect(result.attempts).toBe(2);
+ expect(result.claim).not.toHaveBeenCalled();
+ expect(result.sleep).not.toHaveBeenCalled();
+ expect(result.repaired).toEqual({
+ marked: true,
+ max_tokens: 196608,
+ });
+ });
+
+ it("leaves a prompt-plus-max_tokens context sum terminal", async () => {
+ const result = await repairPayload(
+ "400: Prompt tokens plus max_tokens must be at most 131,072",
+ { max_tokens: 65536 },
+ );
+
+ expect(result.attempts).toBe(1);
+ expect(result.claim).toHaveBeenCalledTimes(1);
+ });
+
it("clamps a proxy expected-value ceiling without adding a missing field", async () => {
const result = await repairPayload(
'400 {"error":{"code":"InvalidParameter","message":"The parameter max_tokens specified in the request is not valid: integer above maximum value, expected a value <= 32768, but got 64000 instead."}}',
diff --git a/packages/agent-runtime/src/provider-retry.ts b/packages/agent-runtime/src/provider-retry.ts
index 4a21981d4..1fbb18313 100644
--- a/packages/agent-runtime/src/provider-retry.ts
+++ b/packages/agent-runtime/src/provider-retry.ts
@@ -135,8 +135,14 @@ export function withoutDerivedOutputLimit(
* - DashScope / Bailian (Qwen): "Range of max_tokens should be [1, 8192]"
* and "<400> InternalError.Algo.InvalidParameter: Range of max_tokens should be [1, 65536]"
* - Groq: "`max_tokens` must be less than or equal to `16384`" (backticks optional)
+ * - Perplexity, and the same Anthropic sentence: "`max_tokens` must be at most 128000"
+ * (backticks optional). "Prompt tokens plus max_tokens must be at most N" names a
+ * context sum, so a `plus` prefix is not this ceiling.
+ * - MiniMax: "does not support max tokens > 196608 (2013)". The words are
+ * space-separated. The parenthesized business code is not the ceiling.
*
- * The same shapes apply to `max_completion_tokens` and `max_output_tokens`.
+ * The same shapes apply to `max_completion_tokens` and `max_output_tokens`,
+ * except the MiniMax sentence, which says `max tokens`.
* A bracketed range contributes its upper bound. Context-length wording such
* as "maximum context length is 131072 tokens" is not an output ceiling.
*/
@@ -146,6 +152,10 @@ const OUTPUT_TOKEN_CEILING_PATTERNS = [
/max_tokens[\s\S]{0,180}?expected a value <=\s*(\d[\d,]*)/i,
/range of `?max_(?:completion_|output_)?tokens`?(?: is| should be) \[`?\d+`?,\s*`?(\d[\d,]*)`?\]/i,
/`?max_(?:completion_|output_)?tokens`? must be less than or equal to `?(\d[\d,]*)`?/i,
+ // The lookbehind also covers an optional backtick, so "plus `max_tokens`"
+ // cannot match by starting after that backtick.
+ /(?<!plus\s+`?)`?max_(?:completion_|output_)?tokens`? must be at most `?(\d[\d,]*)`?/i,
+ /does not support max tokens >\s*(\d[\d,]*)/i,
];
/** The completion cap named in `message`, when it is one of the shapes above. */