401, 403, and 429 Errors When Connecting to an LLM Compatible API: How to Troubleshoot by HTTP Status Code

    This guide explains what 401, 403, and 429 errors mean when you connect to an LLM API or a compatible endpoint. It covers why switching models rarely fixes these errors and walks through a layered troubleshooting method based on the status code and response body. It includes curl and retry examples and a general checklist.

    After you connect to an LLM API or point a local AI coding tool at a compatible endpoint, you may see 401, 403, or 429 errors in the terminal. Many developers respond by switching to another model, or by reinstalling the client and clearing its cache. These steps sometimes appear to work, but more often they waste time because the root cause was never identified.

    A more reliable approach runs in the opposite order: check the HTTP status code first, then read the error type and description in the response body. These three status codes have different meanings in HTTP, and they cannot substitute for one another. The sections below follow the troubleshooting order.

    1. Why Switching Models Doesn’t Help

    Switching models is often ineffective because 401, 403, and 429 errors almost always occur at the authentication, authorization, and rate-limiting layers, not in model inference. In other words:

    • The request may never reach the model service, because a gateway or authentication step rejected it.
    • The request may reach the service, but the account’s quota or permissions do not meet the requirements.
    • Only a few cases relate to the model itself, such as a 404 for a misspelled model name or a 400 when the context length is exceeded.

    Starting with model selection therefore points the investigation in the wrong direction. A better approach is a layered model: network → authentication → authorization → rate limiting → model. Use the status code at each layer as the signal for that layer.

    2. 401 Unauthorized: Authentication Failed

    The official meaning of 401 is “unauthenticated”: the server cannot confirm who you are. Common causes include:

    1. Incorrect or expired API key: The key was revoked or expired, or spaces or line breaks were copied along with it.
    2. Incorrect request header format: The key is placed in the wrong header field, or the Authorization header value does not match the format the endpoint requires, such as a missing prefix.
    3. Environment variable not loaded: The variable is set in the terminal, but the current shell session or client process did not load it, so requests go out with an empty key.

    Troubleshooting steps:

    1. # Confirm the process can read the key (do not print the key to public logs)
    2. echo ${API_KEY:+key_loaded}
    3. # Verify authentication with a minimal request, bypassing the client wrapper
    4. curl -s -o /dev/null -w "%{http_code}\n" \
    5. -H "Authorization: Bearer $API_KEY" \
    6. -H "Content-Type: application/json" \
    7. -d '{"model":"...","messages":[{"role":"user","content":"hi"}]}' \
    8. https://your-endpoint/v1/chat/completions

    If this minimal request also returns 401, the problem lies in the key or the request headers, not in the client tool.

    3. 403 Forbidden: Identity Known, but Access Denied

    The key difference between 401 and 403 is this: the server knows who you are, but you do not have permission to access the resource. Typical scenarios include:

    1. The account has not been granted access to the model or region, or the account is still under review or on an allowlist.
    2. The key’s scope is limited: For example, the key only authorizes certain endpoints, but it is used to call an endpoint it does not cover.
    3. IP or source restrictions: The server applies an access control policy, and the current outbound IP address is not in the allowed range.
    4. Content safety policy blocks: Some platforms check requests for policy compliance and return 403 when a rule is triggered.

    When troubleshooting, focus on two things. First, check the account and key permissions in the provider’s console. Second, call another endpoint with the same key that you know works. If the other endpoints succeed and only one returns 403, the problem is most likely authorization for that specific resource.

    4. 429 Too Many Requests: Rate Limit Triggered

    A 429 means the request itself is valid, but it exceeded a frequency or quota limit. This is the most benign of the three errors. It usually does not require configuration changes, only changes to how the client calls the API.

    1. Check the response body and headers: A well-designed service includes an error type in the 429 response, such as rate_limit_exceeded, and often a suggested wait time in the Retry-After header. Follow that guidance and wait instead of retrying immediately.
    2. Distinguish RPM from TPM: Some limits apply to requests per minute, and others apply to tokens per minute. Long-context requests can trigger a 429 through total token volume even when the request count is low.
    3. Apply backoff and throttling on the client side: Implement retry logic with exponential backoff and jitter, and cap concurrency:
    1. import time, random
    2. def retry_with_backoff(fn, max_retries=5):
    3. for i in range(max_retries):
    4. resp = fn()
    5. if resp.status_code != 429:
    6. return resp
    7. wait = min(2 ** i, 60) + random.uniform(0, 1)
    8. time.sleep(wait)
    9. raise RuntimeError("rate limited, retries exhausted")
    1. Consider a quota increase: If 429 errors happen all the time rather than occasionally, your usage has likely outgrown the current quota. Request a higher limit instead of retrying without limit.

    5. A General Troubleshooting Checklist

    Here is the process in brief:

    1. When you get an error, first record the full status code and the JSON response body, not just the one-line summary in the terminal.
    2. Reproduce the request with curl or a minimal script to rule out interference from the client wrapper.
    3. For 401, check key validity and request header format. For 403, check account permissions, IP policies, and resource authorization. For 429, check request frequency, token quota, and retry strategy.
    4. Check whether local proxy, timeout, and retry settings are making the problem worse. For example, immediate high-concurrency retries after a failure will increase 429 errors.
    5. Only after ruling out these causes should you suspect the model name, parameters, or the service itself. At that point, check the service status page or contact support.

    6. Summary

    401, 403, and 429 mean, respectively, that the server has not confirmed who you are, that you are not allowed to access the resource, and that you are sending requests too frequently. Each requires a different fix. When you hit a connection error, resist the urge to switch models or reinstall the tool. Work through the status codes layer by layer, and you can often find the root cause within minutes. Read the response body first, reproduce the problem with a minimal request, and change configuration last. Building this habit is worthwhile for any developer who connects to LLM services.