fix(usage): correct inflated input_tokens and Zhipu quota routing#264
Open
thedavidweng wants to merge 3 commits into
Open
fix(usage): correct inflated input_tokens and Zhipu quota routing#264thedavidweng wants to merge 3 commits into
thedavidweng wants to merge 3 commits into
Conversation
Owner
|
@thedavidweng Thanks for the focused fix. I found one small issue before this is ready: Could you also add small tests for the delta-input override case in the usage parser and the Zhipu quota base selection/sorting behavior? After that, this should be in good shape. |
- Prefer message_delta input_tokens over message_start when the delta value is smaller, fixing double-counting on providers (Qwen, MiniMax) that report fresh+cached as input_tokens in message_start - Sync cache counts from the same delta block when adopting its input_tokens to avoid stale cache_read/cache_creation values - Route Zhipu quota queries to the endpoint matching the user's configured base_url instead of always hitting api.z.ai - Sort Zhipu tiers by nextResetTime with missing values first, so the 5-hour bucket (just reset, 0% utilization) displays correctly Closes SaladDay#259
- Apply .to_lowercase() in zhipu_quota_base() to match detect_provider() behavior, so uppercase URLs like OPEN.BIGMODEL.CN route correctly - Add tests for delta-input override in Claude stream parsing (inflated start overridden by smaller delta, larger delta ignored, zero start always adopts delta) - Add tests for zhipu_quota_base case-insensitivity and consistency with detect_provider - Add tests for Zhipu tier sorting (None resets_at first, ascending)
d456368 to
77db2f1
Compare
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.
Summary
1. Inflated input_tokens in Claude stream parsing
Some Anthropic-compatible SSE providers (e.g. Qwen, MiniMax) report the full context (fresh + cached) as
input_tokensinmessage_start, double-counting the cached portion. Themessage_deltahandler only adoptedinput_tokenswhen the start value was exactly zero, so the correct (smaller) delta value was silently discarded.Fix: When
message_deltaprovides a positiveinput_tokenssmaller than the current value, prefer it and sync cache counts from the same delta block.2. Zhipu quota query hardcoded to international endpoint
query_zhipu()always hithttps://api.z.ai, making quota queries fail for users on the mainland China preset (open.bigmodel.cn).Fix: Route by the configured
base_url—open.bigmodel.cnusers queryopen.bigmodel.cn, others queryapi.z.ai.3. Zhipu tier sorting
When the 5-hour bucket is at 0% utilization, the API omits
nextResetTime. Without sorting, the weekly bucket could incorrectly claim the five-hour slot.Fix: Sort tiers with missing
nextResetTimefirst (just-reset 5-hour bucket), then by reset time ascending.Changes
src-tauri/src/proxy/usage/parser.rs— updatefrom_claude_stream_eventsdelta handlingsrc-tauri/src/services/coding_plan.rs— addzhipu_quota_basehelper, updatequery_zhipusignature, add tier sortingCloses #259