OpenAI
OpenAI provides GPT models known for strong general reasoning. It’s the default AI engine in Codemetry.
Setup
-
Get an API Key
Visit platform.openai.com/api-keys to create an API key.
-
Set Environment Variable
Terminal window export OPENAI_API_KEY=sk-proj-...Or add to your
.envfile:OPENAI_API_KEY=sk-proj-... -
Configure Codemetry
config/codemetry.php 'ai' => ['enabled' => true,'engine' => 'openai','model' => null, // Uses gpt-4o-mini by default], -
Run Analysis
Terminal window php artisan codemetry:analyze --days=7 --ai=1
Available Models
| Model | Default | Cost | Quality | Speed |
|---|---|---|---|---|
gpt-4o-mini | ✓ | Low | Good | Fast |
gpt-4o | Medium | Excellent | Medium | |
gpt-4-turbo | High | Excellent | Medium | |
gpt-3.5-turbo | Very Low | Acceptable | Very Fast |
Choosing a Model
For routine analysis: Use gpt-4o-mini (default)
- Best cost/quality balance
- Fast response times
- Sufficient for metric explanations
For detailed investigation: Use gpt-4o
- Better reasoning about complex patterns
- More nuanced explanations
- Worth the extra cost for deep dives
# Override model for single runphp artisan codemetry:analyze --days=7 --ai=1 --ai-model=gpt-4oOr in configuration:
'ai' => [ 'engine' => 'openai', 'model' => 'gpt-4o', // Override default],API Key Options
Codemetry checks for API keys in this order:
CODEMETRY_AI_API_KEY— Unified key for any engineOPENAI_API_KEY— OpenAI-specific key
# Option 1: Unified (works if you switch engines)export CODEMETRY_AI_API_KEY=sk-proj-...
# Option 2: Engine-specificexport OPENAI_API_KEY=sk-proj-...Cost Management
OpenAI charges per token. Codemetry sends minimal data:
- ~500-1000 input tokens per day analyzed
- ~200-500 output tokens per summary
Estimated costs per 30-day analysis:
| Model | Input | Output | Total |
|---|---|---|---|
| gpt-4o-mini | ~$0.005 | ~$0.01 | ~$0.015 |
| gpt-4o | ~$0.08 | ~$0.15 | ~$0.23 |
| gpt-3.5-turbo | ~$0.002 | ~$0.003 | ~$0.005 |
Troubleshooting
”Invalid API key”
Cause: API key is incorrect or expired.
Solution:
- Verify key at platform.openai.com/api-keys
- Check for typos or extra whitespace
- Ensure key has not been revoked
”Rate limit exceeded”
Cause: Too many requests or token usage exceeded tier limits.
Solution:
- Wait and retry
- Check usage at platform.openai.com/usage
- Consider upgrading your OpenAI tier
”Model not found”
Cause: Invalid model name or model not available on your account.
Solution:
- Check model name spelling
- Verify model availability for your account tier
- Use a different model
AI Summary Missing
Cause: AI request failed, analysis continued without it.
Check:
ai_unavailableconfounder in output indicates AI failed- Check Laravel logs for detailed error messages
- Verify network connectivity to OpenAI