@@ -10,7 +10,7 @@ export function isConfiguredContextSizeOverflowError(errorMessage: string): bool
1010}
1111
1212/**
13- * Regex patterns to detect context overflow errors from different providers.
13+ * Canonical scoped patterns for context overflow errors from different providers.
1414 *
1515 * These patterns match error messages returned when the input exceeds
1616 * the model's context window.
@@ -41,7 +41,7 @@ export function isConfiguredContextSizeOverflowError(errorMessage: string): bool
4141 * input filling the context window.
4242 * - Ollama: Some deployments truncate silently, others return errors like "prompt too long; exceeded max context length by X tokens"
4343 */
44- const OVERFLOW_PATTERNS = [
44+ const ASSISTANT_OVERFLOW_PATTERNS = [
4545 / p r o m p t i s t o o l o n g / i, // Anthropic token overflow
4646 / r e q u e s t _ t o o _ l a r g e / i, // Anthropic request byte-size overflow (HTTP 413)
4747 / i n p u t i s t o o l o n g f o r r e q u e s t e d m o d e l / i, // Amazon Bedrock
@@ -70,9 +70,72 @@ const OVERFLOW_PATTERNS = [
7070 / ^ 4 1 3 \s * (?: s t a t u s c o d e ) ? \s * \( n o b o d y \) / i, // Cerebras: 413 with no body
7171] ;
7272
73+ const FAILOVER_EXPLICIT_OVERFLOW_PATTERNS = [
74+ / r e q u e s t _ t o o _ l a r g e / i, // Anthropic request byte-size overflow
75+ / c o n t e x t _ o v e r f l o w / i,
76+ CONFIGURED_CONTEXT_SIZE_OVERFLOW_RE , // DS4 server
77+ / i n v a l i d _ a r g u m e n t [ \s \S ] * m a x i m u m n u m b e r o f t o k e n s / i, // Google/Vertex
78+ / r e q u e s t e x c e e d s t h e m a x i m u m s i z e / i, // Anthropic
79+ / c o n t e x t l e n g t h e x c e e d e d / i,
80+ / m a x i m u m c o n t e x t l e n g t h / i,
81+ / p r o m p t i s t o o l o n g / i,
82+ / p r o m p t t o o l o n g / i,
83+ / e x c e e d s m o d e l c o n t e x t w i n d o w / i,
84+ / m o d e l t o k e n l i m i t / i,
85+ / i n p u t e x c e e d s [ \s \S ] * m a x i m u m n u m b e r o f t o k e n s / i,
86+ / ^ (? = [ \s \S ] * c o n t e x t w i n d o w ) (? = [ \s \S ] * r a n o u t o f (?: r o o m | s p a c e ) ) / i, // Codex
87+ / r e q u e s t s i z e e x c e e d s [ \s \S ] * c o n t e x t w i n d o w / i,
88+ / c o n t e x t o v e r f l o w : / i,
89+ / e x c e e d c o n t e x t l i m i t / i,
90+ / e x c e e d s t h e m o d e l ' ? s m a x i m u m c o n t e x t / i,
91+ / m a x _ t o k e n s [ \s \S ] * e x c e e d [ \s \S ] * c o n t e x t / i,
92+ / i n p u t l e n g t h [ \s \S ] * e x c e e d [ \s \S ] * c o n t e x t / i,
93+ / 4 1 3 [ \s \S ] * t o o l a r g e / i,
94+ / c o n t e x t _ w i n d o w _ e x c e e d e d / i,
95+ // FIXED(refactor-06): PR 2 removed the embedded-429 false positive; this is provider overflow.
96+ / i n p u t l e n g t h [ \d , ] + \s + t o k e n s ? e x c e e d s t h e m o d e l l i m i t / i,
97+ / 上 下 文 过 长 | 上 下 文 超 出 | 上 下 文 长 度 超 | 超 出 最 大 上 下 文 | 请 压 缩 上 下 文 / ,
98+ ] ;
99+
100+ const PROVIDER_FALLBACK_OVERFLOW_PATTERNS = [
101+ / \b i n p u t t o k e n c o u n t e x c e e d s t h e m a x i m u m n u m b e r o f i n p u t t o k e n s \b / i, // AWS Bedrock
102+ / \b i n p u t i s t o o l o n g f o r t h i s m o d e l \b / i, // AWS Bedrock stream errors
103+ / \b i n p u t e x c e e d s t h e m a x i m u m n u m b e r o f t o k e n s \b / i, // Google Vertex / Gemini
104+ / \b o l l a m a e r r o r : \s * c o n t e x t l e n g t h e x c e e d e d (?: , \s * t o o m a n y t o k e n s ) ? \b / i,
105+ / \b t o t a l t o k e n s ? .* e x c e e d s ? (?: t h e ) ? (?: m o d e l (?: ' s ) ? ) ? (?: m a x | m a x i m u m | l i m i t ) / i, // Cohere
106+ / \b (?: r e q u e s t | p r o m p t ) \( \d [ \d , ] * \s * t o k e n s ? \) e x c e e d s (?: t h e ) ? a v a i l a b l e c o n t e x t s i z e \b / i, // llama.cpp
107+ / \b i n p u t (?: i s ) ? t o o l o n g f o r (?: t h e ) ? m o d e l \b / i,
108+ ] ;
109+
110+ const CONTEXT_OVERFLOW_PATTERN_SCOPES = {
111+ "assistant-error" : ASSISTANT_OVERFLOW_PATTERNS ,
112+ "failover-explicit" : FAILOVER_EXPLICIT_OVERFLOW_PATTERNS ,
113+ "provider-fallback" : PROVIDER_FALLBACK_OVERFLOW_PATTERNS ,
114+ "failover-hint" : [
115+ / c o n t e x t .* o v e r f l o w | c o n t e x t w i n d o w .* ( t o o (?: l a r g e | l o n g ) | e x c e e d | o v e r | l i m i t | m a x (?: i m u m ) ? | r e q u e s t e d | s e n t | t o k e n s ) | p r o m p t .* ( t o o (?: l a r g e | l o n g ) | e x c e e d | o v e r | l i m i t | m a x (?: i m u m ) ? ) | (?: r e q u e s t | i n p u t ) .* (?: c o n t e x t | w i n d o w | l e n g t h | t o k e n ) .* ( t o o (?: l a r g e | l o n g ) | e x c e e d | o v e r | l i m i t | m a x (?: i m u m ) ? ) / i,
116+ ] ,
117+ "context-window-too-small" : [ / c o n t e x t w i n d o w .* ( t o o s m a l l | m i n i m u m i s ) / i] ,
118+ "tpm-rate-limit-hint" : [ / \b t p m \b | t o k e n s p e r m i n u t e / i] ,
119+ "rate-limit-hint" : [
120+ / r a t e l i m i t | t o o m a n y r e q u e s t s | r e q u e s t s p e r (?: m i n u t e | h o u r | d a y ) | q u o t a | t h r o t t l | 4 2 9 \b | t o k e n s p e r d a y / i,
121+ ] ,
122+ } as const ;
123+
124+ export type ContextOverflowMessageScope = keyof typeof CONTEXT_OVERFLOW_PATTERN_SCOPES ;
125+
126+ /** Match one canonical context-overflow wording scope without applying caller policy. */
127+ export function matchesContextOverflowMessage (
128+ errorMessage : string ,
129+ scope : ContextOverflowMessageScope ,
130+ ) : boolean {
131+ return CONTEXT_OVERFLOW_PATTERN_SCOPES [ scope ] . some ( ( pattern : RegExp ) =>
132+ pattern . test ( errorMessage ) ,
133+ ) ;
134+ }
135+
73136/**
74137 * Patterns that indicate non-overflow errors (e.g. rate limiting, server errors).
75- * Error messages matching unknown of these are excluded from overflow detection
138+ * Error messages matching any of these are excluded from overflow detection
76139 * even if they also match an OVERFLOW_PATTERN.
77140 *
78141 * Example: Bedrock formats throttling errors as "ThrottlingException: Too many tokens,
@@ -142,7 +205,7 @@ function resolveContextInputTokens(message: AssistantMessage): number | undefine
142205 * 1. Send a request that exceeds the model's context window
143206 * 2. Check the errorMessage in the response
144207 * 3. Create a regex pattern that matches the error
145- * 4. The pattern should be added to OVERFLOW_PATTERNS in this file, or
208+ * 4. The pattern should be added to the appropriate canonical scope in this file, or
146209 * check the errorMessage yourself before calling this function
147210 *
148211 * @param message - The assistant message to check
@@ -156,7 +219,7 @@ export function isContextOverflow(message: AssistantMessage, contextWindow?: num
156219 const errorMessage = message . errorMessage ;
157220 // Skip messages matching known non-overflow patterns (e.g. throttling / rate-limit)
158221 const isNonOverflow = NON_OVERFLOW_PATTERNS . some ( ( p ) => p . test ( errorMessage ) ) ;
159- if ( ! isNonOverflow && OVERFLOW_PATTERNS . some ( ( p ) => p . test ( errorMessage ) ) ) {
222+ if ( ! isNonOverflow && matchesContextOverflowMessage ( errorMessage , "assistant-error" ) ) {
160223 return true ;
161224 }
162225 }
0 commit comments