Skip to content

[Feature]: Account for xAI image and video costs from cost_in_usd_ticks #661

Description

@junxinzhang

Affected Page / Module

Usage analytics / cost analytics

Use Case

We run CPA Manager Plus with xAI media models, including grok-imagine-image-quality, grok-imagine-image-2.0, grok-imagine-video, and grok-imagine-video-1.5. xAI bills image generation per image and video generation per output second/resolution, and returns the exact charged amount in usage.cost_in_usd_ticks.

We need Request Monitoring, Usage Analytics, Dashboard, and exports to report the real cost of these successful media requests even when token usage is zero.

Current Pain Point

CPAMP currently calculates cost only from per-1M-token prices. Successful image and video events are stored, but their input/output/total token counts are all zero, so CPAMP reports $0.000.

Verified with CPAMP v1.12.6 and CLIProxyAPI v7.2.145:

  • grok-imagine-image-quality: multiple successful image generation/edit events, all with zero total tokens.
  • grok-imagine-video-1.5: multiple successful video generation/status events, all with zero total tokens.
  • The event records exist, so this is not the old missing-usage-event issue fixed by CLIProxyAPI #4308.
  • The stored price schema only contains prompt/completion/cache prices per 1M tokens.
  • raw_json.usage and response metadata do not preserve cost_in_usd_ticks in CPAMP.

Existing issue #519 only requested informational billingUnit / billingRate fields and explicitly excluded cost calculation. PR #520 was closed without merge. This request is for actual request-level cost accounting.

Expected Solution

  1. Extend the CPA usage event contract to preserve a provider-reported monetary cost, preferably the exact xAI usage.cost_in_usd_ticks value together with its currency and source.
  2. Store that request-level cost in CPAMP without converting it into synthetic tokens.
  3. Prefer the provider-reported exact cost for media requests; keep existing token-based estimation as the fallback for normal language models.
  4. Include media costs in Request Monitoring, Usage Analytics, Dashboard totals, API-key/account/model breakdowns, hourly rollups, and exports.
  5. If the upstream usage event does not yet expose cost_in_usd_ticks, document and track the required CLIProxyAPI change rather than silently reporting zero cost.
  6. Support at least:
    • grok-imagine-image-quality
    • grok-imagine-image-2.0
    • grok-imagine-video
    • grok-imagine-video-1.5

Relevant Data Dimensions

  • API Key / client
  • AI provider
  • Model
  • Auth file
  • Account
  • Project
  • Time range
  • Cost
  • Token / usage
  • Quota
  • Failure reason / error type
  • None of these

Expected Acceptance Result

  • A successful xAI image response containing usage.cost_in_usd_ticks = 500000000 is stored and displayed as $0.05, while token counts remain zero.
  • A successful video request is charged using the exact provider-reported request cost and is included once in aggregate totals, without double-counting generation and polling events.
  • Cost totals match the xAI console for the same request IDs and time range within normal rounding tolerance.
  • Language-model token billing continues to work unchanged.
  • Historical events without provider-reported cost remain explicitly unknown or estimated; they are not presented as confirmed $0.000 charges.
  • Automated tests cover image cost, video cost, polling deduplication, rollups, exports, and token-billing regression.

Priority

High: affects the main workflow

Additional Context

The key distinction is that these models use non-token billing. Displaying a billing label is useful, but it does not solve request-level accounting or aggregate cost reporting.

Checklist

  • I have searched existing Issues and did not find a duplicate request.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions