Skip to content

feat: Cost Management & Budget Control #52

@pescn

Description

@pescn

Summary

Implement cost management and budget control features at the API Key level.

Requirements

Budget System

  • One-time quota allocation (non-periodic)
  • Real-time cost calculation based on models.inputPrice and models.outputPrice
  • API Key level budget management

Alert Thresholds (Configurable)

Threshold Notify Action
80% API Key Owner Warning
90% API Key Owner Warning
95% API Key Owner + Admin Urgent Warning
100% API Key Owner + Admin Warning + Soft Limit

Soft Limit Strategy

  • At 100%: Reject new requests, allow in-progress requests to complete
  • Alert debounce: Same threshold alert interval ≥ 1 hour (configurable)

Database Changes

  • api_keys table: Add budget_limit, budget_used, budget_alert_thresholds, owner_email
  • completions table: Add calculated_cost
  • embeddings table: Add calculated_cost

API Endpoints

  • GET /api/admin/costs/overview - Cost overview
  • GET /api/admin/costs/by-key - Group by API Key
  • GET /api/admin/costs/by-model - Group by model
  • PUT /api/admin/apiKey/:id/budget - Set budget

概要

实现 API Key 级别的成本管理和预算控制功能。

需求

预算系统

  • 一次性额度分配(非周期性)
  • 基于 models.inputPricemodels.outputPrice 实时计算成本
  • API Key 级别的预算管理

告警阈值(可配置)

阈值 通知对象 行为
80% API Key 所有者 预警
90% API Key 所有者 预警
95% API Key 所有者 + 管理员 紧急预警
100% API Key 所有者 + 管理员 预警 + 软限制

软限制策略

  • 100% 时:拒绝新请求,允许进行中的请求正常完成
  • 告警防抖:同一阈值告警间隔 ≥ 1小时(可配置)

数据库变更

  • api_keys 表新增:budget_limit, budget_used, budget_alert_thresholds, owner_email
  • completions 表新增:calculated_cost
  • embeddings 表新增:calculated_cost

API 端点

  • GET /api/admin/costs/overview - 成本总览
  • GET /api/admin/costs/by-key - 按 API Key 分组
  • GET /api/admin/costs/by-model - 按模型分组
  • PUT /api/admin/apiKey/:id/budget - 设置预算

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions