Skip to content

feat(token-usage): attribute token usage by agent - #7207

Merged
zhijianma merged 12 commits into
agentscope-ai:mainfrom
yuanxs21:token-usage-agent
Aug 24, 2026
Merged

feat(token-usage): attribute token usage by agent#7207
zhijianma merged 12 commits into
agentscope-ai:mainfrom
yuanxs21:token-usage-agent

Conversation

@yuanxs21

Copy link
Copy Markdown
Collaborator

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 request ContextVar agent 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 legacy provider:model rows. Old colon keys are left as-is (no migration).

GET /token-usage/details now returns one row per (date, agent, provider, model) with additive agent_id: null for pre-tracking rows, "" for recorded-but-unattributed. Console re-aggregates details as before; GET /token-usage summary shape is unchanged. The UI maps null and "" to Unattributed, and named ids through getAgentDisplayName.

This PR:

  • Adds peek_current_agent_id() and records agent_id on _UsageEvent / disk entries / TokenUsageRecord
  • Switches TokenRecordingModelWrapper usage reads to safe_attr(..., "usage") so ChatResponse (dict subclass) missing usage does not KeyError
  • Aggregates details into a By Agent table (token-total descending) next to By Model / By Date; i18n for byAgent / agent / unattributed (and labels LLM calls as such)
  • Leaves historical colon keys unattributed until they age out of the date window
  • Adds unit coverage in tests/unit/token_usage/test_token_usage.py (no merge into legacy rows, model fallback from unit-separator keys, named agent_id round-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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactoring

Component(s) Affected

  • Core / Backend (app, agents, config, providers, utils, local_models)
  • Console (frontend web UI)
  • Channels (DingTalk, Lark, QQ, Discord, iMessage, etc.)
  • Skills
  • CLI
  • Documentation (website)
  • Tests
  • CI/CD
  • Scripts / Deploy

Checklist

  • I ran pre-commit run --all-files locally and it passes
  • If pre-commit auto-fixed files, I committed those changes and reran checks
  • I ran tests locally (pytest or as relevant) and they pass
  • Documentation updated (if needed)
  • Ready for review

For Channel Changes (DingTalk, Lark, QQ, Console, etc.)

  • I ran ./scripts/check-channels.sh (or ./scripts/check-channels.sh --changed) and it passes
  • Contract test exists in tests/contract/channels/test_<channel>_contract.py (REQUIRED)
  • Contract test implements create_instance() with proper channel initialization
  • All 19 contract verification points pass (see tests/contract/channels/__init__.py)
  • Optional: Unit tests in tests/unit/channels/test_<channel>.py for complex internal logic

Testing

[How to test these changes]

Evidence

Examples of valid evidence:

  • Terminal transcript of the test run (e.g. pytest tests/unit/app/chats/ -q output)
  • Screenshot of the Console UI showing the fix
  • CI artifact link
  • pre-commit run --all-files summary
pre-commit run --all-files
# paste summary result

pytest
# paste summary result

Additional Notes

[Optional: any other context]

@yuanxs21
yuanxs21 requested a deployment to ai-review-approved August 21, 2026 09:06 — with GitHub Actions Waiting
@github-actions

Copy link
Copy Markdown

Welcome to QwenPaw! 🐾

Hi @yuanxs21, this is your 34th Pull Request.

🙌 Join Developer Community

Thanks 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:
https://qwenpaw.agentscope.io/docs/community

We truly appreciate your enthusiasm—and look forward to your future contributions! 😊

We'll review your PR soon.

@zhijianma
zhijianma merged commit 555a458 into agentscope-ai:main Aug 24, 2026
18 of 21 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in QwenPaw Aug 24, 2026
@yuanxs21
yuanxs21 deleted the token-usage-agent branch August 25, 2026 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants