Skip to content

Commit 2a097f3

Browse files
fix(llm): expand context overflow patterns (anomalyco#37840)
Co-authored-by: Aiden Cline <rekram1-node@users.noreply.github.com>
1 parent ba4b8e2 commit 2a097f3

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

packages/llm/src/provider-error.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { LLMError, ProviderErrorEvent } from "./schema"
33

44
const patterns = [
55
/prompt is too long/i,
6+
/request_too_large/i,
67
/input is too long for requested model/i,
78
/exceeds the context window/i,
9+
/exceeds (?:the )?(?:model'?s )?maximum context length(?: of [\d,]+ tokens?|\s*\([\d,]+\))/i,
810
/input token count.*exceeds the maximum/i,
911
/tokens in request more than max tokens allowed/i,
1012
/maximum prompt length is \d+/i,
1113
/reduce the length of the messages/i,
1214
/maximum context length is \d+ tokens/i,
15+
/exceeds (?:the )?maximum allowed input length of [\d,]+ tokens?/i,
16+
/input \(\d+ tokens\) is longer than the model'?s context length \(\d+ tokens\)/i,
1317
/exceeds the limit of \d+/i,
1418
/exceeds the available context size/i,
1519
/greater than the context length/i,
@@ -21,11 +25,17 @@ const patterns = [
2125
/input length.*exceeds.*context length/i,
2226
/prompt too long; exceeded (?:max )?context length/i,
2327
/too large for model with \d+ maximum context length/i,
28+
/prompt has [\d,]+ tokens?, but the configured context size is [\d,]+ tokens?/i,
2429
/model_context_window_exceeded/i,
30+
/too many tokens/i,
31+
/token limit exceeded/i,
2532
]
2633

34+
const exclusions = [/^(throttling error|service unavailable):/i, /rate limit/i, /too many requests/i]
35+
2736
export const isContextOverflow = (message: string) =>
28-
patterns.some((pattern) => pattern.test(message)) || /^4(00|13)\s*(status code)?\s*\(no body\)/i.test(message)
37+
!exclusions.some((pattern) => pattern.test(message)) &&
38+
(patterns.some((pattern) => pattern.test(message)) || /^4(00|13)\s*(status code)?\s*\(no body\)/i.test(message))
2939

3040
export const isContextOverflowFailure = (failure: unknown) =>
3141
failure instanceof LLMError

packages/llm/test/provider-error.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,29 @@ import { describe, expect, test } from "bun:test"
22
import { isContextOverflow } from "../src"
33

44
describe("provider error classification", () => {
5-
test("classifies Z.AI GLM token limit messages as context overflow", () => {
6-
expect(isContextOverflow("tokens in request more than max tokens allowed")).toBe(true)
5+
test("classifies provider token limit messages as context overflow", () => {
6+
const messages = [
7+
"tokens in request more than max tokens allowed",
8+
'{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}',
9+
"Requested token count exceeds the model's maximum context length of 131072 tokens.",
10+
"Input length (265330) exceeds model's maximum context length (262144).",
11+
"Input length 131393 exceeds the maximum allowed input length of 131040 tokens.",
12+
"The input (516368 tokens) is longer than the model's context length (262144 tokens).",
13+
"Prompt has 5,958,968 tokens, but the configured context size is 256,000 tokens",
14+
"Too many tokens",
15+
"Token limit exceeded",
16+
]
17+
18+
expect(messages.every(isContextOverflow)).toBe(true)
19+
})
20+
21+
test("does not classify rate limits as context overflow", () => {
22+
const messages = [
23+
"Throttling error: Too many tokens, please wait before trying again.",
24+
"Rate limit exceeded, please retry after 30 seconds.",
25+
"Too many requests. Please slow down.",
26+
]
27+
28+
expect(messages.some(isContextOverflow)).toBe(false)
729
})
830
})

0 commit comments

Comments
 (0)