Auth diagnostics · production-checked 2026-08-04

Fix API 401 invalid token errors

HTTP 401 on MonkeyAI is usually an authentication problem, not a full outage. Read the JSON code first, fix one credential path, then retest GET /v1/models before touching agent config.

Base URL: https://monkeytheme.com/v1 Codes seen: API_KEY_REQUIRED · INVALID_API_KEY Safe probe: GET /v1/models

What 401 actually means here

A structured 401 means the gateway answered and rejected the credential presentation. That is different from a blank gateway page, a TLS failure, or a 504 upstream timeout.

SignalMeaningFirst move
HTTP 401 + JSON bodyAuth layer rejected the requestInspect code and the Authorization path
HTTP 404 / model not foundAuth may already be fineUse a model id from /v1/models
HTTP 504Upstream or proxy timeoutSee the 504 guide; a new key will not fix it
SDK timeout, no HTTP statusClient stopped waitingCheck network/timeout settings, not key text first

Measured production responses

These probes hit https://monkeytheme.com on 2026-08-04. No real user key was used. Bodies are summarized from live JSON.

Request shapeHTTPcodemessage summary
No Authorization header401API_KEY_REQUIREDAPI key is required in Authorization header (Bearer scheme), x-api-key header, or x-goog-api-key header
Authorization: Bearer with empty token401API_KEY_REQUIREDsame required-key message
Authorization: Basic …401API_KEY_REQUIREDBasic is not accepted; message still asks for Bearer / x-api-key / x-goog-api-key
Authorization: Bearer sk-invalid-test-key-not-real401INVALID_API_KEYInvalid API key
x-api-key: sk-invalid-test401INVALID_API_KEYInvalid API key
x-goog-api-key: sk-invalid-test401INVALID_API_KEYInvalid API key
POST /v1/chat/completions without a key401API_KEY_REQUIREDsame required-key message
Practical split: if the code is API_KEY_REQUIRED, the key never arrived in an accepted header. If the code is INVALID_API_KEY, a value arrived and failed validation. Do not rotate keys to “fix” a missing header.

Reproduce the two failure classes

Run these against the public gateway. Replace nothing with a live secret while diagnosing missing-header cases.

# 1) Missing key → API_KEY_REQUIRED
curl -sS -i https://monkeytheme.com/v1/models | sed -n '1,20p'

# 2) Wrong auth scheme → still API_KEY_REQUIRED on this gateway
curl -sS -i https://monkeytheme.com/v1/models \
  -H "Authorization: Basic dGVzdA==" | sed -n '1,20p'

# 3) Present but fake key → INVALID_API_KEY
curl -sS -i https://monkeytheme.com/v1/models \
  -H "Authorization: Bearer sk-invalid-test-key-not-real" | sed -n '1,20p'

# 4) Alternate headers also accepted as key carriers (invalid value still 401)
curl -sS https://monkeytheme.com/v1/models -H "x-api-key: sk-invalid-test"
curl -sS https://monkeytheme.com/v1/models -H "x-goog-api-key: sk-invalid-test"

Accepted key carriers observed in the error text: Authorization: Bearer …, x-api-key, and x-goog-api-key. OpenAI-compatible clients should still prefer Bearer unless a specific client only supports one of the alternates.

Production-anchored case study (August 8, 2026)

The following signals come from a real journalctl -u sub2api --since '24 hours ago' sweep on the gateway host. Numbers are not staged or fabricated. They are the only 401 entries this gateway emitted in the last 24 hours, summed across all client IPs.

SignalValue (24h window)
Total 401 responses84
ingress_reject_reason: api_key_required60 (71%)
ingress_reject_reason: invalid_api_key24 (29%)
latency_ms range0 to 5 ms (avg 0.24 ms)
Distinct paths hit/v1/models (48), /v1/responses (15), /v1/chat/completions (9), /v1/messages (8), /v1/embeddings (4)
Distinct client IPs3 (65.49.150.250, 65.49.151.65, 2a02:4780:10:6b04::1)
GET vs POST split58 GET / 26 POST

Three log shapes from the same window. The first two are real external traffic. The third is the only internal 401 in the window and is included so readers can recognize it.

