AI App Authentication: API Keys, OAuth and Rate Limits for LLM Services
AI app authentication: API keys for server calls, OAuth with PKCE for user access, and rate-limit handling that tells a real limit from a spend cap.
By Galactic News Editorial · Published 2026-11-18T07:29:37+00:00 · Updated 2026-11-18T07:29:37+00:00
AI App Authentication: API Keys, OAuth and Rate Limits for LLM Services
Every AI app answers three questions before its first model call. How does it prove who it is? How does it act for a user? What happens when the provider says slow down? The answers are API keys, OAuth with PKCE, and rate-limit handling. This guide covers all three, with the mistakes that cost teams the most.
API keys: the server-to-server credential
An API key is a long secret string. It identifies your account to the LLM provider. Every request carries it as a bearer token in the Authorization header. OpenAI keys belong to an organization and a project. You create them in the provider dashboard. The provider shows the full key once. After that, it never shows it again.
Treat a key like a database password. Never commit it to git. Never hardcode it in source. Never log it. Never ship it in client-side JavaScript. A leaked key is the most common way teams get a surprise bill. Attackers scan public repos for keys. Providers scan too, and may auto-disable a leaked key.
Store keys in environment variables on the server, or in a secrets manager. Use one key per environment: dev, staging and production. That way you can revoke one without breaking the others. Use project-level keys where the provider supports them. They scope usage and spend limits to one app, which is safer than a single organization-wide key.
Set spend limits in the provider dashboard. They act as a hard backstop against runaway usage. A bug in a retry loop can burn through a budget overnight. A cap stops it.
OAuth 2.0 with PKCE: when your app acts for a user
API keys work when your server calls the model. OAuth fits a different case. Your app needs to act on a user's behalf, with the user's own account and permissions. OAuth 2.0, defined in RFC 6749, is an authorization framework. It lets an app get limited access to a user's resources without ever seeing the user's password.
The recommended flow is the authorization code flow with PKCE. PKCE stands for Proof Key for Code Exchange. It is defined in RFC 7636. It began as a fix for mobile apps that cannot keep a secret. It is now the baseline for web, mobile, single-page and CLI apps alike.
The flow has five steps. First, the client creates a random code verifier, 43 to 128 characters long. It hashes the verifier to make a code challenge. Second, the client sends the user's browser to the authorization server with the challenge. Third, the user signs in and approves the requested access. Fourth, the server redirects back with a short-lived authorization code. Fifth, the client exchanges the code for tokens in a direct server-to-server request, sending the original verifier. The server checks the verifier against the challenge. An attacker who intercepts the code cannot redeem it without the verifier.
Tokens never travel through the browser in this flow. The code exchange happens back-channel, server to server. That is the point. The browser carries a code, not a credential.
Two grants are deprecated and should not be used in new apps. The implicit grant puts the access token in the URL fragment, where it can leak. The resource owner password grant asks the user to hand their password to your app. Use authorization code with PKCE instead. The OAuth 2.1 draft makes PKCE required for all clients and drops the implicit flow entirely.
How API keys and OAuth compare
| Question | API key | OAuth 2.0 with PKCE |
|---|---|---|
| Who is authenticated? | Your app or account | A specific user, through your app |
| Where is the secret kept? | Your server or secrets manager | No long-lived secret in the client; short-lived tokens only |
| Best for | Server-side model calls, background jobs | Apps acting on a user's behalf |
| Revocation | Delete or rotate the key | Revoke the refresh token or the grant |
| Main risk | Leaked key, runaway spend | Redirect or code interception without PKCE |
Many AI apps need both. The server uses its own API key to call the model. Users sign in with OAuth so the app can read their files or send on their behalf. Keep the two apart. Never use a user's OAuth token to pay for model calls. Never use your API key to prove a user's identity.
Rate limits: RPM, TPM and the three kinds of 429
LLM providers limit how fast you can call them. OpenAI measures requests per minute and tokens per minute. Limits vary by account tier and model. Anthropic measures three axes per model class: requests per minute, input tokens per minute and output tokens per minute. Exceed any one and you get a 429.
A 429 is not one error. It is three different problems wearing the same number. First, a real rate limit. The response includes a Retry-After header. Wait that long, then retry. Second, a monthly spend cap. Anthropic returns a 429 with no Retry-After header and an error code for an enforced spend limit. Retrying cannot work until the first of the next month, or until the tier changes. Third, provider overload. Anthropic returns a 529 overloaded error. OpenAI returns a 500 or 503. These mean the provider is saturated, not your account.
Branch on the error code before you retry. A retry loop that keys only on the status code will hammer a spend-cap 429 for a full billing cycle. That fills logs with failures and can trigger the same error across the account. Check for the spend-cap code first. If it is there, stop. Alert a human. Do not retry.
For real rate limits, respect the Retry-After header. If it is missing, use exponential backoff with jitter. Wait a short time, then double the wait on each retry, with a random jitter so clients do not retry in sync. Cap the retries. A request that never gives up blocks the user forever.
Which errors to retry, and which to never retry
Retry the transient errors. A 429 for rate, a 500, a 503 and a 529 all clear on their own. Wait, then try again. Never retry the permanent errors. A 400 means the request is malformed. A 401 means the key is wrong or revoked. A 403 means the account lacks access to the model. A 413 means the payload is too large. Retrying these only repeats the failure.
Never retry a spend-cap or exhausted-credit 429. OpenAI has a separate 429 for exhausted credit. Anthropic's spend-cap 429 has no Retry-After header. Both mean the same thing: the money is gone. Fix the billing or the cap, then retry.
Watch for one subtle case. With streaming over server-sent events, an error can arrive after a 200 response. A retry loop that only looks at the HTTP status will miss it. Handle stream errors in the stream handler, not only in the HTTP layer.
Putting it together: a production checklist
- Separate keys per environment. Dev, staging and production each get their own key. Revoke one without touching the others.
- Store keys in a secrets manager. Never in git, never in logs, never in client code. Rotate keys at once if a leak is suspected.
- Set spend caps. A per-key or per-project cap stops a retry bug from draining the budget.
- Use OAuth with PKCE for users. Authorization code flow, PKCE always, short-lived access tokens, refresh token rotation.
- Build a smart retry layer. Respect Retry-After. Branch on the spend-cap error code. Back off with jitter. Cap retries. Fall back to another provider on a 529.
- Log errors, never secrets. Redact keys and tokens from every log line. A key in a log is a leaked key.
Bottom line
Use API keys for your server's own model calls. Use OAuth with PKCE when your app acts for a user. Treat the two as separate systems. For rate limits, read the error body, not just the status code. A 429 with a Retry-After header means wait and retry. A 429 without one means the spend cap is hit, and no amount of retrying will fix it. Build that branch into your retry logic on day one. It is the difference between a brief slowdown and a month of failed calls.