Skip to content

OpenAI

OpenAI provides GPT models known for strong general reasoning. It’s the default AI engine in Codemetry.

Setup

  1. Get an API Key

    Visit platform.openai.com/api-keys to create an API key.

  2. Set Environment Variable

    Terminal window
    export OPENAI_API_KEY=sk-proj-...

    Or add to your .env file:

    OPENAI_API_KEY=sk-proj-...
  3. Configure Codemetry

    config/codemetry.php
    'ai' => [
    'enabled' => true,
    'engine' => 'openai',
    'model' => null, // Uses gpt-4o-mini by default
    ],
  4. Run Analysis

    Terminal window
    php artisan codemetry:analyze --days=7 --ai=1

Available Models

ModelDefaultCostQualitySpeed
gpt-4o-miniLowGoodFast
gpt-4oMediumExcellentMedium
gpt-4-turboHighExcellentMedium
gpt-3.5-turboVery LowAcceptableVery 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
Terminal window
# Override model for single run
php artisan codemetry:analyze --days=7 --ai=1 --ai-model=gpt-4o

Or in configuration:

'ai' => [
'engine' => 'openai',
'model' => 'gpt-4o', // Override default
],

API Key Options

Codemetry checks for API keys in this order:

  1. CODEMETRY_AI_API_KEY — Unified key for any engine
  2. OPENAI_API_KEY — OpenAI-specific key
Terminal window
# Option 1: Unified (works if you switch engines)
export CODEMETRY_AI_API_KEY=sk-proj-...
# Option 2: Engine-specific
export 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:

ModelInputOutputTotal
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:

”Rate limit exceeded”

Cause: Too many requests or token usage exceeded tier limits.

Solution:

”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_unavailable confounder in output indicates AI failed
  • Check Laravel logs for detailed error messages
  • Verify network connectivity to OpenAI