# openai.AuthenticationError: Incorrect API key provided: sk-... You can find your API key at https://platform.openai.com/account/api-keys.

- **ID:** `llm/invalid-api-key-format`
- **Domain:** llm
- **Category:** auth_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

The API key string is malformed, expired, or belongs to a different organization/project than the one being used for the request.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai>=1.0.0 | active | — | — |
| openai==0.28.0 | active | — | — |

## Workarounds

1. **Verify the API key by testing it directly with curl:
curl -H 'Authorization: Bearer YOUR_API_KEY' https://api.openai.com/v1/models
If this fails, the key is invalid. Check for trailing newlines or spaces in your .env file.** (95% success)
   ```
   Verify the API key by testing it directly with curl:
curl -H 'Authorization: Bearer YOUR_API_KEY' https://api.openai.com/v1/models
If this fails, the key is invalid. Check for trailing newlines or spaces in your .env file.
   ```
2. **Ensure the API key is loaded correctly from environment variables without quotes:
import os
import openai
openai.api_key = os.getenv('OPENAI_API_KEY').strip()
print(repr(openai.api_key))  # Should show 'sk-...' without extra spaces** (90% success)
   ```
   Ensure the API key is loaded correctly from environment variables without quotes:
import os
import openai
openai.api_key = os.getenv('OPENAI_API_KEY').strip()
print(repr(openai.api_key))  # Should show 'sk-...' without extra spaces
   ```

## Dead Ends

- **** — The root cause is often a copy-paste error or whitespace, not the key itself. (60% fail)
- **** — The key may be revoked by the provider after exposure, or the code leaks secrets. (80% fail)
