When a request fails
Read an error response, tell a key problem from a credit problem from a rate limit, and fix each one.
Your shop assistant has been answering questions for a week. This morning, customers see a blank reply. Your server logs show the request went out and something came back, but not an answer.
When a request fails, the response returns an error object with a numeric code and a message instead of choices. The HTTP status matches error.code. Three codes cover most failures you’ll see:
{
"error": {
"code": 402,
"message": "Insufficient credits. Add more using https://openrouter.ai/settings/credits"
}
}- 401 means the key is the problem: missing, mistyped, disabled, or deleted. Check the
Authorizationheader and the key’s status on the keys page. - 402 means the account or key has run out of credits, or the key hit its spending limit. Add credits on the credits page or raise the key’s limit. Exception: if your balance is positive and
metadata.reasonisin_flight_budget_exhausted, too many paid requests are running concurrently. Wait for theRetry-Afterduration and retry (see Rate limits). - 429 means you’re sending requests faster than allowed. It can come from OpenRouter or from the provider serving the model. Wait and retry, with a longer wait each time.
- Key401
- Credits402
- Rate limit429
- Rate limit429
Check your understanding
Question 1 of 2The response says "code": 402 with the message Insufficient credits. Customers are still asking questions. What do you do first?
Read the message too. It often says exactly which of these happened and what to do. The errors reference lists every code.
Apply it
Everything works on your laptop. You deploy to your server, and the first request returns a 401. What’s the most likely cause?
The server doesn’t have the key. On your laptop, OPENROUTER_API_KEY is set in your terminal; on the server, it’s empty or missing, so the request goes out without a valid Authorization header. Set the variable in your host’s secret settings and restart the app.
If the variable is set, check that the key on the keys page is still enabled and hasn’t been deleted.
Takeaway
Read the code before you change anything. 401 is the key, 402 is credits, 429 is speed. Retrying helps a 429, or a 402 that says in_flight_budget_exhausted, and only with a wait between tries.