Skip to content

401 API Key Error: Causes and How to Fix It

Updated · TokenomixAI

A 401 API key error means the server rejected the credentials you sent, not that the model or the network failed. Common causes are a mistyped key, a trailing space, an incomplete Authorization header, or a key your terminal never read. This page walks through each cause in order.

A 401 is usually about how the key is sent, so check the key contents, the shape of the Authorization header, and where your terminal or config file reads the key before you blame quota.

What Does a 401 API Key Error Mean?

A 401 status means the server doesn't recognize the credentials you sent. The request is rejected before any model is called, so switching models or waiting a few minutes won't change the result. Everything worth checking sits on the client side: the key contents, the header shape, and how the process reads the key.

This error is different from 429. The TokenomixAI rate limit is 600 requests per minute per account, shared by all keys in that account, and above it the API returns 429 with Retry-After. A 401 doesn't carry a wait time, because it isn't about request speed.

What you seeWhat it meansFirst step
401 UnauthorizedThe server doesn't recognize the key you sentCheck the key contents and the Authorization header
429 Too Many RequestsAbove 600 requests per minute per accountWait for Retry-After and reduce parallel requests
status is exhausted on the quota endpointTokens are used upTop up; the key stays the same

Key Typed Wrong, Truncated, or With a Stray Space

A TokenomixAI key starts with sk-tkx-. If the first characters you see look different, the thing you copied is probably not the key, but some other piece of text from the payment page.

Copying from chat, email, or a browser often drags invisible characters along: a trailing space, a newline, or quotation marks that get pasted with the key. The server reads the string as it is, so a single space after the key is enough to make it unrecognized.

Truncated keys also happen in terminals. When you copy from a screen that wraps a long line, the end of the key can stay behind and only the beginning gets pasted.

  • Copy the key again from its original source, not from a screenshot or a message that's been forwarded several times.
  • Make sure there's no space at the start or the end of the key, and no newline at the end.
  • If the key sits in a JSON file, keep it inside the quotes with no extra spaces inside them.
  • Check the key length: a key that stops mid-block was usually cut off during the copy.

The Right Authorization Header and Base URL

The header looks like Authorization: Bearer sk-tkx-.... The word Bearer is required, followed by one space, then the key. Quotes around the key aren't needed, and if they get sent they become part of the value, so the server sees a different key.

For OpenAI-compatible endpoints the base URL is https://api.tokenomixai.com/v1. Chat completions, responses and quota all live under that address. One wrong character in the path can send the request somewhere else, so copy the base URL as it is.

curl https://api.tokenomixai.com/v1/chat/completions \
  -H "Authorization: Bearer sk-tkx-..." \
  -H "Content-Type: application/json" \
  -d '{"model": "MODEL_ID", "messages": [{"role": "user", "content": "Halo"}]}'

If you use an SDK, put the key string in the API key field and point the base URL at the address above. More on the key format and where it comes from is in OpenAI Compatible API Key.

Beyond the header there are subtler mistakes: the header goes out twice, or a second header with the same name is sent empty. If the library you use adds headers on its own, check the docs.

Key Not Read in the Terminal

An environment variable only lives in the terminal session where it was set. If you export the key in one tab and run the agent in another, or launch it from an app shortcut, the process may not inherit the value and the request goes out with no credentials.

On Windows, a value set with the $env: prefix only lives in the PowerShell window that's open. Closing the window drops it, and the next session needs it set again before you start the agent.

A quick check is to print the variable's value in the terminal before starting the agent. If it comes back empty, truncated, or with a trailing space, you've found the cause without touching the key at all.

  • An empty value means the variable isn't set in that session, not that the key is wrong.
  • A truncated value means the key wasn't copied in full.
  • A trailing space or newline means the copy from the browser brought extra characters along.
  • Quotes at the end of the value get sent as part of it and make the key mismatch.

401 in OpenCode and Claude Code

OpenCode stores providers in ~/.config/opencode/opencode.json, on Windows %USERPROFILE%\.config\opencode\opencode.json. The tokenomixai entry is written as part of the provider object, with the base URL and API key inside that provider's options, using the npm package @ai-sdk/openai-compatible.