# (1) api_key_required — header missing entirely, latency_ms 0
"path": "/v1/models", "method": "GET", "status_code": 401,
"latency_ms": 0, "client_ip": "65.49.150.250",
"ingress_reject_reason": "api_key_required"

# (2) invalid_api_key — header present but value rejected, latency_ms 0 to 5
"path": "/v1/models", "method": "GET", "status_code": 401,
"latency_ms": 2, "client_ip": "65.49.150.250",
"ingress_reject_reason": "invalid_api_key"

# (3) upstream_failover_switching — internal, NOT a client 401
#     this is the gateway switching accounts because the upstream returned 401
"path": "/v1/chat/completions", "method": "POST",
"model": "gpt-5.6-sol", "account_id": 16,
"upstream_status": 401, "switch_count": 1, "max_switches": 10
How to read the latency. All 84 client-side 401s have latency_ms of 0 to 5. That means the gateway rejected the request before it ever called the upstream. If your 401 has latency_ms in the hundreds or thousands, the request reached the upstream and you are looking at a different log shape (see the upstream_failover_switching line above, or the 504 page for the >100ms shapes).
What this evidence does not show. The 24h window contains zero 401s from authenticated traffic. Every entry above is either a header that never arrived or a token that was never valid. If you see a 401 from a key that was working yesterday, look at After auth works and check whether the key was rotated, revoked, or whether the request is now hitting a different upstream that does not recognize it.

Fix checklist in order

  1. Confirm Base URL is https://monkeytheme.com/v1 (include /v1).
  2. Confirm the header is literally Authorization: Bearer <key> with one space and no quotes around the key.
  3. If the JSON code is API_KEY_REQUIRED, fix env var names, shell export, and client fields before creating a new key.
  4. If the code is INVALID_API_KEY, re-copy the key from the MonkeyAI dashboard, check for trailing whitespace/newlines, and confirm the key belongs to this site.
  5. Retest GET /v1/models. Only after a model list returns should you restart Codex, Cline, Hermes, or another agent client.
  6. Keep model names exact. A good key plus a nickname model id becomes a different error class.

Client places that commonly drop the key

Shell / curl

Unexported env vars, wrong variable name, or a header written as Bearer: sk-... instead of Authorization: Bearer sk-....

OpenAI SDKs

api_key empty because the process was started before .env loaded. Restart the process after exporting the key.

Codex CLI / coding agents

Profile points at another provider, or env_key names a variable that is unset in that shell.

GUI clients

Base URL missing /v1, key pasted into the model field, or a stale key cached from a previous workspace.

What not to change first

After auth works

curl -sS https://monkeytheme.com/v1/models \
  -H "Authorization: Bearer ***" | head -c 400

Success looks like a JSON model list, not an empty 200 HTML page. Then send one small chat request and only afterwards reconnect long-running agents.

FAQ

Does HTTP 401 mean MonkeyAI is down?

Usually no. On 2026-08-04, missing or invalid credentials returned structured JSON 401 responses from https://monkeytheme.com/v1/models. That is an authentication response, not an outage page.

What is the difference between API_KEY_REQUIRED and INVALID_API_KEY?

API_KEY_REQUIRED means the gateway did not receive a usable key in Authorization Bearer, x-api-key, or x-goog-api-key. INVALID_API_KEY means a key value was present but rejected. Fix the missing header first; only rotate or re-copy the key when you already see INVALID_API_KEY.

Can a key from another product or domain cause 401?

Yes. MonkeyAI keys are for https://monkeytheme.com/v1. A key from another gateway, another brand panel, or a different hostname can fail even if the string looks similar.

Should I paste the full API key into a support ticket or chat?

No. Share only the last four characters, the exact endpoint, UTC timestamp, HTTP status, and the JSON code/message. Revoke the key if it was exposed.

What should I test after the key looks fixed?

Call GET /v1/models first. When that returns a model list, run one small chat completion or client request. Do not jump straight into a long agent session while auth is still uncertain.

Related guides

API key test matrix

Broader 401 / 404 / model-not-found flow.

Open guide

Base URL /v1

When a missing /v1 creates confusing client errors.

Open guide

model not found

Auth passed, model id did not.

Open guide

504 upstream timeout

When the failure is latency, not credentials.

Open guide