Skip to content

fix: move tool_costs outside the tier_rate computing#204

Merged
Mile-Away merged 1 commit intomainfrom
fix/decrease-consume
Jan 26, 2026
Merged

fix: move tool_costs outside the tier_rate computing#204
Mile-Away merged 1 commit intomainfrom
fix/decrease-consume

Conversation

@xinquiry
Copy link
Collaborator

@xinquiry xinquiry commented Jan 26, 2026

变更内容

  • 新功能
  • 修复 Bug
  • 增强重构
  • 其他(请描述)

简要描述本次 PR 的主要变更内容。

相关 Issue

请关联相关 Issue(如有):#编号

检查清单

默认已勾选,如不满足,请检查。

  • 已在本地测试通过
  • 已补充/更新相关文档
  • 已添加测试用例
  • 代码风格已经过 pre-commit 钩子检查

其他说明

如有特殊说明或注意事项,请补充。

Summary by Sourcery

Bug Fixes:

  • 防止在消耗计算中,工具相关成本被按分级费率乘数错误放大或缩小。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Prevent tool-related costs from being incorrectly scaled by the tier rate multiplier in consumption calculations.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 26, 2026

审查者指南(在小型 PR 上折叠显示)

审查者指南

调整消耗计算逻辑,使工具成本在应用分级费率倍数之后再进行添加,而不是与基础成本和 Token 成本一起乘以分级费率。

更新后的消耗计算逻辑流程图

flowchart TD
    A[Start calculate] --> B[Read input_tokens and output_tokens from ConsumptionContext]
    B --> C[Compute token_cost = input_tokens * INPUT_TOKEN_RATE + output_tokens * OUTPUT_TOKEN_RATE]
    C --> D[Compute base_amount = BASE_COST + token_cost]
    D --> E["Compute tiered_amount = int(base_amount * tier_rate)"]
    E --> F[Compute final_amount = tiered_amount + tool_costs]
    F --> G[Return ConsumptionResult with amount = final_amount]
Loading

文件级别改动

Change Details Files
将计价计算修改为在分级费率倍增计算时排除工具成本。
  • 仅使用 BASE_COST 和 token_cost 计算 base_amount,将工具成本排除在分级倍率部分之外。
  • 先用 tier_rate 乘以 base_amount,再单独加上工具成本来计算 final_amount。
  • 在仅更改金额计算的算术逻辑的前提下,保持 calculate 方法的整体结构和返回类型不变。
service/app/core/consume_strategy.py

技巧和命令

与 Sourcery 交互

  • 触发一次新的审查: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 在某条审查评论下回复,请 Sourcery 从该评论创建一个 issue。你也可以回复 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在你想要的位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来在任意时间(重新)生成摘要。
  • 生成审查者指南: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理完所有评论并且不想再看到它们,这会非常有用。
  • 忽略所有 Sourcery 审查: 在 pull request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审查。特别适用于你想用一次全新的审查重新开始的情况——别忘了再评论 @sourcery-ai review 来触发新的审查!

自定义你的使用体验

打开你的 dashboard 以:

  • 启用或禁用审查特性,比如由 Sourcery 生成的 pull request 摘要、审查者指南等。
  • 更改审查语言。
  • 添加、移除或编辑自定义审查指令。
  • 调整其他审查设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the consumption calculation so that tool costs are added after applying the tier rate multiplier, rather than being multiplied by the tier rate along with base and token costs.

Flow diagram for updated consumption calculation logic

flowchart TD
    A[Start calculate] --> B[Read input_tokens and output_tokens from ConsumptionContext]
    B --> C[Compute token_cost = input_tokens * INPUT_TOKEN_RATE + output_tokens * OUTPUT_TOKEN_RATE]
    C --> D[Compute base_amount = BASE_COST + token_cost]
    D --> E["Compute tiered_amount = int(base_amount * tier_rate)"]
    E --> F[Compute final_amount = tiered_amount + tool_costs]
    F --> G[Return ConsumptionResult with amount = final_amount]
Loading

File-Level Changes

Change Details Files
Change pricing calculation to exclude tool costs from tier-based multiplier.
  • Compute base_amount only from BASE_COST and token_cost, excluding tool_costs from the tier-multiplied portion.
  • Calculate final_amount by multiplying base_amount by tier_rate and then adding tool_costs separately.
  • Preserve the overall structure and return type of the calculate method while changing only the arithmetic for amount computation.
service/app/core/consume_strategy.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并留下了一些总体反馈:

  • 行内注释 # Tier rate multiplies ALL costs (including tool costs) 现在已经过时了,因为 context.tool_costs 是在应用 tier_rate 之后才被加上的,请更新该注释以反映新的行为。
  • 请考虑舍入行为的变化是否是有意为之:之前 tool_costs 被包含在传给 int(...) 的值中,而现在它们是在截断之后再相加的,这会在某些输入下略微改变总金额。
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The inline comment `# Tier rate multiplies ALL costs (including tool costs)` is now outdated given that `context.tool_costs` are added after applying `tier_rate`, so please update the comment to reflect the new behavior.
- Consider whether the change in rounding behavior is intentional: previously `tool_costs` were included in the value passed to `int(...)`, whereas now they are added after truncation, which can slightly change total amounts for some inputs.

