invalid_api_key or 404 error usually means the request never reached LaoZhang API or the base URL path is wrong; the key itself is rarely the problem. A LaoZhang API key only works with LaoZhang API hosts, set as shown below.
Match the error to its cause
Configure each SDK
Each SDK builds request paths differently. The OpenAI SDK appends only the resource name, so its base URL must include/v1. The Anthropic SDK appends /v1/messages itself, so adding /v1 produces /v1/v1/messages and a 404.
- OpenAI SDK
- Anthropic SDK
- Google Gen AI SDK
LAOZHANG_API_KEY environment variable before you run these. To see which models accept the Anthropic format, check the model list in Claude protocol.
Configure with environment variables
Many tools read OpenAI’s standard environment variables, so you can leave the base URL out of your code:- Your code, a
.envfile, and the shell each set a different URL. - A third-party client still has an old provider saved.
- The process wasn’t restarted and still reads the old variables.
https://api.laozhang.ai/v1 as the API URL, your LaoZhang API key, and a full model ID from the model catalog.
Confirm the setup with one request
data array means the key and host are both correct; the array lists the models this token can call. If you still get a 401, check in Tokens that the token is enabled, hasn’t expired, and has quota left, or create a new token and try again.
A key that works, then intermittently gets 401
Keys don’t stop working at random. If a key worked a moment ago and the next request gets 401Invalid token., some code path almost certainly sent a different key:
- One process, container, or cron job didn’t load the environment variable, so the request went out with an empty key.
- When reading the key fails, the code falls back to an old key hard-coded in the source or a backup config.
- Service instances or workers have different configs, and some still hold an old key.
- The key lost characters when it was concatenated, truncated, or copied.
Invalid token., so the error alone can’t tell you which one you sent. Log the first 6 characters and the length of the key where the request is sent, and compare them with the token in the console:
Fallback hosts
All three hosts accept the same API key, paths, and parameters. Only the hostname changes:- Default:
api.laozhang.ai - Direct connections from Europe or North America:
api-vip.laozhang.ai - When
api.laozhang.aican’t be reached:api2.laozhang.ai
Still stuck?
Email hi@laozhang.ai with:- the request time and time zone;
- the full request URL (without the key) and the SDK you use;
- the HTTP status and the full error body;
- the first 6 and last 4 characters of the key.