fix(usage): record OpenRouter image costs and token counts

Merged
SA
2d ago
+275-8

Fixes #1382.

OpenRouter image generations were recorded as $0 with zero input/output tokens: core.ImageUsage dropped cost/cost_details and the Chat-style prompt_tokens/completion_tokens at decode.

  • core.ImageUsage now decodes both usage namings (Images naming wins when both are present), keeps output_tokens_details, and collects unknown members into RawUsage, which is passed through to the client like chat usage extras.
  • Image usage extraction merges RawUsage into RawData, so provider-reported costs (OpenRouter cost → cost_source: "openrouter_credits") apply to image endpoints as they do for chat. Without a cost, reported output tokens are priced at output_image_per_mtok.

Existing rows stored at $0 need POST /admin/usage/recalculate-pricing after deploy.

Summary by CodeRabbit

  • New Features
    • Image usage now accepts both image-style and chat-style token fields, including output-token details.
    • Provider-supplied usage metadata and cost details are preserved for client responses. Exact per-request costs take precedence over catalog rates, and OpenRouter credit costs are identified when provided.
    • Image output-token details are included in usage metadata.
  • Bug Fixes
    • Provider-reported image counts are no longer treated as generated image counts when calculating output costs.
  • Documentation
    • Clarified how provider costs, token field names, and cost details are handled for image requests.