## Individual Comments

### Comment 1
<location> `service/app/core/consume_strategy.py:102-104` </location>
<code_context>
         token_cost = context.input_tokens * self.INPUT_TOKEN_RATE + context.output_tokens * self.OUTPUT_TOKEN_RATE

         # Tier rate multiplies ALL costs (including tool costs)
-        base_amount = self.BASE_COST + token_cost + context.tool_costs
-        final_amount = int(base_amount * tier_rate)
+        base_amount = self.BASE_COST + token_cost
+        final_amount = int(base_amount * tier_rate) + context.tool_costs

         return ConsumptionResult(
</code_context>

<issue_to_address>
**issue:** The inline comment no longer matches the updated calculation for tool costs.

The code now excludes `context.tool_costs` from the tier multiplier while the inline comment says all costs are multiplied. Please either update the comment to match the current behavior (tool costs are added after applying `tier_rate`) or, if tool costs are meant to scale with `tier_rate`, revert the calculation so they are included in `base_amount` before the multiplier.
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的代码审查有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • The inline comment # Tier rate multiplies ALL costs (including tool costs) is now outdated given that context.tool_costs are added after applying tier_rate, so please update the comment to reflect the new behavior.
  • Consider whether the change in rounding behavior is intentional: previously tool_costs were included in the value passed to int(...), whereas now they are added after truncation, which can slightly change total amounts for some inputs.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The inline comment `# Tier rate multiplies ALL costs (including tool costs)` is now outdated given that `context.tool_costs` are added after applying `tier_rate`, so please update the comment to reflect the new behavior.
- Consider whether the change in rounding behavior is intentional: previously `tool_costs` were included in the value passed to `int(...)`, whereas now they are added after truncation, which can slightly change total amounts for some inputs.

## Individual Comments

### Comment 1
<location> `service/app/core/consume_strategy.py:102-104` </location>
<code_context>
         token_cost = context.input_tokens * self.INPUT_TOKEN_RATE + context.output_tokens * self.OUTPUT_TOKEN_RATE

         # Tier rate multiplies ALL costs (including tool costs)
-        base_amount = self.BASE_COST + token_cost + context.tool_costs
-        final_amount = int(base_amount * tier_rate)
+        base_amount = self.BASE_COST + token_cost
+        final_amount = int(base_amount * tier_rate) + context.tool_costs

         return ConsumptionResult(
</code_context>

<issue_to_address>
**issue:** The inline comment no longer matches the updated calculation for tool costs.

The code now excludes `context.tool_costs` from the tier multiplier while the inline comment says all costs are multiplied. Please either update the comment to match the current behavior (tool costs are added after applying `tier_rate`) or, if tool costs are meant to scale with `tier_rate`, revert the calculation so they are included in `base_amount` before the multiplier.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 102 to +104
# Tier rate multiplies ALL costs (including tool costs)
base_amount = self.BASE_COST + token_cost + context.tool_costs
final_amount = int(base_amount * tier_rate)
base_amount = self.BASE_COST + token_cost
final_amount = int(base_amount * tier_rate) + context.tool_costs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: 行内注释不再与工具成本的最新计算方式一致。

现在代码将 context.tool_costs 排除在层级乘数之外,但行内注释却说明所有成本都会被乘以该系数。请更新注释以匹配当前行为(工具成本是在应用 tier_rate 之后再相加),或者如果工具成本本来就应该随 tier_rate 一起缩放,请恢复为之前的计算方式,使它们在乘数之前就被包含在 base_amount 中。

Original comment in English

issue: The inline comment no longer matches the updated calculation for tool costs.

The code now excludes context.tool_costs from the tier multiplier while the inline comment says all costs are multiplied. Please either update the comment to match the current behavior (tool costs are added after applying tier_rate) or, if tool costs are meant to scale with tier_rate, revert the calculation so they are included in base_amount before the multiplier.

@codecov
Copy link

codecov bot commented Jan 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Mile-Away Mile-Away merged commit f391ca9 into main Jan 26, 2026
10 checks passed
@Mile-Away Mile-Away deleted the fix/decrease-consume branch January 26, 2026 04:50
Mile-Away pushed a commit that referenced this pull request Jan 26, 2026
## [1.0.10](v1.0.9...v1.0.10) (2026-01-26)

### 🐛 Bug Fixes

* move tool_costs outside the tier_rate computing ([#204](#204)) ([f391ca9](f391ca9))
@Mile-Away
Copy link
Contributor

🎉 This PR is included in version 1.0.10 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants