Skip to content

OpenAI

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

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

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
],

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-...

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

Cause: API key is incorrect or expired.

Solution:

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

Solution:

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

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