Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(ai-gateway): classify current input limits (#6945)

Ezra Ellette739090783bb1
diff --git a/packages/ai-gateway/src/handlers/chat.ts b/packages/ai-gateway/src/handlers/chat.ts
index 0fa9e5d8e..6e52b5244 100644
--- a/packages/ai-gateway/src/handlers/chat.ts
+++ b/packages/ai-gateway/src/handlers/chat.ts
@@ -123,6 +123,8 @@ const USER_INPUT_TOO_LARGE_PATTERNS = [
/maximum context length/i,
/context length.*exceeded/i,
/request payload size exceeds/i,
+ /input tokens exceed the configured limit/i,
+ /exceeds the available context size/i,
// Vertex MaaS (glm-5 etc): "The input (325052 tokens) is longer than the
// model's context length (202752 tokens)" — SCREENPIPE-AI-PROXY-C, 28 users.
/longer than the model'?s context length/i,
@@ -144,6 +146,10 @@ const CLIENT_PAYLOAD_PATTERNS: Array<{ re: RegExp; message: string }> = [
re: /at least one message is required/i,
message: 'The request must include at least one user or assistant message.',
},
+ {
+ re: /tool_calls.*array too long.*maximum length/i,
+ message: 'A message contains too many tool calls. Start a new chat or compact the conversation and try again.',
+ },
];
export function clientPayloadMessage(status: number, msg: string): string | null {
diff --git a/packages/ai-gateway/src/test/chat-fallback.unit.test.ts b/packages/ai-gateway/src/test/chat-fallback.unit.test.ts
index 88b9a9c66..fc68df28f 100644
--- a/packages/ai-gateway/src/test/chat-fallback.unit.test.ts
+++ b/packages/ai-gateway/src/test/chat-fallback.unit.test.ts
@@ -78,12 +78,38 @@ describe('chat handler — oversized-input classification (SCREENPIPE-AI-PROXY-C
expect(isUserInputTooLarge(400, 'prompt is too long: 250000 tokens > 200000 maximum')).toBe(true);
});
+ it('matches current OpenAI input-limit phrasings', () => {
+ expect(
+ isUserInputTooLarge(
+ 400,
+ 'Input tokens exceed the configured limit of 272000 tokens. Your messages resulted in 272325 tokens.',
+ ),
+ ).toBe(true);
+ expect(
+ isUserInputTooLarge(
+ 400,
+ 'request (64174 tokens) exceeds the available context size (32768 tokens), try increasing it',
+ ),
+ ).toBe(true);
+ });
+
it('ignores unrelated 400s and non-4xx statuses', () => {
expect(isUserInputTooLarge(400, 'invalid tool schema')).toBe(false);
expect(isUserInputTooLarge(500, 'maximum context length exceeded')).toBe(false);
});
});
+describe('chat handler — oversized tool-call payloads', () => {
+ it('returns a clean client message for the current OpenAI tool-call limit', () => {
+ const message = clientPayloadMessage(
+ 400,
+ "Invalid 'messages[11].tool_calls': array too long. Expected an array with maximum length 128, but got an array with length 504 instead.",
+ );
+
+ expect(message).toContain('too many tool calls');
+ });
+});
+
describe('chat handler — geo-block detection (SCREENPIPE-AI-PROXY-1C)', () => {
it('detects the OpenAI unsupported-region 403', () => {
expect(isGeoBlocked(403, '403 Country, region, or territory not supported')).toBe(true);