api7 / api7/aisix

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

Open
#457 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

cross-repo P2
Dominant language
Rust
Stars
157
Forks
32
Avg merge
1h 25m
Merged PRs (30d)
145

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the UsageEvent wire struct and trace the audio.rs and images.rs paths that emit events. Then inspect the cp-api pricing rules and the end-to-end ledger coverage for legacy whisper-1 and dall-e-* requests. Done means duration and image dimensions remain wire-compatible, are populated, and produce non-zero priced ledger entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.