Re_Backend/API_KEY_TROUBLESHOOTING.md

265 lines
6.0 KiB
Markdown

# 🔑 API Key Troubleshooting Guide
## ⚠️ Problem: Getting 404 Errors for ALL Claude Models
If you're getting 404 "model not found" errors for **multiple Claude models**, the issue is likely with your Anthropic API key, not the model versions.
---
## 🔍 Step 1: Verify Your API Key
### Check Your API Key Status
1. Go to: https://console.anthropic.com/
2. Log in to your account
3. Navigate to **Settings****API Keys**
4. Check:
- ✅ Is your API key active?
- ✅ Does it have an active billing method?
- ✅ Have you verified your email?
- ✅ Are there any usage limits or restrictions?
### API Key Tiers
Anthropic has different API access tiers:
| Tier | Access Level | Requirements |
|------|-------------|--------------|
| **Free Trial** | Limited models, low usage | Email verification |
| **Paid Tier 1** | All Claude 3 models | Add payment method, some usage |
| **Paid Tier 2+** | All models + higher limits | More usage history |
**If you just created your API key:**
- You might need to add a payment method
- You might need to make a small payment first
- Some models might not be available immediately
---
## 🎯 Step 2: Try the Most Basic Model (Claude 3 Haiku)
I've changed the default to **`claude-3-haiku-20240307`** - this should work with ANY valid API key.
### Restart Your Backend
**IMPORTANT:** You must restart the server for changes to take effect.
```bash
# Stop the current server (Ctrl+C)
# Then start again:
cd Re_Backend
npm run dev
```
### Check the Startup Logs
Look for this line:
```
[AI Service] ✅ Claude provider initialized with model: claude-3-haiku-20240307
```
### Test Again
Try generating a conclusion. You should see in logs:
```
[AI Service] Generating with Claude model: claude-3-haiku-20240307
```
---
## 🔧 Step 3: Check for Environment Variable Overrides
Your `.env` file might be overriding the default model.
### Check Your `.env` File
Open `Re_Backend/.env` and look for:
```bash
CLAUDE_MODEL=...
```
**If it exists:**
1. **Delete or comment it out** (add `#` at the start)
2. **Or change it to Haiku:**
```bash
CLAUDE_MODEL=claude-3-haiku-20240307
```
3. **Restart the server**
---
## 🐛 Step 4: Verify API Key is Loaded
Add this temporary check to see if your API key is being loaded:
### Option A: Check Logs on Startup
When you start the server, you should see:
```
[AI Service] ✅ Claude provider initialized with model: claude-3-haiku-20240307
```
If you DON'T see this:
- Your API key might be missing or invalid
- Check `.env` file has: `CLAUDE_API_KEY=sk-ant-api03-...`
### Option B: Test API Key Manually
Create a test file `Re_Backend/test-api-key.js`:
```javascript
const Anthropic = require('@anthropic-ai/sdk');
require('dotenv').config();
const apiKey = process.env.CLAUDE_API_KEY || process.env.ANTHROPIC_API_KEY;
console.log('API Key found:', apiKey ? 'YES' : 'NO');
console.log('API Key starts with:', apiKey ? apiKey.substring(0, 20) + '...' : 'N/A');
async function testKey() {
try {
const client = new Anthropic({ apiKey });
// Try the most basic model
const response = await client.messages.create({
model: 'claude-3-haiku-20240307',
max_tokens: 100,
messages: [{ role: 'user', content: 'Say hello' }]
});
console.log('✅ API Key works!');
console.log('Response:', response.content[0].text);
} catch (error) {
console.error('❌ API Key test failed:', error.message);
console.error('Error details:', error);
}
}
testKey();
```
Run it:
```bash
cd Re_Backend
node test-api-key.js
```
---
## 💡 Step 5: Alternative - Use OpenAI or Gemini
If your Anthropic API key has issues, you can switch to another provider:
### Option A: Use OpenAI
1. **Get OpenAI API key** from: https://platform.openai.com/api-keys
2. **Add to `.env`:**
```bash
AI_PROVIDER=openai
OPENAI_API_KEY=sk-...
```
3. **Install OpenAI SDK:**
```bash
cd Re_Backend
npm install openai
```
4. **Restart server**
### Option B: Use Google Gemini
1. **Get Gemini API key** from: https://makersuite.google.com/app/apikey
2. **Add to `.env`:**
```bash
AI_PROVIDER=gemini
GEMINI_API_KEY=...
```
3. **Install Gemini SDK:**
```bash
cd Re_Backend
npm install @google/generative-ai
```
4. **Restart server**
---
## 🎯 Quick Checklist
- [ ] My Anthropic API key is valid and active
- [ ] I have a payment method added (if required)
- [ ] My email is verified
- [ ] I've deleted/commented out `CLAUDE_MODEL` from `.env` (or set it to haiku)
- [ ] I've **restarted the backend server completely**
- [ ] I see the correct model in startup logs
- [ ] I've tested with the test script above
---
## 🆘 Still Not Working?
### Check Your API Key Format
Valid format: `sk-ant-api03-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
- Must start with `sk-ant-`
- Must be quite long (80+ characters)
- No spaces or line breaks
### Get a New API Key
1. Go to https://console.anthropic.com/settings/keys
2. Delete old key
3. Create new key
4. Add payment method if prompted
5. Update `.env` with new key
6. Restart server
### Contact Anthropic Support
If nothing works:
- Email: support@anthropic.com
- Check: https://status.anthropic.com/ (for service issues)
- Community: https://anthropic.com/community
---
## 🎯 Current System Default
The system now defaults to:
```
claude-3-haiku-20240307
```
This is the **most basic Claude model** that should work with **any valid API key**, even free tier.
If even Haiku doesn't work, there's a fundamental issue with your Anthropic API key or account status.
---
## ✅ Success Indicators
When everything is working correctly, you should see:
1. **On server startup:**
```
[AI Service] ✅ Claude provider initialized with model: claude-3-haiku-20240307
```
2. **When generating conclusion:**
```
[AI Service] Generating with Claude model: claude-3-haiku-20240307
```
3. **In response:**
```
[AI Service] ✅ Conclusion generated successfully
```
No 404 errors! ✅