Beginner Q&A: Why did my API return 431 instead of a bad-key error?
HTTP 431 means request headers are too large. Common causes include oversized cookies, long Referer values, or excessive custom metadata. Separate header-size failures from authentication errors, then reduce the request safely and identify which hop returned the response.
The short answer
431 Request Header Fields Too Large means a server refused to process a request because its HTTP header fields were too large. It does not mean the API key is necessarily wrong, and it does not mean the model context window was exceeded. A server may say which header is too large; a client can resubmit after reducing the headers. Common triggers include a long Referer and too many accumulated Cookie values.1
For API users, first separate headers from the request body. A key usually travels in the Authorization header, while prompts, messages, and file data usually travel in the body. A 431 points to the header side. An oversized body may produce a different error, so do not treat every 4xx response as an authentication failure.
What can trigger it
1. Putting too much data in custom headers
Some applications place conversation state, trace metadata, tool lists, or large JSON blobs in custom X-* headers. Headers are better for short metadata such as authentication, content type, and request IDs, not for transporting the body. Putting business data in the request body is easier to maintain and more likely to match the server's expected protocol.
2. Large cookies or Referer values
In browser environments, cookies can accumulate as multiple systems add them; long page URLs can also be sent as a Referer. MDN lists cookies and Referer values among common causes of 431.1 A pure server-side API client usually does not attach browser cookies automatically, but a request passing through a browser extension, proxy, or shared HTTP client may still carry extra headers.
3. Duplicate authorization or tracing headers
A custom wrapper may set Authorization, traceparent, or a debug header both globally and per request, creating duplicates or unexpected growth. Do not put a full API key in the URL, logs, or multiple headers. Keep only the single credential the target API requires.
Find which hop rejected the request
An API request may pass through a local client, corporate proxy, gateway or relay, and upstream provider. Each hop can have different limits and error pages. Investigate in this order:
- Identify the response source. Check server identifiers, the error JSON shape, request ID, and timestamp. An SDK message such as “request failed” is not enough. An HTML page generated by a proxy may not be the model API's original response.
- Measure header names and byte lengths without recording secrets. Record header names or lengths only, and redact
Authorization. Do not write keys, cookies, or prompts into shared logs just to debug.
- Run a minimal comparison request. Use a clean HTTP client with only
Authorization,Content-Type, and required fields. If it succeeds, restore custom headers one at a time until the trigger appears.
- Inspect client defaults. Browser cookies, automatically added Referer values, trace fields accumulated during retries, and SDK middleware are worth checking. Server-side clients can explicitly disable browser-related headers they do not need.
- Do not retry an unchanged request blindly. Repeating the same oversized headers will not fix the problem. Reduce, clear, or correct the logic that creates them first.
How it differs from 401 and 413
401 Unauthorizedusually means the request lacks valid authentication credentials. Check whether the key exists, is sent asAuthorization: Bearer ..., and has not been revoked.431means the headers are too large. A valid key can still be rejected if extra cookies or custom headers exceed the server's limit.413 Content Too Largeconcerns an oversized request body. Shortening a prompt to fix 431 is the wrong first move if the headers are the problem.
A status code only suggests a problem category. Check the actual error and the route the request took. If the same request behaves differently across network environments, look for a proxy adding cookies, authentication fields, or tracing metadata.
A minimal check for SalpX users
SalpX's official documentation publishes https://www.salpx.com/v1 as the Base URL and sends API keys in Authorization: Bearer <API Key>.2 If an SDK request returns 431, try a minimal command-line request that carries no browser state and verify that it sends only required headers. Never paste a real key into a forum, ticket, or screenshot; use a local environment variable and mask it in logs.
If the minimal request still returns 431, record the time, request ID, response status, and header names, then report the issue to the service provider. Do not send secret values or the full business payload. If the minimal request succeeds, restore the SDK's extra headers one at a time. Start with cookies, long Referer values, duplicate authentication fields, and unnecessarily large metadata.
Remember
A 431 does not mean the model rejected your content. It means some layer thinks the “envelope” is too thick. Identify the responding hop, measure headers, run a minimal request, and restore fields one by one. Shortening the prompt, replacing the key, or retrying repeatedly usually misses the cause.
Sources & attribution
This article references the public materials listed below. Analysis and wording are SalpX's own; quoted material remains the property of its original authors and publishers.
- 431 Request Header Fields Too Large - HTTP — MDN Web Docs · accessed 2026-11-28Note:MDN's official reference defines 431 as request headers being too large, with common causes including oversized cookies or Referer values, and recommends resubmitting with smaller headers.
- SalpX API Reference — SalpX · accessed 2026-11-28Note:SalpX's official documentation provides Bearer authentication and the Base URL used in the minimal request checks in this article.
One key for Claude, GPT and 50+ models
The SalpX gateway is OpenAI-SDK compatible. Point base_url at https://www.salpx.com/v1 and you are done.