AI Configuration
Set up AI providers (OpenAI and Anthropic Claude) for BlockForge's AI features.
Overview
BlockForge supports multiple AI providers:
- OpenAI - GPT-4, GPT-4 Turbo, GPT-3.5 Turbo
- Anthropic - Claude 3 (Sonnet, Haiku, Opus)
OpenAI Setup
1. Get API Key
- Go to platform.openai.com
- Sign up or log in
- Navigate to API Keys
- Click Create new secret key
- Copy the key (you won't see it again!)
2. Configure Environment
Add to .env.local:
bash
VITE_AI_PROVIDER=openai
VITE_AI_API_KEY=sk-...
VITE_AI_MODEL=gpt-4o-mini3. Available Models
Choose based on your needs:
gpt-4o-mini- Fast, cost-effective (recommended for most use cases)gpt-4o- More capable, faster than GPT-4gpt-4-turbo- High quality, good for complex tasksgpt-4- Most capable, slowergpt-3.5-turbo- Fast and cheap, good for simple tasks
4. Test Configuration
- Start development server
- Open a page in BlockForge
- Select some text
- Click AI icon
- Try an AI action (e.g., "Improve")
Anthropic Claude Setup
Option 1: Direct API (Client-Side)
Add to .env.local:
bash
VITE_AI_PROVIDER=anthropic
VITE_AI_API_KEY=sk-ant-...
VITE_AI_MODEL=claude-3-haiku-20240307Option 2: Edge Function (Recommended)
For better security, use Supabase Edge Function as a proxy:
Set up Edge Function:
- Create Edge Function in Supabase
- Add
ANTHROPIC_API_KEYto function secrets - Deploy function
Configure environment:
bash
VITE_AI_PROVIDER=anthropic
VITE_AI_API_KEY=your-edge-function-url
VITE_AI_MODEL=claude-3-haiku-20240307Available Claude Models
claude-3-haiku-20240307- Fast, cost-effectiveclaude-3-sonnet-20240229- Balanced, recommendedclaude-3-opus-20240229- Most capable, slower
Get Anthropic API Key
- Go to console.anthropic.com
- Sign up or log in
- Navigate to API Keys
- Click Create Key
- Copy the key
Switching Providers
You can switch providers by changing environment variables:
bash
# Use OpenAI
VITE_AI_PROVIDER=openai
VITE_AI_API_KEY=sk-...
# Or use Anthropic
VITE_AI_PROVIDER=anthropic
VITE_AI_API_KEY=sk-ant-...Restart the development server after changing providers.
Model Selection
For Speed
- OpenAI:
gpt-4o-miniorgpt-3.5-turbo - Anthropic:
claude-3-haiku-20240307
For Quality
- OpenAI:
gpt-4oorgpt-4-turbo - Anthropic:
claude-3-sonnet-20240229
For Complex Tasks
- OpenAI:
gpt-4 - Anthropic:
claude-3-opus-20240229
Cost Considerations
OpenAI Pricing (approximate)
- GPT-4o-mini: Very low cost
- GPT-3.5-turbo: Low cost
- GPT-4o: Medium cost
- GPT-4: Higher cost
Anthropic Pricing (approximate)
- Claude Haiku: Very low cost
- Claude Sonnet: Medium cost
- Claude Opus: Higher cost
Monitor usage in respective provider dashboards.
Security Best Practices
- Never commit API keys - Use environment variables only
- Use Edge Functions - For production, proxy through Edge Functions
- Set usage limits - Configure limits in provider dashboards
- Rotate keys - Regularly rotate API keys
- Monitor usage - Track API usage and costs
Troubleshooting
API Key Errors
- Verify key is correct
- Check key has necessary permissions
- Ensure key hasn't expired
Rate Limiting
- Reduce request frequency
- Upgrade API plan if needed
- Implement retry logic
Model Not Found
- Verify model name is correct
- Check provider has access to model
- Update to supported model version
Connection Issues
- Check network connectivity
- Verify API endpoint URLs
- Check firewall/proxy settings
Production Setup
For production deployment:
- Add environment variables in hosting platform (Vercel, etc.)
- Use Edge Functions for sensitive API keys
- Set up monitoring for API usage
- Configure rate limiting if needed
- Set up alerts for errors or high usage