Skip to content

feat: expose video task billing usage (token via header, seconds in body) - #5831

Open
feitianbubu wants to merge 2 commits into
QuantumNous:mainfrom
feitianbubu:feat/video-usage-header
Open

feat: expose video task billing usage (token via header, seconds in body)#5831
feitianbubu wants to merge 2 commits into
QuantumNous:mainfrom
feitianbubu:feat/video-usage-header

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Jul 1, 2026

Copy link
Copy Markdown
Member

📝 变更描述 / Description

向视频任务查询接口的输出补充计费计量信息,方便下游(含中转/网关场景)据此计费。分两类:

1. 按量(token)计费 → 响应头 X-New-Api-Usage

GET /v1/videos/{task_id} 响应上新增响应头,携带任务 token 用量:

X-New-Api-Usage: {"completion_tokens": 1234, "total_tokens": 1234}
  • 仅在适配器 ParseTaskResult 能解析出 total_tokens > 0 时设置。
  • 走响应头而非响应体,body 保持纯 OpenAI 视频对象标准结构。

2. 按秒计费 → 响应体标准字段 seconds

ToOpenAIVideo 统一将计费所用时长(BillingContext.OtherRatios["seconds"],即实际计费依据)写入标准的 seconds 字段:

{ "model": "...", "status": "completed", "seconds": "5", ... }
  • seconds 是 OpenAI 视频对象的标准字段,不污染结构。
  • 单点实现,所有按秒计费渠道(Ali/Veo 等)自动生效;非按秒渠道(无 seconds 倍率)不受影响。
  • 返回值等于实际计费依据,显示与扣费一致。

两处均为纯增量,不改变任何现有计费/响应逻辑;普通客户端忽略即可。

🚀 变更类型 / Type of change

  • ✨ 新功能 (New feature)

🔗 关联任务 / Related Issue

  • N/A

✅ 提交前检查项 / Checklist

  • 人工确认: 描述由人工整理撰写。
  • 非重复提交: 已确认无重复 PR。
  • 变更理解: 已理解改动原理与影响范围。
  • 范围聚焦: 仅向 video 响应补充计量信息(header + seconds 字段),无其它改动。
  • 本地验证: go build ./... 通过。
  • 安全合规: 无敏感凭据,仅暴露计量信息,符合代码规范。

📸 运行证明 / Proof of Work

  • 按量模型完成态查询:响应头 X-New-Api-Usage: {"completion_tokens":1234,"total_tokens":1234}
  • 按秒模型(如 happyhorse / veo)完成态查询:响应体 "seconds": "5",与实际计费时长一致。

Summary by CodeRabbit

  • New Features
    • Added support for exporting token usage metadata on video fetch responses when available.
    • Video fetch responses now include a usage header alongside the standard OpenAI-compatible video payload.
    • Extended OpenAI video output to optionally include per-second billing duration when provided by billing context.

@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 81a68c7d-8f4a-4721-9acd-cd8b7ca17939

📥 Commits

Reviewing files that changed from the base of the PR and between 969ecca and 9a8459b.

📒 Files selected for processing (1)
  • model/task.go

Walkthrough

The OpenAI video response path now adds upstream usage data to a response header and includes billing-derived seconds in the converted video payload when available.

Changes

Video response enrichment

Layer / File(s) Summary
Parse task result and set usage header
relay/relay_task.go
Parses the upstream task result, marshals token usage when totals are present, and sets the X-New-Api-Usage response header.
Populate video seconds from billing context
model/task.go
Adds strconv import support and sets openAIVideo.Seconds from the billing context seconds ratio when it is positive.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • QuantumNous/new-api#1659: Both PRs modify videoFetchByIDRespBodyBuilder in relay/relay_task.go along the OpenAI video conversion path.
  • QuantumNous/new-api#2141: Adds ParseTaskResult/upstream task result plumbing that aligns with this PR’s usage-header logic.

Poem

A bunny peeked at video streams,
Counted tokens, stitched sweet dreams,
Seconds hopped in, neat and bright,
Headers gleamed with usage light,
Thump! The payload feels just right.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: exposing billing usage via a header and seconds in the video body.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@feitianbubu feitianbubu changed the title feat: expose video task token usage via X-New-Api-Usage response header feat: expose video task billing usage (token via header, seconds in body) Jul 1, 2026
JacksonsY added a commit to JacksonsY/new-api that referenced this pull request Jul 19, 2026
移植上游 PR QuantumNous#6173(异步视频输出 Token 计费)与 QuantumNous#5831(计量外露)。

QuantumNous#6173:RecalculateTaskQuotaByTokens 此前把 totalTokens 整体按模型倍率
计价,输出 token 未乘补全倍率。现新增 completionTokens 入参,按
input + completion×completionRatio 计价;倍率优先取任务提交时的
BillingContext 快照而非当前配置,避免中途改价影响在途任务。
TaskBillingContext 新增 CompletionRatio(指针,兼容未保存该字段的历史
任务);未配置补全倍率的模型默认取 1,与旧行为等价。

QuantumNous#5831:任务计量经 X-New-Api-Usage 响应头透传给下游(中转套娃场景),
body 保持纯 OpenAI 结构;ToOpenAIVideo 带出按秒计费的时长。

一处加固:OtherRatios["seconds"] 读自数据库,历史记录可能写于尚无时长
上界校验的版本,转 int 前先钳到 MaxTaskDurationSeconds——超出 int
范围的 float64 转换结果是未定义的。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Calcium-Ion
Calcium-Ion force-pushed the main branch 2 times, most recently from 51fdfc5 to 2b6f1df Compare August 30, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant