Symptoms
- HTTP status code 401 (Unauthorized) or 403 (Forbidden)
- Error messages mentioning “invalid API key,” “authentication failed,” or “access denied”
- Requests that work from one machine but fail from another
Verify the Environment Variable
Each preset references an environment variable for its API key. The variable name is defined in the preset YAML file using the${VAR_NAME} syntax. For example, the openai preset uses ${OPENAI_API_KEY} and the anthropic preset uses ${ANTHROPIC_API_KEY}.
Confirm that the variable is set and not empty:
.env file with a library like vlucas/phpdotenv, make sure the file is loaded before Polyglot resolves the preset:
Check the Key Format
Some providers have distinctive key formats. Verifying the prefix can catch copy-paste errors early:- OpenAI keys typically start with
sk- - Anthropic keys typically start with
sk-ant- - Mistral keys are UUIDs or short alphanumeric strings
Confirm the Preset Matches the Provider
When you callInference::using('openai'), Polyglot loads the openai preset and uses the API key, URL, and endpoint configured in that file. If you accidentally pass the wrong preset name, the key may not match the provider.
Test the Key Directly
Use a minimal script to confirm that the key works independently of your application logic:Pass the Key Programmatically
If environment variables are not practical, you can supply the API key directly throughLLMConfig:
Security note: Avoid hard-coding API keys in source files that are committed to version control. Use environment variables, secrets managers, or encrypted configuration files.
Common Pitfalls
- Trailing whitespace or newlines in the environment variable. Trim the value if your loading mechanism adds whitespace.
- Expired or revoked keys. Regenerate the key in your provider’s dashboard.
- Organization or project restrictions. Some OpenAI keys require an
organizationvalue in the preset metadata. Check the preset YAML for ametadata.organizationfield. - IP allowlists. Some providers or enterprise plans restrict API access to specific IP addresses. Confirm your server’s IP is permitted.