fix(cost): count V4 reasoning tokens in usage output#762
Conversation
There was a problem hiding this comment.
Code Review
This pull request bumps the version of the workspace and all internal crates from 0.8.13 to 0.8.14. It also updates the usage parsing logic in the TUI client to correctly account for reasoning tokens when completion tokens are reported as zero, ensuring accurate cost calculations. A review comment suggests simplifying the token normalization logic using .max() to improve robustness and avoid the use of unstable language features.
| if output_tokens == 0 | ||
| && let Some(reasoning_tokens) = reasoning_tokens_raw | ||
| { | ||
| output_tokens = reasoning_tokens; | ||
| } |
There was a problem hiding this comment.
The logic to normalize output_tokens can be simplified and made more robust by using .max(). This ensures that output_tokens is at least the number of reasoning tokens, even if completion_tokens is non-zero but incorrectly reported as less than the reasoning count by the API. Additionally, this avoids the use of the unstable let_chains feature, which may cause compilation issues on stable Rust toolchains.
output_tokens = output_tokens.max(reasoning_tokens_raw.unwrap_or(0));
Closes #754
Summary
completion_tokens_details.reasoning_tokensas output tokens when the API reports zero completion tokensTest plan
cargo fmt --all -- --checkcargo test -p deepseek-tui parse_usage_counts_reasoning_tokens_when_completion_tokens_are_zero --lockedcargo build --workspace --all-features --lockedcargo clippy --workspace --all-targets --all-features --locked -- -D warningscargo test --workspace --all-features --locked