cost: read qwen's usage log - #420
Merged
Merged
Conversation
qwen was listed as an engine that keeps no readable record, and that has stopped being true: it appends one record per request to ~/.qwen/usage/token-usage-YYYY-MM.jsonl, and writes the directory it was started in to ~/.qwen/projects/<slug>/chats/<session>.runtime.json. So a qwen session launched through /agents showed up nowhere in /usage. Two pieces of its arithmetic are easy to get wrong, and both are checked against qwen-code's own conversion rather than guessed: - `cachedTokens` is part of `inputTokens`, so fresh input is the difference — the same trap codex's cumulative counts have. - `thoughtsTokens` is already inside `outputTokens` on the OpenAI-compatible path (`completion_tokens` contains `reasoning_tokens`), so adding it would double-count the thinking. Only the native path, which reports Gemini's `candidatesTokenCount`, has to add it back. No Alibaba rates are shipped — that stays deliberate — so qwen runs report tokens and no cost, and the report already says how to price them. Verified against this machine's real log: 314,485 in / 54,574 out / 8.34M cached across 3 sessions, matching a raw sum of the file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
ThreatCrush Security Scan3 finding(s) in the 3 file(s) this pull request changes. MEDIUM: 3
52 pre-existing finding(s) elsewhere in the repository — **HIGH/CRITICAL**: 5 | **MEDIUM**: 41 | **LOW**: 6Not introduced by this pull request. The full set is in the Security tab.
…and 32 more. Full results in the Security tab. Snippets are redacted; ThreatCrush never prints matched credential material. |
Merged
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.
Follow-on to #418.
/usagecovers whatever/agentslaunched — but only for engines with a reader, andqwenwas listed as one that keeps no readable record. That has stopped being true, so a qwen session showed up nowhere in the report.qwen writes:
~/.qwen/usage/token-usage-YYYY-MM.jsonl— one record per request: timestamp, sessionId, model, and the token counts~/.qwen/projects/<slug>/chats/<session>.runtime.json— a tiny file carryingwork_dir, which is the only exact directory on disk (the project slug is lossy, like Claude's)So the reader groups requests by session, and attributes each session to the directory qwen was started in — subagent requests carry the session that spawned them, so they land on the right run.
The arithmetic
Both traps were checked against qwen-code's own response conversion, not guessed:
cachedTokensis part ofinputTokens, so fresh input is the difference — counting both would bill the cache twice at the full input rate.thoughtsTokensis already insideoutputTokenson the OpenAI-compatible path (authType: "openai", whereoutputTokensiscompletion_tokensand that containsreasoning_tokens), so adding it would double-count thinking. The native path reports Gemini'scandidatesTokenCount, which excludes thoughts — that is the one case where they are added back.No Alibaba rates are shipped; that stays deliberate. qwen runs report tokens with no cost, and the report already prints how to price them in
~/.moshcode/pricing.json.Verification
Against this machine's real log: 314,485 in / 54,574 out / 8.34M cached across 3 sessions, matching a raw sum of the file, with the two known directories attributed correctly.
Five new tests cover the session sum, cache netting, both thinking paths, the cwd filter, the window, and a session with no runtime file. Full suite: 1980 passing, 0 failing.
Still uncosted: gemini, kimi, deepseek, openagents — none are installed here, so there was nothing to verify a reader against.
🤖 Generated with Claude Code