计费模块

计费模块管理模型调用的费用规则、用量汇总及月度账单,是 Token 模块用量数据的财务映射。


6.1 计费规则

查询计费规则列表

GET /api/v1/billing/rules

查询参数 说明
model_id 按模型筛选
membership_level 按会员等级筛选
{
  "code": 0,
  "data": {
    "items": [
      {
        "rule_id": "br_001",
        "model_id": "gpt-4o",
        "model_name": "GPT-4o",
        "membership_level": "basic",
        "input_price": "0.028",
        "output_price": "0.084",
        "unit": "per_1k_tokens",
        "currency": "CNY",
        "effective_at": "2024-01-01T00:00:00Z"
      }
    ]
  }
}

查询计费规则详情

GET /api/v1/billing/rules/{rule_id}

创建计费规则(管理员)

POST /admin/v1/billing/rules

{
  "model_id": "claude-3-5-sonnet",
  "membership_level": "free",
  "input_price": "0.045",
  "output_price": "0.135",
  "unit": "per_1k_tokens",
  "currency": "CNY",
  "effective_at": "2024-02-01T00:00:00Z"
}

更新计费规则(管理员)

PUT /admin/v1/billing/rules/{rule_id}


6.2 费用预估

POST /api/v1/billing/estimate

{
  "model_id": "gpt-4o",
  "input_tokens": 1000,
  "output_tokens": 500
}

响应:

{
  "code": 0,
  "data": {
    "model_id": "gpt-4o",
    "model_name": "GPT-4o",
    "input_tokens": 1000,
    "output_tokens": 500,
    "input_cost": "0.028",
    "output_cost": "0.042",
    "total_cost": "0.070",
    "currency": "CNY",
    "applied_rule": {
      "membership_level": "basic",
      "input_price": "0.028",
      "output_price": "0.084"
    }
  }
}

6.3 用量统计

用量汇总

GET /api/v1/billing/usage

查询参数 说明
period today / week / month / custom
start_date period=custom 必填
end_date period=custom 必填
{
  "code": 0,
  "data": {
    "period": "month",
    "input_tokens": 3200000,
    "output_tokens": 1600000,
    "total_tokens": 4800000,
    "request_count": 8750,
    "total_cost": "134.40",
    "currency": "CNY"
  }
}

每日用量明细

GET /api/v1/billing/usage/daily

{
  "code": 0,
  "data": {
    "items": [
      {
        "date": "2024-01-01",
        "input_tokens": 128000,
        "output_tokens": 64000,
        "total_tokens": 192000,
        "request_count": 350,
        "total_cost": "5.38"
      }
    ]
  }
}

按模型分组费用

GET /api/v1/billing/usage/by-model

{
  "code": 0,
  "data": {
    "items": [
      {
        "model_id": "gpt-4o",
        "model_name": "GPT-4o",
        "input_tokens": 2000000,
        "output_tokens": 960000,
        "total_cost": "89.60",
        "request_count": 5200
      }
    ]
  }
}

按 API Key 分组费用

GET /api/v1/billing/usage/by-apikey

{
  "code": 0,
  "data": {
    "items": [
      {
        "key_id": "key_01HX...",
        "key_name": "生产环境",
        "key_prefix": "sk-prod-****",
        "total_tokens": 3080000,
        "total_cost": "86.24",
        "request_count": 5200
      }
    ]
  }
}

6.4 月度账单

账单列表

GET /api/v1/billing/invoices

{
  "code": 0,
  "data": {
    "items": [
      {
        "invoice_id": "inv_202401",
        "period": "2024-01",
        "total_tokens": 4800000,
        "total_cost": "134.40",
        "currency": "CNY",
        "status": "settled",
        "settled_at": "2024-02-01T00:00:00Z"
      }
    ]
  }
}

账单详情

GET /api/v1/billing/invoices/{invoice_id}

{
  "code": 0,
  "data": {
    "invoice_id": "inv_202401",
    "period": "2024-01",
    "total_tokens": 4800000,
    "total_cost": "134.40",
    "currency": "CNY",
    "status": "settled",
    "breakdown": [
      {
        "model_id": "gpt-4o",
        "model_name": "GPT-4o",
        "input_tokens": 2000000,
        "output_tokens": 960000,
        "total_cost": "89.60"
      },
      {
        "model_id": "qwen-turbo",
        "model_name": "通义千问 Turbo",
        "input_tokens": 1200000,
        "output_tokens": 640000,
        "total_cost": "44.80"
      }
    ],
    "settled_at": "2024-02-01T00:00:00Z"
  }
}

下载账单 PDF

GET /api/v1/billing/invoices/{invoice_id}/download

{
  "code": 0,
  "data": {
    "download_url": "https://oss.example.com/invoices/inv_202401.pdf",
    "expires_at": "2024-02-01T12:10:00Z"
  }
}