Skip to content
Back to skills

481 Usage Total Token Source A59178db

ASecurity

The canonical token total for UI display is the accumulated pydantic-ai usage totals (prompt + completion) from `usage.session_total_usage`, not heuristic message counting. This aligns the UI with billed API usage and avoids slow per-message estimation.

  • 9 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added June 1, 2026
ai-agentsapi

Works with

  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned June 1, 2026

npx -y skills add tools-only/X-Skills --skill 481-usage-total-token-source_a59178db --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 481 Usage Total Token Source A59178db?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 481 Usage Total Token Source A59178db
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-481-usage-total-token-source-a59178db/badge)](https://www.skillsdirectory.com/skills/tools-only-481-usage-total-token-source-a59178db)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
title: Canonical token total uses usage totals
link: usage-total-token-source
type: doc
path: src/tunacode/core/agents/agent_components/orchestrator/usage_tracker.py
depth: 0
seams: [S, M]
ontological_relations:
  - relates_to: [[usage-tracking]]
  - affects: [[resource-bar]]
  - affects: [[session-usage-metrics]]
tags:
  - tokens
  - usage
  - ui
created_at: 2026-02-02T16:59:25-06:00
updated_at: 2026-02-02T16:59:25-06:00
uuid: d78e6d77-0ca6-4050-9a14-8da069b2325a
---

# Canonical token total uses usage totals

## Summary

The canonical token total for UI display is the accumulated pydantic-ai usage totals
(prompt + completion) from `usage.session_total_usage`, not heuristic message counting.
This aligns the UI with billed API usage and avoids slow per-message estimation.

## Context

- The UI previously used `conversation.total_tokens`, which is computed by
  `estimate_messages_tokens` over the full message list.
- Pydantic-ai always exposes a `RequestUsage`, but providers may return all zeros when
  usage is unavailable.
- `RequestUsage` does not expose cached token counts, so cache usage cannot be reported.

## Root Cause

Heuristic token counting is both slow on large histories and inconsistent with actual
API usage totals, causing UI token numbers to drift from provider-reported usage.

## Changes

- Define the canonical token total as `usage.session_total_usage.total_tokens`
  (prompt + completion).
- Treat zero-usage responses as valid zeros; do not fall back to heuristics.
- Keep cached tokens at zero because `RequestUsage` does not supply them.
- Retain `conversation.total_tokens` only for context-window sizing, not UI usage totals.

## Behavioral Impact

- The UI token display reflects API usage totals and may remain at 0 when providers omit
  usage data.
- Cached token counts are not displayed until providers supply a value.
- Context-window size is no longer conflated with usage totals.

## Related Cards

- [[usage-tracking]]
- [[resource-bar]]

Files in this skill

  • README.md1.1 KB
  • skill.md1.9 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.