Skip to content

Image generation models (gemini-3-pro-image-preview) billing calculation is incorrect #663

@sususu98

Description

@sususu98

Summary

The current billing logic does not correctly handle image generation models like gemini-3-pro-image-preview. The system treats IMAGE output tokens the same as TEXT tokens, resulting in ~7.6x undercharging.

Price Configuration

[models."gemini-3-pro-image-preview"]
mode = "image_generation"
input_cost_per_token = 0.000002
output_cost_per_token = 0.000012
output_cost_per_image = 0.134
output_cost_per_image_token = 0.00012

Example usageMetadata

{
  "promptTokenCount": 326,
  "candidatesTokenCount": 2340,
  "totalTokenCount": 3003,
  "promptTokensDetails": [
    { "modality": "TEXT", "tokenCount": 326 }
  ],
  "candidatesTokensDetails": [
    { "modality": "IMAGE", "tokenCount": 2000 }
  ],
  "thoughtsTokenCount": 337
}

Current vs Expected Billing

Item Current Calculation Expected Calculation
Input TEXT 326 × $0.000002 = $0.000652 326 × $0.000002 = $0.000652
Output IMAGE 2000 × $0.000012 = $0.024 2000 × $0.00012 = $0.24
Output TEXT (included above) 340 × $0.000012 = $0.00408
Thoughts 337 × $0.000012 = $0.00404 337 × $0.000012 = $0.00404
Total $0.0328 $0.2488

Difference: ~7.6x undercharging

Root Cause

  1. response-handler.ts: extractUsageMetrics does not parse candidatesTokensDetails to distinguish IMAGE vs TEXT modality
  2. cost-calculation.ts: calculateRequestCost does not use output_cost_per_image_token field for IMAGE tokens

Suggested Fix

  1. Extend UsageMetrics type to include output_image_tokens
  2. Parse candidatesTokensDetails in extractUsageMetrics to extract IMAGE token count
  3. Update calculateRequestCost to apply output_cost_per_image_token for image output tokens

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions