fix(openai): record zero-cost usage for unpriced models#2319
Open
wucm667 wants to merge 1 commit intoWei-Shaw:mainfrom
Open
fix(openai): record zero-cost usage for unpriced models#2319wucm667 wants to merge 1 commit intoWei-Shaw:mainfrom
wucm667 wants to merge 1 commit intoWei-Shaw:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
修复 OpenAI 兼容接口在模型缺少定价时,成功请求没有写入使用记录的问题。
之前请求已经成功返回,但如果计费模型找不到价格,异步用量记录任务会在写入
usage_logs前失败,导致这类自定义模型 / 第三方 OpenAI-compatible 模型在使用记录里完全不可见。这个 PR 将“缺少定价”改为可恢复场景:仍然记录一条 0 费用的使用记录,保留 token、模型、账号、API key、请求耗时等用量信息。
变更内容
ErrModelPricingUnavailable,用于通过errors.Is判断模型定价缺失。total_cost = 0、actual_cost = 0的使用记录。openai_usage.pricing_missing_record_zero_cost。说明
这个改动不会为未知模型补价格,也不会对未知模型扣费。
如果需要准确计费,管理员仍然需要配置渠道定价或补充模型价格数据。
这个 PR 的目标是:请求成功时,即使价格缺失,也不要丢失用量可见性。
测试
GOCACHE=/private/tmp/sub2api-go-build go test -tags=unit ./internal/service结果