Skip to content

UsageEvent: carry non-token cost basis for audio (duration) and images (n×size×quality) #457

Description

@moonming

Context

#226 (non-chat UsageEvent emission) is now complete — all 6 endpoints emit (embeddings/responses/completions/rerank #402/#426/#425/#428, audio/images #454). But the audio/images MVP (#406/#407) only carries token counts. The token shape covers the modern models (gpt-image-1, gpt-4o-transcribe), which return a usage token block. The legacy models do not, and are not billed by tokens at all:

  • audio (whisper-1, and gpt-4o-transcribe under some response_formats) → billed by duration-seconds (OpenAI returns usage:{type:"duration",seconds:N}).
  • images (dall-e-2/3) → billed by (model, size, quality) × n — no token block at all.

Today those requests emit a zero-token UsageEvent: visible + attributed in the ledger (no longer invisible — that gap is closed by #454), but with no cost basis, so cp-api cannot price them.

Gap

The UsageEvent wire struct (aisix-obs) has no field to carry duration-seconds or image (count/size/quality). cp-api's pricing therefore can't compute spend for legacy audio/image traffic even though the event now arrives.

Proposed work (cross-repo)

  1. ai-gateway — extend UsageEvent with optional cost-basis fields, e.g. audio_duration_seconds: Option<f64> and image_count/image_size/image_quality: Option<...> (all #[serde(skip_serializing_if)] so existing token-only events are unchanged on the wire). Populate them in audio.rs / images.rs from the response usage.duration and the request n/size/quality.
  2. cp-api (AISIX-Cloud) — add pricing rules keyed on duration-seconds (audio) and (model,size,quality) tuples (images), and apply them when the new fields are present.

Cross-checked against comparable open-source multi-provider gateways: audio is priced cost_per_second × duration; images via a (quality,size,n) cost calculator — confirming duration + (n,size,quality) are the right cost dimensions.

Acceptance

  • UsageEvent carries duration (audio) + n/size/quality (images), back-compatible on the wire
  • DP populates them for legacy whisper-1 / dall-e-* traffic
  • cp-api prices duration-based audio and per-image image spend
  • e2e asserts a dall-e-3 / whisper-1 request produces a priced (non-zero spend) ledger entry

Refs: #226 (parent, now complete), #454 (origin — the token-MVP that deferred this)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Long-tail integrations — backlogcross-repoRequires changes in DP + CP + Dashboard UI + e2e

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions