Skip to content

Fix two issues with costs#1732

Merged
dgageot merged 2 commits intodocker:mainfrom
dgageot:fix-costs
Feb 14, 2026
Merged

Fix two issues with costs#1732
dgageot merged 2 commits intodocker:mainfrom
dgageot:fix-costs

Conversation

@dgageot
Copy link
Member

@dgageot dgageot commented Feb 14, 2026

No description provided.

Move the RateLimit assignment inside the Usage nil-check block.
When res.Usage is nil but res.RateLimit is non-nil (e.g. Anthropic
with track_usage disabled), msgUsage stays nil and dereferencing it
panics.

Assisted-By: cagent
Providers like Gemini emit cumulative token usage on every stream chunk.
The previous code used sess.Cost += on each emission, re-adding the full
cost every time. Track the cost already contributed by the current stream
and only apply the delta so the session total stays correct.

Assisted-By: cagent
@dgageot dgageot requested a review from a team as a code owner February 14, 2026 09:43
Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

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

Review Summary

No issues found

The changes correctly implement two important bug fixes:

  1. RateLimit indentation fix (line 1096): Properly moves the RateLimit assignment inside the conditional block, ensuring the pointer is only dereferenced when not nil.

  2. Cost calculation fix (lines 1273-1301):

    • Introduces prevStreamCost to track cumulative costs and prevent double-counting in streaming responses
    • Fixes parenthesis placement to ensure the entire cost calculation is divided by 1e6 (not just the last term)
    • Correctly implements differential cost tracking: sess.Cost += streamCost - prevStreamCost

Both changes are well-implemented and address real issues in the cost tracking logic.

@dgageot dgageot merged commit 8d89956 into docker:main Feb 14, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants