feat(token-usage): attribute token usage by agent - #7207
Merged
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
yuanxs21
requested a deployment
to
ai-review-approved
August 21, 2026 09:06 — with
GitHub Actions
Waiting
|
Hi @yuanxs21, this is your 34th Pull Request. 🙌 Join Developer CommunityThanks so much for your contribution! We'd love to invite you to join the official QwenPaw developer group! You can find the Discord and DingTalk group links under the "Developer Community" section on our docs page: We truly appreciate your enthusiasm—and look forward to your future contributions! 😊 We'll review your PR soon. |
yuanxs21
requested a deployment
to
maintainer-approved
August 21, 2026 09:06 — with
GitHub Actions
Waiting
yuanxs21
requested a deployment
to
ai-review-approved
August 21, 2026 09:18 — with
GitHub Actions
Waiting
yuanxs21
had a problem deploying
to
maintainer-approved
August 21, 2026 09:18 — with
GitHub Actions
Failure
zhijianma
approved these changes
Aug 24, 2026
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.

Description
Token usage was stored and shown only as
(date, provider, model), so Settings → Token Usage could not tell which agent consumed the tokens. This PR stamps the requestContextVaragent id onto each usage event and adds a By Agent table on the Token Usage page.Recording reads
peek_current_agent_id(). Unset context is stored as"". New rows use unit-separator keys, so they do not merge into legacyprovider:modelrows. Old colon keys are left as-is (no migration).GET /token-usage/detailsnow returns one row per(date, agent, provider, model)with additiveagent_id:nullfor pre-tracking rows,""for recorded-but-unattributed. Console re-aggregates details as before;GET /token-usagesummary shape is unchanged. The UI mapsnulland""to Unattributed, and named ids throughgetAgentDisplayName.This PR:
peek_current_agent_id()and recordsagent_idon_UsageEvent/ disk entries /TokenUsageRecordTokenRecordingModelWrapperusage reads tosafe_attr(..., "usage")soChatResponse(dict subclass) missingusagedoes notKeyErrorbyAgent/agent/unattributed(and labels LLM calls as such)tests/unit/token_usage/test_token_usage.py(no merge into legacy rows, model fallback from unit-separator keys, namedagent_idround-trip, ContextVar set/empty and no active-agent fallback)Related Issue: Fixes #(issue_number) or Relates to #(issue_number)
Security Considerations: [If applicable, e.g. channel auth, env/config handling]
Type of Change
Component(s) Affected
Checklist
pre-commit run --all-fileslocally and it passespytestor as relevant) and they passFor Channel Changes (DingTalk, Lark, QQ, Console, etc.)
./scripts/check-channels.sh(or./scripts/check-channels.sh --changed) and it passestests/contract/channels/test_<channel>_contract.py(REQUIRED)create_instance()with proper channel initializationtests/contract/channels/__init__.py)tests/unit/channels/test_<channel>.pyfor complex internal logicTesting
[How to test these changes]
Evidence
Examples of valid evidence:
pytest tests/unit/app/chats/ -qoutput)pre-commit run --all-filessummaryAdditional Notes
[Optional: any other context]