All files / web/src/lib/ai-usage pricing.ts

100% Statements 139/139
85.71% Branches 12/14
100% Functions 1/1
100% Lines 139/139

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 1401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 11x 11x 12x 5x 5x 5x 5x 5x 5x 5x 5x 5x 12x 3x 12x 2x 12x 1x 12x 12x  
/**
 * AI model pricing configuration.
 *
 * Prices are applied at query time for cost estimation — NOT stored in the DB.
 * Update this file when provider prices change.
 *
 * All prices in USD.
 */
 
interface TokenPricing {
  /** Price per 1M input tokens */
  inputPerMillion: number
  /** Price per 1M output tokens */
  outputPerMillion: number
  /** Price per 1M reasoning tokens (if applicable) */
  reasoningPerMillion?: number
}
 
interface ImagePricing {
  /** Price per image generated */
  perImage: number
}
 
interface AudioPricing {
  /** Price per minute of audio */
  perMinute: number
}
 
interface CharacterPricing {
  /** Price per 1M characters */
  perMillionCharacters: number
}
 
type PricingEntry =
  | { type: 'tokens'; pricing: TokenPricing }
  | { type: 'image'; pricing: ImagePricing }
  | { type: 'audio'; pricing: AudioPricing }
  | { type: 'characters'; pricing: CharacterPricing }
 
/**
 * Pricing table keyed by `${provider}/${model}` or `${provider}/${apiType}`.
 *
 * When looking up pricing, we try specific model first, then fall back to apiType.
 */
export const PRICING: Record<string, PricingEntry> = {
  // --- OpenAI LLM (Responses API / Chat Completions) ---
  'openai/gpt-5': {
    type: 'tokens',
    pricing: { inputPerMillion: 2, outputPerMillion: 8 },
  },
  'openai/gpt-5.2': {
    type: 'tokens',
    pricing: { inputPerMillion: 2, outputPerMillion: 8, reasoningPerMillion: 8 },
  },
  'openai/gpt-4o-mini': {
    type: 'tokens',
    pricing: { inputPerMillion: 0.15, outputPerMillion: 0.6 },
  },
 
  // --- OpenAI Realtime ---
  'openai/gpt-realtime-1.5': {
    type: 'audio',
    pricing: { perMinute: 0.066 }, // audio input; output is ~$0.132/min
  },
 
  // --- OpenAI TTS ---
  'openai/gpt-4o-mini-tts': {
    type: 'characters',
    pricing: { perMillionCharacters: 12 },
  },
 
  // --- OpenAI Image ---
  'openai/gpt-image-1': {
    type: 'image',
    pricing: { perImage: 0.04 }, // varies by quality/size
  },
 
  // --- OpenAI Embeddings ---
  'openai/text-embedding-3-large': {
    type: 'tokens',
    pricing: { inputPerMillion: 0.13, outputPerMillion: 0 },
  },
 
  // --- Google Gemini ---
  'gemini/gemini-2.5-flash-image': {
    type: 'image',
    pricing: { perImage: 0.039 },
  },
  'gemini/gemini-3-pro-image-preview': {
    type: 'image',
    pricing: { perImage: 0.039 },
  },
 
  // --- ElevenLabs ---
  'elevenlabs/music_v1': {
    type: 'audio',
    pricing: { perMinute: 0.3 }, // estimate
  },
}
 
/**
 * Estimate the cost of a single usage record.
 *
 * Returns null if pricing is not configured for this provider/model.
 */
export function estimateCost(record: {
  provider: string
  model: string
  apiType: string
  inputTokens?: number | null
  outputTokens?: number | null
  reasoningTokens?: number | null
  imageCount?: number | null
  inputCharacters?: number | null
  audioDurationSeconds?: number | null
}): number | null {
  const key = `${record.provider}/${record.model}`
  const entry = PRICING[key]
  if (!entry) return null
 
  switch (entry.type) {
    case 'tokens': {
      const input = (record.inputTokens ?? 0) / 1_000_000
      const output = (record.outputTokens ?? 0) / 1_000_000
      const reasoning = (record.reasoningTokens ?? 0) / 1_000_000
      return (
        input * entry.pricing.inputPerMillion +
        output * entry.pricing.outputPerMillion +
        reasoning * (entry.pricing.reasoningPerMillion ?? entry.pricing.outputPerMillion)
      )
    }
    case 'image':
      return (record.imageCount ?? 0) * entry.pricing.perImage
    case 'audio':
      return ((record.audioDurationSeconds ?? 0) / 60) * entry.pricing.perMinute
    case 'characters':
      return ((record.inputCharacters ?? 0) / 1_000_000) * entry.pricing.perMillionCharacters
  }
}