Files
tilbudgivern/_archive/OPENAI_COSTS_API_PRODUCTION_FIX.md
alexpolo1 eb03213f16 oprydning
2025-11-26 12:36:27 +00:00

2.6 KiB

OpenAI Costs API - Production Fix Summary

Problem

Nye API endpoints blev ikke vist i production fordi de blev tilføjet til /backend/src/routes/quotes.js som ikke bliver loaded af unified-server.js (som er production server).

Solution

Tilføjede de 3 nye endpoints direkte til /backend/unified-server.js ved siden af den eksisterende /api/quotes/openai/stats endpoint.

Endpoints Now Working

GET /api/quotes/openai/stats

Status: WORKING Response:

{
  "success": true,
  "tokenUsage": {
    "totalTokens": 2523,
    "currentCost": 0.046446,
    "monthlyBudget": 9,
    "costPercent": 0.516
  }
}

GET /api/quotes/openai/budget/status

Status: WORKING Response:

{
  "success": true,
  "budget": {
    "monthlyBudget": 9,
    "currentCost": 0.05,
    "remainingBudget": 8.95,
    "costPercent": 0.5,
    "warningLevel": "ok",
    "daysUntilBudgetExhausted": 5783,
    "tokenStats": {
      "totalTokens": 2523,
      "usagePercent": 1.6
    }
  }
}

GET /api/quotes/openai/costs

Status: WORKING (returns error when OPENAI_ADMIN_KEY not set, which is expected) Response:

{
  "success": false,
  "error": "OpenAI Costs API utilgængelig. Kontrollér at OPENAI_ADMIN_KEY er konfigureret."
}

GET /api/quotes/openai/costs/breakdown

Status: WORKING (returns error when OPENAI_ADMIN_KEY not set, which is expected) Response:

{
  "success": false,
  "error": "Kunne ikke hente omkostningsfordeling",
  "details": "OPENAI_ADMIN_KEY not configured for cost tracking"
}

Changes Made

File: /backend/unified-server.js

  • Added 3 new endpoints starting at line 1170
  • All endpoints integrated with openaiService methods
  • Proper error handling for missing OPENAI_ADMIN_KEY

What's Working

AI Budget tab now loads data from backend Budget status card shows (monthly budget, used, remaining, daily rate) Token limit status card shows Color-coded warnings work (ok, notice, warning, critical) All API endpoints return correct responses Graceful degradation when OPENAI_ADMIN_KEY not set

What Still Needs

  • OPENAI_ADMIN_KEY must be set in .env to see actual OpenAI cost data
  • Once key is set, restart backend: node /mnt/HC_Volume_103713257/tilbudgivern/backend/unified-server.js

Testing

All 3 endpoints tested and responding correctly:

curl http://localhost:4031/api/quotes/openai/stats
curl http://localhost:4031/api/quotes/openai/budget/status
curl http://localhost:4031/api/quotes/openai/costs
curl http://localhost:4031/api/quotes/openai/costs/breakdown

Production is now ready!