new-api icon indicating copy to clipboard operation
new-api copied to clipboard

[Feature Request] 支持同一模型按分组配置不同计费模式(按次/按量)

Open gtwelhb520 opened this issue 7 months ago • 3 comments

根据我对项目代码的深入分析,现在来回答你的问题并整理需求。

分析结论

当前项目不支持这个功能,但可以实现。

当前计费架构

graph TD
    A[用户请求] --> B{获取模型计费模式}
    B -->|billing_mode = ratio| C[按量计费<br/>token数 × 模型倍率 × 分组倍率]
    B -->|billing_mode = tiered_expr| D[表达式计费<br/>自定义公式]
    B -->|modelPrice 存在| E[按次计费<br/>固定价格 × 分组倍率]
    
    F[分组系统] --> G[GroupRatio: 仅作为价格乘数]
    
    style B fill:#ff6b6b,color:#fff
    style F fill:#ffa94d,color:#fff

核心限制

计费模式(billing_mode)是按模型全局配置的,不是按 (模型, 分组) 维度配置:

  • GetBillingMode(model) — 只接受模型名,不接受分组参数
  • GetModelPrice(name, false) — 全局 modelPriceMap,无分组维度
  • ModelPriceHelper() — 计费模式判断与分组无关

分组(Group)目前只影响价格乘数(GroupRatio),不影响计费模式本身。


以下是按你要求整理的需求文档:


Read This First (Do Not Remove This Section)

  • Docs: https://docs.newapi.ai/
  • Usage questions first: https://deepwiki.com/QuantumNous/new-api
  • Warning: issues with this template removed, section headings deleted, or content cleared may be closed directly. Repeated abusive submissions may result in a block.

Your current newapi version

请填写当前版本号,例如:v1.0.0

Submission Checks

  • [ ] I have confirmed there are no similar issues
  • [ ] I have thoroughly read the docs at https://docs.newapi.ai/ and the project README, and confirmed the current version cannot meet my needs
  • [ ] I have not removed any guidance or section headings from this template and will complete it as requested
  • [ ] I understand that maintainers have limited time and issues that do not follow this template may be ignored or closed directly

Feature Description

需求:支持同一模型按分组配置不同计费模式(按次/按量)

背景: 当前系统的计费模式(ratio 按量 / model_price 按次 / tiered_expr 表达式)是按模型全局配置的,所有分组共享同一种计费方式。分组(Group)仅作为价格乘数(GroupRatio)影响最终费用,无法改变计费模式本身。

期望能力: 同一个模型,在不同分组下可以使用不同的计费模式。例如:

模型 分组 计费模式 说明
gpt-4o A组 按次收费(model_price) 每次请求固定扣费 $0.01
gpt-4o B组 按量收费(ratio) 按 token 用量 × 倍率计费

Use Case

场景描述:

运营方通过 A、B 两个渠道接入了同一个上游模型(如 gpt-4o),希望对不同用户群体采用差异化计费策略:

  1. A 组(按次收费): 面向轻度用户或批量调用场景,每次请求收取固定费用,不关心 token 用量。适合调用量大但单次 token 少的用户。
  2. B 组(按量收费): 面向重度用户,按实际 token 消耗量计费,用多少付多少。适合单次请求 token 量大的用户。

当前无法实现的原因:

  1. billing_setting.BillingMode 的数据结构是 map[string]string(模型名→计费模式),没有分组维度
  2. ratio_setting.modelPriceMap(按次价格)是全局的,不区分分组
  3. relay/helper/price.go 中的 ModelPriceHelper 计费模式判断逻辑不考虑分组信息
  4. model/pricing.go 中的定价接口 返回的 Pricing 结构中,QuotaType、ModelPrice、ModelRatio 均为模型级别,无分组维度

实现方向建议:

需要将计费模式从「按模型」维度扩展为「按模型+分组」维度,涉及以下模块改造:

  1. 数据结构扩展: BillingMode / modelPriceMap 增加分组维度(如 map[string]map[string]string)
  2. 计费逻辑改造: ModelPriceHelper 在判断计费模式时传入当前分组信息
  3. 定价接口扩展: /api/pricing 返回分组级别的计费模式和价格
  4. 管理后台: 支持按分组配置模型的计费模式和价格

gtwelhb520 avatar May 03 '26 16:05 gtwelhb520

[Bot Review]

感谢提交功能请求!我们注意到您未勾选「提交确认/Submission Checks」中的复选框,请在方便时编辑 Issue 补充勾选。后续如有进展,相关信息会在此更新。

支持...有时候不同上游之间的价格和计费模式可能不同

djkloop avatar Jul 08 '26 05:07 djkloop

+1

Leyanshi avatar Sep 27 '26 03:09 Leyanshi