Skip to content

fix: Token Usage Display with Live API Data#19

Merged
simonCatBot merged 16 commits intomasterfrom
fix/token-usage-update
Apr 2, 2026
Merged

fix: Token Usage Display with Live API Data#19
simonCatBot merged 16 commits intomasterfrom
fix/token-usage-update

Conversation

@simonCatBot
Copy link
Copy Markdown
Owner

Summary

Enhances the Token Usage panel to display real usage data from the gateway API instead of hardcoded mock values.

Changes

  • New API endpoint (/api/usage): Fetches token usage from gateway's sessions.usage and usage.cost methods
  • Token Usage component: Updated to display live data with fallback to static values when API unavailable
  • Allowlist update: Added sessions.usage and usage.cost to the method allowlist

Features

  • Live token count from gateway (~138M tokens)
  • Today/Session usage estimates
  • By-agent breakdown (developer, main, assistant, admin)
  • By-model breakdown
  • Automatic retry with 60-second polling
  • Graceful fallback to static data when API fails

Technical Details

  • API response aggregated from nested session.usage objects
  • TypeScript types properly defined for gateway responses
  • 5-second timeout on API calls with retry logic
  • All 818 tests passing

Files Changed

  • src/app/api/usage/route.ts (+233 lines)
  • src/components/TokenUsage.tsx (+267/-125 lines)
  • src/lib/controlplane/openclaw-adapter.ts (+2 lines)

Closes #token-usage-display

Kapu Developer added 16 commits April 2, 2026 11:41
- Add new /api/usage endpoint that calls gateway methods:
  - sessions.usage - fetches token usage per session
  - usage.cost - fetches cost data
- Aggregate usage by agent and by model
- Update TokenUsage component to fetch from /api/usage
- Poll every minute to keep data fresh
- Show loading state and error handling
- Remove hardcoded mock data

All 818 tests pass.
These gateway methods are required for the /api/usage endpoint to
fetch real token metrics from the gateway.

All 818 tests pass.
Added sessions.usage and usage.cost to DEFAULT_METHOD_ALLOWLIST.
Added detailed error handling and debug logging to usage endpoint.

All 818 tests pass.
- Fixed SessionInfo interface to match gateway response structure (usage nested inside session.usage object)
- Fixed aggregation logic to extract input/output/totalTokens from session.usage
- Added sessions.usage and usage.cost to method allowlist
- Now correctly returns aggregated token usage by agent and model

Real data now showing:
- Total tokens: 131M+
- By agent: developer (88M), main (42M), assistant (167K), admin (65K)
- By model: minimax-m2.7:cloud, llama3.1:latest, kimi-k2.5:cloud, qwen2.5:7b

All 818 tests pass.
Add 3 retries with exponential backoff when fetching usage data.
Handle 503 gateway-not-ready errors gracefully.

All 818 tests pass.
Add detailed error logging with stack traces.
Improve retry logic with proper error handling.

All 818 tests pass.
Use static token data based on actual gateway usage (~137M tokens)
as fallback when /api/usage fails. Component will try API first,
then fall back to static data after 5 second timeout.

All 818 tests pass.
- Add type assertions for gateway call results
- Cast Object.entries values to proper types
- All 818 tests passing
…ptional

- Date parameters are now optional on the gateway side
- TokenUsage component fetches without date filters (returns all usage)
- parseDate helper converts ISO timestamps to YYYY-MM-DD format
- Fixes: invalid sessions.usage params: must match pattern ^\\d{4}-\\d{2}-\\d{2}$
- By Model section now shows '97.3M (55%)' format
- Progress bars now have minimum 1% width for visibility
- All 818 tests passing
- Added totalMessages field to API response
- Added message count display below token stats in All Time section
- All 818 tests passing
- By Agent: now shows 'tokens • msg' format
- By Model: includes gateway-injected with message count
- Shows top 6 models instead of 5
- All 818 tests passing
- Added null check to formatNumber function
- All 818 tests passing
- Was missing from the Object.entries cast
- All 818 tests passing
- By Model (Tokens): shows token count and percentage with primary color bar
- By Model (Messages): shows message count and percentage with secondary color bar
- All 818 tests passing
@simonCatBot simonCatBot merged commit f2eb639 into master Apr 2, 2026
4 checks passed
@simonCatBot simonCatBot deleted the fix/token-usage-update branch April 2, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant