413 request_too_large: How to Fix "Request too large" in the Claude API and Claude Code
HTTP 413 request_too_large means the request body was bigger, in bytes, than the endpoint accepts. It is not about tokens or your plan quota. As of 2026-11-25, Anthropic's error reference lists a 128 MB limit for the first-party Messages API, while Claude Code reports the same condition as Request too large (max 32MB). Try with a smaller file. In practice the cause is almost always base64 images, large PDFs, or a long agent session that keeps resending everything it has read.
QCode is an independent third-party API service. It is not affiliated with or endorsed by Anthropic or OpenAI. Limits below are quoted from Anthropic's and Claude Code's own documentation, with the read date.
Updated 2026-11-26
Key facts
- 413 = bytes, not tokens. Anthropic documents 413 as
request_too_large: "Request exceeds the maximum allowed number of bytes." A token overflow is a different error ("Prompt is too long", a 400). - Documented size limits (Anthropic, read 2026-11-25): Messages API 128 MB, Token Counting API 128 MB, Batch API 256 MB, Files API 500 MB. On the first-party API, Cloudflare returns the 413 before the request reaches Anthropic's servers.
- Claude Code wording:
Request too large (max 32MB). Try with a smaller file.andRequest too large for the API's 32MB request limit. Older builds showedRequest too large (max 20MB). Image-specific:Image was too large. Double press esc to go back and try again with a smaller image. - Image limits: up to 8000×8000 px and 10 MB per base64 image on the Claude API; more than 20 images in one request triggers a stricter per-image dimension limit. Images larger than the model's native resolution (2,576 px long edge on Claude 4.7 and later) are downscaled anyway, so sending bigger files only adds bytes.
- PDF limits: 600 pages per request (100 when the context window is under 1M tokens), standard unencrypted PDF, and the whole payload must fit the request size limit.
- Gateways may differ. Any proxy or API gateway between you and the model, QCode included, can enforce its own body limit. QCode does not publish its own request body limit as of 2026-11-26; if you need the exact ceiling, measure it with your own requests.
What the error means
Every Messages API call carries the full conversation: system prompt, tool definitions, every prior turn, every tool result, and every image or document block encoded as base64. Base64 adds roughly one third to the raw file size. Nothing is "remembered" on the server between calls, so a screenshot pasted on turn 3 travels again on turn 4, 5 and 40.
That is why 413 often appears suddenly in the middle of a session that worked a minute ago. The last message was small; the accumulated history was not.
You may see the error in different shapes depending on the client:
| Where | What you see |
|---|---|
| Raw HTTP / curl | HTTP status 413, error type request_too_large |
| Anthropic Python / TypeScript SDK | A status error with status_code == 413; some SDK versions also define a dedicated ContentTooLargeError, so check your version's exception list |
| Claude Code CLI | Request too large (max 32MB). Try with a smaller file. |
| Claude Code, pasted image | Image was too large. Double press esc to go back and try again with a smaller image. |
| Other IDE agents (Cline, Roo, Cursor BYOK) | Usually the raw 413 text or an "API request failed" banner |
Do not confuse it with these neighbours:
Prompt is too long/Input is too long for requested model: too many tokens for the context window. See the context length exceeded guide.invalid_request_errormentioning "many-image requests": more than 20 images and at least one exceeds the stricter dimension cap. That is a 400, not a 413.
Causes, ranked by how often they happen
- Screenshots and photos pasted into an agent session. A 4K PNG screenshot can be several megabytes; ten of them in history, resent every turn, adds up fast. Browser or computer-use MCP tools that return screenshots as tool results are the same problem.
- Large PDFs sent inline as base64. A scanned 300-page PDF can be tens of megabytes even before encoding.
- The agent read a huge file. Minified bundles, lockfiles, CSV exports, log files or a
dist/folder pulled into context through a file-read tool. Each read is stored as a tool result and resent. - Very long sessions with many tool results. Even pure text adds up after hundreds of turns, especially with verbose test output.
- Your own code builds a giant request. Batch-style scripts that concatenate whole repositories or many images into one Messages call.
- A gateway with a lower limit. A corporate proxy, an API gateway or a relay may reject bodies well below the vendor's limit.
Step-by-step fixes
Claude Code
- Get out of the oversized turn. If an image caused it, press Esc twice as the message says and resend without the image. Otherwise run
/rewind(or press Esc twice) to step back to a checkpoint before the big content was added. - Shrink the history.
/compactsummarises the conversation; images and bulky tool results do not survive the summary as raw bytes. If compaction itself fails, start fresh with/clearor a newclaudesession. Note that resuming the same session later resends the same history. - Resize images before pasting. Keep the long edge at or below about 2,000 px and prefer JPEG or WebP for photos:
# macOS
sips -Z 2000 screenshot.png --out screenshot-small.png
# ImageMagick 7 (Linux / macOS / Windows)
magick screenshot.png -resize "2000x2000>" -quality 85 screenshot.jpg
- Split PDFs. Send only the pages you need:
qpdf big.pdf --pages . 1-50 -- part1.pdf
- Keep the agent away from giant files. Add deny rules in
.claude/settings.jsonso Claude Code cannot read build output or generated data:
{
"permissions": {
"deny": ["Read(./dist/**)", "Read(./node_modules/**)", "Read(./**/*.min.js)", "Read(./data/**/*.csv)"]
}
}
For logs, ask Claude to use grep, head or tail instead of reading the whole file.
- Check where requests go. Run
/statusto confirm theAnthropic base URL. If you use a gateway such ashttps://api.qcode.cc/api, a much lower body limit there produces the same symptom; see Claude Code gateway errors.
Codex CLI
Codex sends the whole thread through the Responses API in the same way, so images attached with -i and long tool output accumulate too. Use /compact in the TUI to summarise, start a new thread with /new when a task changes, and resize images before attaching them. On QCode, Codex talks to https://api.qcode.cc/openai with wire_api = "responses"; Claude models are not available on that path.
Anthropic SDK (Python / TypeScript)
- Catch the error explicitly so you can shrink and retry once instead of failing a whole job:
import os, anthropic
client = anthropic.Anthropic(
base_url="https://api.qcode.cc/api",
api_key=os.environ["QCODE_API_KEY"],
)
try:
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=messages,
)
except anthropic.APIStatusError as e:
if e.status_code == 413:
messages = drop_old_images(messages) # your trimming function
msg = client.messages.create(model="claude-sonnet-5-5", max_tokens=1024, messages=messages)
else:
raise
- Downscale images in code before encoding:
from PIL import Image
img = Image.open("page.png")
img.thumbnail((2000, 2000)) # keeps aspect ratio
img.convert("RGB").save("page.jpg", quality=85)
- Do not resend old images. Replace image blocks from earlier turns with a short text note ("screenshot of the login form, already analysed") once the model has answered about them.
- Use file references where supported. Anthropic's Files API lets you upload once and reference a
file_id, which keeps every later request small. Check that your provider or gateway supports it before relying on it; QCode does not document Files API or Message Batches support as of 2026-11-26, so test it with a small request first. - Split work across requests. One request per document or per batch of 10–20 images is easier to retry than one huge call.
IDE plugins (Cline, Roo Code, Cursor with your own key)
- Start a new task instead of continuing a thread that has many screenshots.
- Remove pasted images from the input before resending.
- Add generated folders to the extension's ignore settings so the agent does not read them.
- If the plugin shows a raw 413, the fixes above apply unchanged; the plugin is only relaying the API's answer.
How to confirm it is fixed
- Measure the payload yourself. Save the request body to
body.jsonand check its size:
wc -c body.json
curl -sS -o /dev/null -w '%{http_code} upload=%{size_upload}\n' \
-X POST https://api.qcode.cc/api/v1/messages \
-H "x-api-key: $QCODE_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
--data-binary @body.json
A 200 with a smaller size_upload confirms the fix. A 413 at a size well under 128 MB points to a gateway or proxy limit.
2. In Claude Code, send a normal prompt after /compact; if it answers, the session is below the limit again. /context shows token usage (not bytes), which helps you spot sessions that are growing too fast.
Prevention checklist
- Resize every screenshot before it enters an agent session.
- Prefer text over images when the content is text (paste the error message, not a photo of it).
- Deny-list build output, dependencies and data folders for the agent.
- Compact or start a new session when a task changes.
- In your own code, log request body size and alert above a threshold you choose, well before any vendor limit.
FAQ
What is the maximum request size for the Claude API?
Anthropic's errors page (read 2026-11-25) lists 128 MB for the Messages API and Token Counting API, 256 MB for the Batch API and 500 MB for the Files API on the first-party API. Other platforms and gateways can set their own, lower limits.
Why does Claude Code say 32MB when the API page says 128 MB?
The message quoted from Claude Code names 32MB, while Anthropic's errors page documents 128 MB for the Messages API (read 2026-11-25). We cannot confirm which figure current Claude Code builds enforce client-side, so keep requests well below both.
Is 413 the same as "Prompt is too long"?
No. 413 is about bytes on the wire, usually images or PDFs. "Prompt is too long" is about tokens exceeding the model's context window and is fixed by compacting or trimming text.
Why did the error appear after the session had been working fine?
Each request resends the whole history. An image pasted many turns ago is still in every new request, so the total crosses the limit even though your latest message is tiny.
Will /compact remove images from history?
Compaction replaces the conversation with a summary, so earlier image bytes are no longer sent. If the request is too large for compaction to run, use /rewind to an earlier checkpoint or start a new session.
Can a gateway like QCode cause a 413 on its own?
Any proxy in the path can enforce its own body limit. If you get 413 on a request far below 128 MB, test the same body against the gateway with curl and contact its support with the request size. QCode does not document the body it returns for an oversized request as of 2026-11-26, so check what your client actually receives.
Does resizing images reduce cost as well?
Usually yes. Claude bills images by visual tokens, and images above the model's native resolution are downscaled anyway, so a smaller upload costs the same or fewer tokens and far fewer bytes.