{
  "provider": {
    "tokenomixai": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://api.tokenomixai.com/v1",
        "apiKey": "sk-tkx-..."
      }
    }
  }
}

If you edit that file by hand, keep the key inside JSON quotes and make sure the provider structure is still an object, not an array. If the edit goes wrong, restore first and run the installer again so the entry is written cleanly without removing your other providers, MCP servers and plugins.

bash <(curl -fsSL https://tokenomixai.com/install/opencode.sh) --restore

Claude Code uses the Anthropic schema, so its base URL is written as https://api.tokenomixai.com without /v1, because Claude Code adds /v1/messages itself. The auth token is stored as ANTHROPIC_AUTH_TOKEN in the env block of ~/.claude/settings.json, on Windows %USERPROFILE%\.claude\settings.json.

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.tokenomixai.com",
    "ANTHROPIC_AUTH_TOKEN": "sk-tkx-..."
  }
}

A 401 in Claude Code usually means ANTHROPIC_AUTH_TOKEN is empty, still holds an old key, or picked up a space while you edited the file by hand. The background on both values is in Anthropic Base URL and Auth Token in Claude Code, and the setup steps are on the Claude Code page.

Check Quota Before You Change Anything

The quota endpoint returns JSON with the fields status, remainingTokens, maxTokens, usagePercent, expiresAt and low. The low field is true when 10 percent or less of the tokens remain, so you know when to plan a top-up.

curl https://api.tokenomixai.com/v1/quota -H "Authorization: Bearer sk-tkx-..."

If status is exhausted, the tokens are gone and the key won't serve requests until you top up. After a top-up the key stays the same, remaining tokens from earlier purchases are added together, and the validity follows the latest top-up.

Quota is counted in tokens, not in time windows, so the key doesn't stop and restart on a fixed schedule. It keeps working while tokens remain and before it expires. The limit that does apply is 600 requests per minute per account.

Checklist When the 401 Comes Back

Run these steps from the top down before you change anything else. Many cases end at the first two.

  1. Print the key in your terminal and confirm it starts with sk-tkx-, with no spaces, no trailing newline and no quotes.
  2. Confirm the header uses Authorization: Bearer followed by one space and the key, and that no empty header with the same name is sent.
  3. Check the base URL: https://api.tokenomixai.com/v1 for OpenAI-compatible endpoints, and no /v1 for Claude Code.
  4. Call the quota endpoint and read status and remainingTokens.
  5. If you use OpenCode or Claude Code, open the config file and confirm the stored key is the one you're currently using.
  6. If 401 persists, run the installer again with a freshly copied key instead of stacking more manual edits.

For other tools that only give you a base URL field and an API key field, the same rules apply: the address ends in /v1 for the OpenAI schema, and the key goes out in full. Per-tool setup steps live in the docs.

Frequently asked questions

Does a 401 mean my quota is used up?

No. A 401 means the server doesn't recognize the credentials you sent, so the fix is in the key contents and the way it's sent. Remaining tokens are a separate check: the quota endpoint returns status and remainingTokens for that.

Why does the same key work in curl but fail in my app?

Usually the app reads the key from somewhere else, such as an environment variable in a different session or a config file you edited by hand. Check whether a space or a pair of quotes got saved into that file as well.

Do I need to buy a new key after a 401?

Usually not. Fix the key text and the header first, because the same key is reused on top-up and doesn't change. A new key only adds cost without removing the cause.

Where can I see the key Claude Code is using?

It's in the env block of ~/.claude/settings.json as ANTHROPIC_AUTH_TOKEN, next to ANTHROPIC_BASE_URL. On Windows the same file lives at %USERPROFILE%\.claude\settings.json.

Can a wrong base URL cause a 401 instead of a 404?

It can, when the path still reaches an endpoint that checks credentials but the value you sent was altered on the way, for example by a quote or a space. Copy the base URL as it is, with /v1 for OpenAI-compatible endpoints and without it for Claude Code, and that variable is removed.

More articles