Show task tokens and usage estimates in the agent command center (#44970)

## What changed

- Display input/output token counts and estimated credits and USD cost in task details. Prefer live token totals, falling back to complete totals from usage breakdowns.
- Fetch estimates for the selected task on a one-minute cadence for supported Business and Enterprise plans. Cache results, preserve prior nonzero estimates when a response reports zero, and stop fetching when the capability is unavailable.
- Clear cached usage and discard pending results on account changes, reconnects, or missed server events. Prioritize activity and usage over the original prompt when space is limited.

## Testing

Add a rendering snapshot and tests covering caching, stale results, unavailable usage, token resets, and incomplete usage breakdowns.

GitOrigin-RevId: b72c96053557389fdc723a70fab0dab841a52e49
This commit is contained in:
Eric Traut
2026-09-12 04:29:48 +00:00
committed by copyberry
parent 7efb0262d6
commit aee8a55ab6
15 changed files with 533 additions and 4 deletions
@@ -48,6 +48,16 @@ impl App {
.get_mut(&thread_id)
.and_then(Option::as_mut);
match notification {
ServerNotification::ThreadTokenUsageUpdated(usage) => {
if self.agents_overview.threads.contains_key(&thread_id) {
self.agents_overview
.usage
.entry(thread_id)
.or_default()
.tokens = Some(usage.token_usage.total.clone());
self.repaint_agents_overview();
}
}
ServerNotification::ThreadStarted(started) => {
if started.thread.ephemeral {
return;
@@ -59,11 +69,15 @@ impl App {
ServerNotification::ThreadArchived(_) | ServerNotification::ThreadDeleted(_) => {
self.agents_overview.activity.remove(&thread_id);
self.agents_overview.last_messages.remove(&thread_id);
self.agents_overview.usage.remove(&thread_id);
self.agents_overview.threads.remove(&thread_id);
self.agents_overview.refresh_thread_ids.remove(&thread_id);
}
ServerNotification::ThreadClosed(_) => {
self.agents_overview.activity.remove(&thread_id);
if let Some(usage) = self.agents_overview.usage.get_mut(&thread_id) {
usage.tokens = None;
}
if let Some(thread) = thread {
thread.status = ThreadStatus::NotLoaded;
}
@@ -71,6 +85,9 @@ impl App {
ServerNotification::ThreadReverted(_) => {
self.agents_overview.activity.remove(&thread_id);
self.agents_overview.last_messages.remove(&thread_id);
if let Some(usage) = self.agents_overview.usage.get_mut(&thread_id) {
usage.tokens = None;
}
self.repaint_agents_overview();
}
ServerNotification::ThreadStatusChanged(status) => {
@@ -94,8 +111,10 @@ impl App {
}
_ => return,
}
if !matches!(notification, ServerNotification::ThreadReverted(_))
&& self.agents_overview.threads.contains_key(&thread_id)
if !matches!(
notification,
ServerNotification::ThreadReverted(_) | ServerNotification::ThreadTokenUsageUpdated(_)
) && self.agents_overview.threads.contains_key(&thread_id)
{
self.agents_overview.refresh_thread_ids.insert(thread_id);
}