Skip to content

Comments

Handle 413 context overflow errors gracefully#395

Closed
alejandroOPI wants to merge 1 commit intoopenclaw:mainfrom
alejandroOPI:fix/handle-413-context-overflow
Closed

Handle 413 context overflow errors gracefully#395
alejandroOPI wants to merge 1 commit intoopenclaw:mainfrom
alejandroOPI:fix/handle-413-context-overflow

Conversation

@alejandroOPI
Copy link

Problem

When the conversation context exceeds the model's limit, the gateway returns a raw 413 error that makes the assistant completely unresponsive. Users have no indication of what went wrong or how to fix it.

This happened to me on Jan 6-7, 2026 - the assistant was stuck returning 413 errors for hours until I manually cleared the context.

Solution

This PR adds graceful handling of context overflow errors:

  1. New helper function isContextOverflowError() in pi-embedded-helpers.ts detects various forms of the error (413, request_too_large, context length exceeded, etc.)

  2. User-friendly error message in formatAssistantErrorText() returns a helpful message instead of raw JSON

  3. Early return in runner - When session.prompt() throws a context overflow error, we return a helpful response instead of propagating the error

Changes

  • src/agents/pi-embedded-helpers.ts: Added isContextOverflowError() function and updated formatAssistantErrorText() to detect and handle 413 errors
  • src/agents/pi-embedded-runner.ts: Import the new helper and catch context overflow errors in the prompt execution path

Testing

  • Build passes (pnpm build)
  • Error detection covers: request_too_large, request exceeds the maximum size, context length exceeded, maximum context length

Future Improvements

This is a minimal fix. A more complete solution could:

  • Automatically truncate old messages and retry
  • Proactively warn when approaching context limits
  • Write a summary to memory before truncating

Addresses #394

When the conversation context exceeds the model's limit, instead of
throwing an opaque error or returning raw JSON, we now:

1. Detect context overflow errors (413, request_too_large, etc.)
2. Return a user-friendly message explaining the issue
3. Suggest using /new or /reset to start fresh

This prevents the assistant from becoming completely unresponsive
when context grows too large (e.g., from many screenshots or long
tool outputs).

Addresses issue openclaw#394
@steipete
Copy link
Contributor

steipete commented Jan 7, 2026

Landed on main via fast-forward. Commits: d5547122 (context overflow handling + helper tests), 43f72aea (tool errors concise, requested), 24ee7c47 (changelog + clawtributors credit for PR #395).\n\nGate: pnpm lint && pnpm build && pnpm test && pnpm docs:list

steipete added a commit that referenced this pull request Jan 7, 2026
@steipete steipete closed this Jan 7, 2026
dgarson added a commit to dgarson/clawdbot that referenced this pull request Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants