fix(usage): 失败请求不计入总用量 / exclude failed requests from token aggregates - #401
Merged
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.
变更概述
本次修改调整了 Token 使用量、费用及配额消耗的统计口径:
usage_included字段,用于标记请求是否计入使用量。统计口径说明
虽然 OpenAI 官方文档没有明确承诺所有本地观察到的 499 或 502 请求都一定不会产生费用,但实际使用中大多数失败请求都不会产生计费。
例如,499 可能表示客户端超时或主动断开,但上游服务可能已经开始甚至完成处理。因此,仅凭本地 HTTP 状态码无法准确完成账单对账。
本项目的本地使用量统计统一采用以下口径:
数据库迁移
新增迁移:
crates/core/migrations/125_request_token_stats_successful_usage.sql迁移行为包括:
usage_included字段。兼容性说明
迁移前已经压缩、且同时包含成功和失败请求的混合小时数据,无法精确拆分。
这是因为旧版小时聚合结构没有分别保存成功请求和失败请求的 Token 数量。迁移后产生的新数据以及仍保留原始记录的数据,均会按照新口径准确统计。
测试情况
已通过:
cargo fmt --all -- --checkgit diff --checkcargo test -p codexmanager-corecargo test -p codexmanager-service --test rpc rpc_requestlog_list_and_summary_support_pagination -- --nocapture同时尝试执行了完整的
cargo test --workspace,过程中遇到:因此,本次修改涉及的核心存储包及请求日志 RPC 路径已分别完成针对性验证。