Fix Claude API 413 request_too_large
When the Claude API returns 413 request_too_large or Claude Code stops at "Request too large (max 32MB)", learn what the limit counts and shrink the request.
API Error: 413 {"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}The request body is over the byte limit. This output comes from issue anthropics/claude-code#14447; the official API docs list the same error as 413 - request_too_large: Request exceeds the maximum allowed number of bytes. It is not the context-window error prompt is too long. This one is rejected on bytes, before any tokens are counted.
[1] Check the limit for the endpoint you called. The official request size limits:
| Endpoint | Maximum request size |
|---|---|
| Messages API | 32 MB |
| Token Counting API | 32 MB |
| Batch API | 256 MB |
| Files API | 500 MB |
The docs say exceeding a limit returns 413 request_too_large, and that on the direct Claude API, Cloudflare returns this error before the request reaches the API servers. They do not say whether MB means 1,000,000 or 1,048,576 bytes.
[2] Find out why the body is large. The response says only Request exceeds the maximum size, so it does not tell you which block was too big, and issue #14447 gives no cause either. Suspects, in order:
- Images and PDFs piled up in the conversation history
- Long pasted text or a huge tool result (
tool_result) - Files embedded as base64
Base64 writes 3 bytes as 4 characters, so the data grows by about a third. Measured:
import base64, os raw = os.urandom(30 * 1024 * 1024)enc = base64.b64encode(raw)print(f"raw {len(raw):>10} bytes ({len(raw) / 1e6:.1f} MB)")print(f"base64 {len(enc):>10} bytes ({len(enc) / 1e6:.1f} MB)")$ python b64size.pyraw 31457280 bytes (31.5 MB)base64 41943040 bytes (41.9 MB)A 31.5 MB (30 MiB) original, under the 32 MB limit, became 41.9 MB after base64. Whichever unit MB means, a file under the limit can still push the request over it once encoded.
[3] Upload big files once with the Files API and refer to them by file_id. Per the official Files API docs, a file can be up to 500 MB, and a Messages request references the file_id instead of re-sending the content. Images, PDFs, and plain-text files (text/plain) are supported.
{ "type": "document", "source": { "type": "file", "file_id": "file_..." }}The file content still counts as input tokens. A file larger than the context window fails with a 400 even when referenced by file_id (see the Files API error list).
[4] Cut what one request carries. Lower the resolution of images, send only the needed part of long text, or split the work across requests. The Batch API limit for bulk jobs is 256 MB.
[5] In Claude Code, follow the Request too large entry in the official error reference. The message reads:
Request too large (max 32MB). Accumulated images and attachments in the conversation pushed the request over the limit. Run /compact, or double press esc to go back and remove attachments.The docs describe two variants:
Request too large (max 32MB; 20.1MB of about 33.4MB is images or documents).Images or documents caused it, and Claude Code retries with them stripped.Request too large for the API's 32MB request limit: the messages alone are over the limit. Per the docs the message also sayscompacting cannot make it fit, and there is no automatic retry.
Then follow the docs in order. If you see compacting cannot make it fit, press Esc twice to go back past the turn that added the large content, or run /clear (which discards the whole conversation context, so try Esc first). Otherwise run /compact, which drops accumulated images and attachments. For big files, give Claude the path instead of pasting the content so it can read them in chunks. The docs say that before v2.1.212, enough accumulated images made every turn fail, and before v2.1.229 the attachment advice showed even when compacting could not help, so update if your version is older.
[6] Do not confuse it with an image-size error. A single image over 8000 pixels on its longest edge (3000 when more than 20 images are in context) is also rejected, but as a 400 in the Image was too large family, not a 413.
$ wc -c < big.json34603093$ curl -sS -o - -w '\nHTTP %{http_code}\n' https://api.anthropic.com/v1/messages -H "x-api-key: sk-ant-fake" ... --data-binary @big.json{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"},"request_id":"..."}HTTP 401Related: for a full context window, see Claude Code Forgets Rules in Long Chats: /compact vs /clear.