alibaba/page-agent · warning · InvokeError
CONTEXT_LENGTH
CONTEXT_LENGTH
Error message
Response truncated: max tokens reached
What it means
The model stopped because it hit the max_tokens / output token limit (finish_reason === 'length') before completing a valid response. The full raw response is attached in details, and unlike other errors there is no underlying cause object. For tool-calling agents this usually means truncated, unparseable tool arguments.
Source
Thrown at packages/llms/src/OpenAIClient.ts:154
InvokeErrorTypes.INVALID_RESPONSE,
'Response body is not valid JSON',
error
)
}
const choice = data.choices?.[0]
if (!choice) {
throw new InvokeError(InvokeErrorTypes.INVALID_SCHEMA, 'No choices in response', data)
}
// Check finish_reason
switch (choice.finish_reason) {
case 'tool_calls':
case 'function_call': // gemini
case 'stop': // some models use this even with tool calls
break
case 'length':
throw new InvokeError(
InvokeErrorTypes.CONTEXT_LENGTH,
'Response truncated: max tokens reached',
undefined,
data
)
case 'content_filter':
throw new InvokeError(
InvokeErrorTypes.CONTENT_FILTER,
'Content filtered by safety system',
undefined,
data
)
default:
throw new InvokeError(
InvokeErrorTypes.INVALID_SCHEMA,
`Unexpected finish_reason: ${choice.finish_reason}`,
undefined,
dataView on GitHub (pinned to d02db1ee7c)
Solutions
- Raise or remove max_tokens in the request/config
- If the provider has a separate output-token cap, request a higher one
- Reduce expected output size: fewer/simpler tools, shorter requested formats
- If finish_reason 'length' recurs, catch CONTEXT_LENGTH and re-invoke asking for a continuation or a more compact response
Example fix
// before
const llm = new LLM({ client, maxTokens: 256 })
// after
const llm = new LLM({ client, maxTokens: 4096 }) Defensive patterns
Strategy: fallback
Validate before calling
// keep an eye on prompt+output budget before invoking
const approxInputTokens = estimateTokens(JSON.stringify(request.messages))
if (approxInputTokens + minOutputBudget > modelContextLimit) throw new Error('Prompt too large for safe completion') Type guard
const isContextLengthError = (e: unknown): e is InvokeError => e instanceof InvokeError && e.code === 'CONTEXT_LENGTH'
Try / catch
try {
return await client.invoke(req)
} catch (e) {
if (isContextLengthError(e)) {
return await client.invoke({ ...req, max_tokens: (req.max_tokens ?? 1024) * 4 })
}
throw e
} Prevention
- Set max_tokens generously for tool-calling tasks (tool JSON is verbose)
- Watch token usage fields in responses to detect approaching limits
- Trim conversation history aggressively in long agent loops
When it happens
Trigger: invoke() where max_tokens (or the provider's default output cap) is reached: very long tool call JSON cut off mid-argument; max_tokens set low in config; reasoning models spending the budget on hidden reasoning tokens before emitting content.
Common situations: Default or user-set max_tokens too small for the task; long structured outputs (big JSON tool inputs); models like o1/gemini-thinking burning tokens on reasoning; combined prompt+completion caps on some providers.
AI-assisted analysis of alibaba/page-agent@d02db1ee7c (2026-08-28).
Data as JSON: /api/errors/76836ecc74d5f208.
Report an issue: GitHub.