pydantic_ai.messages.ModelResponse.usage and pydantic_ai.usage.RequestUsage
Tickets Created (max 5)
Ticket ID
Title
Priority
Status
pending
Not created yet
-
-
Dependencies
T2 depends on T1 (helper available for UI consumption)
T3 depends on M1 decision on canonical token source
1---2name: 1497-2026-02-02-16-46-20-pydantic-usage-token-counting-1a56c3description: Goal4---56## Goal78Stop per-message heuristic token counting and use pydantic-ai per-call usage totals as the source of truth for token display.910**Non-goals:**11- No UI redesigns or visual style changes12- No changes to LLM providers or retry logic13- No new dependencies1415## Scope & Assumptions1617**In scope:**18- Replace the UI token source with pydantic-ai usage totals19- Remove or retire per-message `estimate_messages_tokens` usage in runtime flows20- Ensure persisted sessions and runtime state stay coherent2122**Out of scope:**23- Any change to tool output pruning heuristics24- Pricing model changes25- Streaming logic changes2627**Verified facts (no assumptions):**28- `ModelResponse.usage` is always present but can be empty (pydantic-ai returns zeroed `RequestUsage` when provider usage is missing).29- `RequestUsage` exposes deprecated `request_tokens`/`response_tokens` properties (mapped to `input_tokens`/`output_tokens`).30- `RequestUsage` does not provide a `cached_tokens` attribute (we currently default cached tokens to 0 in normalization).31- Using usage totals will reflect *API usage totals*, not context window size.3233## Deliverables (DoD)34351. UI token display uses accumulated pydantic-ai usage totals362. Heuristic per-message token counting is no longer invoked in core agent flow373. No new mypy errors; ruff passes384. Existing tests pass (if any touch these paths)3940## Readiness (DoR)4142- [x] Current call sites for token counting identified43- [x] Pydantic-ai usage fields verified4445## Milestones4647- **M1:** Define new canonical token source for UI (usage totals)48- **M2:** Remove core flow calls to `update_token_count()`49- **M3:** Wire UI resource bar to usage totals50- **M4:** Validate persisted session compatibility5152## Work Breakdown (Tasks)5354| Task | ID | Summary | File(s) | Acceptance Tests |55|------|-----|---------|---------|------------------|56| T1 | tk-new | Add helper to derive total usage tokens (input+output) from session usage totals | `core/agents/agent_components/orchestrator/usage_tracker.py`, `types/canonical.py` | Existing tests pass |57| T2 | tk-new | Replace resource bar token source with usage totals | `ui/app.py`, `ui/widgets/resource_bar.py` | Manual run: resource bar updates post-request |58| T3 | tk-new | Remove per-message token count updates from agent flow | `core/agents/main.py`, `core/state.py` | Existing tests pass |59| T4 | tk-new | Update session persistence defaults/migrations if needed | `core/state.py`, `core/types/state_structures.py` | Load old session works |6061## Risks & Mitigations6263| Risk | Impact | Likelihood | Mitigation | Trigger |64|------|--------|------------|------------|---------|65| Usage totals differ from context window size | Medium | High | Document change in behavior and UI meaning | Token display confusion |66| Missing usage for some providers (usage == 0) | Medium | Medium | Decide whether to surface explicit warning vs. accept zero totals | Zero usage observed |67| Cached token counts unavailable from RequestUsage | Low | High | Map cache_read/write to cached tokens or keep cached=0 | Cached token reporting needed |68| Legacy sessions lack totals | Medium | Medium | Default to zero and accumulate forward | Session load errors |6970## Test Strategy7172- `uv run pytest` (full suite)73- Manual: run a request, confirm resource bar tokens increase by usage totals7475## References7677- `src/tunacode/core/agents/agent_components/orchestrator/orchestrator.py` (where usage is read)78- `src/tunacode/core/agents/agent_components/orchestrator/usage_tracker.py` (usage accumulation)79- `src/tunacode/ui/app.py` (resource bar update)80- `pydantic_ai.messages.ModelResponse.usage` and `pydantic_ai.usage.RequestUsage`8182## Tickets Created (max 5)8384| Ticket ID | Title | Priority | Status |85|-----------|-------|----------|--------|86| pending | Not created yet | - | - |8788## Dependencies8990- T2 depends on T1 (helper available for UI consumption)91- T3 depends on M1 decision on canonical token source
Run npx skillmds@latest add tools-only/1497-2026-02-02-16-46-20-pydantic-usage-token-counting-1a56c in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Goal It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tools-only (@tools-only) published this skill. Their other Agent Skills are listed on their SkillMD profile.