Skip to content

fix(coding-agent): recover from 413 request too large#1936

Closed
ogulcancelik wants to merge 1 commit intobadlogic:mainfrom
ogulcancelik:fix/request-too-large-image-pruning
Closed

fix(coding-agent): recover from 413 request too large#1936
ogulcancelik wants to merge 1 commit intobadlogic:mainfrom
ogulcancelik:fix/request-too-large-image-pruning

Conversation

@ogulcancelik
Copy link
Copy Markdown
Contributor

Anthropic returns HTTP 413 request_too_large when the request payload exceeds 32 MB. I could reproduce this consistently in image-heavy sessions where context token usage was still relatively low (~30%), but the request body had grown too large because conversation history contained many base64-encoded images.

I couldn’t reproduce the same failure on OpenAI models, likely because their effective payload limits are higher, but 413 is a standard HTTP error for this class of problem.

Root cause

Compaction currently tracks token usage, not HTTP request size.

That works for normal text-heavy sessions, but images are a bad mismatch for token-only accounting:

  • images are relatively cheap in tokens
  • images are expensive in bytes once embedded as base64 in message history

So a session can stay far below the context window while still exceeding the provider’s request size limit.

Fix

When a request fails with HTTP 413 / request_too_large:

  • detect it separately from token-based context overflow
  • remove the failed assistant error from in-memory state
  • strip the oldest half of image blocks from context
  • replace stripped images with a text placeholder
  • retry the request
  • repeat up to 3 attempts

This recovery now works even when auto-compaction is disabled.

The stripping logic handles images in:

  • user messages
  • toolResult messages
  • custom messages

Session history on disk is left untouched. Only the in-memory request context is modified for recovery.

Tests

Added tests for:

  • isRequestTooLarge() detection
  • image counting and oldest-first stripping
  • repeated stripping across retries
  • custom messages with images
  • recovery from 413 even when auto-compaction is disabled

@ogulcancelik
Copy link
Copy Markdown
Contributor Author

also should i have opened an issue first ? let me know if you do prefer issue first prs now, just wanted to provide a quick solution.

@badlogic badlogic added the possibly-openclaw-clanker User has activity on openclaw/openclaw label Mar 24, 2026
@badlogic
Copy link
Copy Markdown
Owner

currently refactoring agent session and fixing this will be part of that work.

@badlogic badlogic closed this Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

possibly-openclaw-clanker User has activity on openclaw/openclaw

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants