Diagnosing 401, 403, and 429 Errors by Status Code

    This article explains why you should not immediately switch to a different AI model when facing HTTP errors. It breaks down the standard meanings of 401, 403, and 429 status codes, shows how to read response bodies and headers for deeper clues, and provides a layered troubleshooting process to isolate authentication, permission, or rate-limit issues efficiently.

    Why You Should Not Switch Models First

    A model is only the final step in a request chain. Before inference begins, a request must pass through network connection, address resolution, authentication, permission checks, and traffic control. The errors 401, 403, and 429 map to authentication, permission, and traffic control respectively—none of them are related to the model’s own capabilities.

    Switching models changes only one variable and does not help confirm the root cause. If the error comes from an invalid API key, a different model will still return 401. If the error is caused by a model that is not authorized for your key, switching to another model might temporarily bypass the problem but it will likely reappear in other scenarios. The safer approach is to read the status code first, then the response body, and finally validate each layer step by step.

    Standard Meanings of the Three Status Codes

    HTTP status codes are defined by protocol specifications. The three codes have distinct semantics and cannot substitute for one another.

    Status Code Standard Meaning Common Root Causes Typical Fix Direction
    401 Unauthorized The request lacks valid identity credentials. RFC 9110 requires 401 responses to include a WWW-Authenticate header. Empty or malformed key, expired or revoked key, incorrect header name, environment variable not loaded. Correct or re-issue credentials, check request headers.
    403 Forbidden The server recognizes the requester’s identity but refuses to perform the operation. Key lacks permission for the target model, account status is abnormal, IP or region restriction, overdue payment or incomplete verification. Adjust permissions or account settings, check the egress network.
    429 Too Many Requests The client has sent more requests in a given time window than allowed. Defined by RFC 6585. Requests per minute, token usage, or concurrency exceeds limits; some services also return 429 when quota is exhausted. Lower request rate and concurrency, wait for quota reset, or upgrade the quota.

    In short: 401 means the server does not know who you are, 403 means the server knows you but does not allow the operation, and 429 means you are allowed but you are sending too many requests or consuming too much.

    Note that different services do not always use 403 and 429 in exactly the same way. Some services use 403 to indicate that a model is not enabled, and some use 429 to indicate insufficient balance. Therefore, status codes give only a general direction; the final conclusion should always be confirmed with the response body.

    Read the Response Body and Headers First

    Error Types in the Response Body

    Most APIs return structured JSON on errors, containing an error type and description. A typical structure looks like this:

    1. {
    2. "error": {
    3. "type": "authentication_error",
    4. "message": "Invalid API key provided."
    5. }
    6. }

    The exact field names and values depend on the official documentation of each API. When troubleshooting, you can classify errors semantically:

    • Authentication errors: indicate an invalid, missing, or expired key. They correspond to 401.
    • Permission errors: indicate that a model is unavailable, the account has no access, or the region/network is restricted. They correspond to 403.
    • Rate limit errors: indicate that requests per minute or token usage has exceeded the limit. They correspond to 429 and are usually resolved by waiting.
    • Quota errors: indicate that the quota or balance is exhausted. They may return 429 or 403, and require adding quota or changing the plan.
    • Parameter errors: indicate an incorrect model name or invalid fields. They usually return 400 and can be mistaken for authentication issues.

    The same status code can map to multiple error types. For example, 429 may be either a rate limit or quota exhaustion. You cannot distinguish them by the status code alone—you must read the message in the response body.

    Key Response Headers

    Response headers also provide important clues:

    • Retry-After: may appear in 429 or 503 responses. It tells you how many seconds to wait or a specific time after which to retry. Waiting according to this value is more effective than blind retries.
    • WWW-Authenticate: should be present in a 401 response and explains the authentication scheme. If it is missing, the request may not have reached the expected authentication module—check the endpoint URL.
    • x-request-id or similar tracking fields: most services return a request identifier that helps you locate a specific request in server-side logs or support tickets.

    Layered Troubleshooting Process

    Follow these layers in order. Only move to the next layer after the current one passes, to avoid changing multiple variables at once.

    Layer Zero: Verify the Endpoint and Path

    An incorrect endpoint will send the request to the wrong service, which can appear as authentication failure, a missing path, or abnormal response format. Check these points:

    1. Whether the base URL matches the service associated with your API key. Using a key from one service against the endpoint of another almost always returns 401.
    2. Whether the path is complete—for example, whether a version segment is missing at the end of the base URL.
    3. Whether there are extra slashes or spaces.

    Layer One: Troubleshoot 401

    401 issues usually focus on the credentials themselves and the request header format. Check in this order:

    • Whether the key is empty. If using an environment variable, print its length (not the value) to confirm the variable exists.
    • Whether the key has leading/trailing whitespace or leftover quotes. This is common after copying from a web page.
    • Whether the environment variable was set in the same terminal session that starts the client. If you export the variable in one window and start the program in another, the program cannot read it.
    • Whether the key has expired, been rotated, or been revoked.
    • Configuration file priority. Most tools read system-wide environment variables, user-level configuration, and project-level configuration. Project-level settings may override user-level settings, causing the old key to still be used.
    • Request header format. Most APIs use Authorization: Bearer <key>, but some use a different header name. Always check the official documentation.

    Layer Two: Troubleshoot 403

    A 403 means the identity is recognized; the problem lies in permissions or policy:

    • Whether the model name is within the scope authorized for this key. Model names are case-sensitive, and version numbers must match exactly.
    • Whether the account is in a normal state—check for overdue payments, pending identity verification, or suspension.
    • Whether there is an IP allowlist or region restriction. Switching a VPN, proxy, or office network may change the egress IP and cause the request to be rejected.
    • Whether the key’s permission scope includes the specific API operation you are calling. For example, a key limited to certain features cannot call other functions.

    A practical verification method: using the same key and the same endpoint, send a minimal request with a model name that is explicitly listed as available in the console or documentation. If it still returns 403, the problem is at the key or account level. If it succeeds, the problem is with model authorization.

    Layer Three: Troubleshoot 429

    The key to resolving 429 issues is distinguishing between rate limiting and quota exhaustion:

    • Short-term rate limit: the response body indicates that requests per minute or token usage exceeded the limit. Waiting a short period usually resolves it.
    • Quota exhaustion: the response body indicates insufficient quota or balance. Waiting will not help; check usage statistics and add quota.
    • Concurrency limit: the number of simultaneous requests exceeds the maximum. AI coding tools and automation scripts often send requests in parallel and can easily trigger concurrency limits.
    • Token limit from long context: the longer the context in a single request, the more tokens are consumed, making it more likely to hit the per-minute token limit.

    Handling measures include: respecting the Retry-After header, lowering concurrency, trimming unrelated context, adding a queue and rate control to batch tasks, and deciding whether to upgrade the quota only after confirming the quota status.

    Verify Connectivity with a Minimal Request

    When troubleshooting, use as simple a request as possible to eliminate interference from complex client-side logic. The following command uses the -i flag to output both the status code and response headers:

    1. curl -i "$LLM_BASE_URL/chat/completions