Skip to content

Add token usage tracking to TokenCapChatClient and display usage summary - #15

Merged
JesseLiberty merged 1 commit into
mainfrom
Metrics
Aug 24, 2026
Merged

Add token usage tracking to TokenCapChatClient and display usage summary#15
JesseLiberty merged 1 commit into
mainfrom
Metrics

Conversation

@JesseLiberty

Copy link
Copy Markdown
Owner

TokenCapChatClient.cs: now tracks InputTokenCount, OutputTokenCount, and cumulative TotalTokenCount from every UsageDetails, plus reasoning tokens pulled from UsageDetails.AdditionalCounts["OutputTokenDetails.ReasoningTokenCount"] (how OpenAI reasoning-model usage is surfaced). Exposes a thread-safe UsageSnapshot (TokenUsageSnapshot record) with the four totals. The existing cap-enforcement behavior is unchanged.
Program.cs: captures the TokenCapChatClient instance built into the shared llm pipeline (all agents use it), and after printing the draft/review results, prints a "TOKEN USAGE" block with input, output, reasoning, and total token counts across the whole run.

Copilot AI lite review requested due to automatic review settings August 24, 2026 16:58
@JesseLiberty
JesseLiberty merged commit 61fc48c into main Aug 24, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new usage tracking surface area is not covered by existing unit tests, increasing regression risk for the added behavior.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR adds end-to-end token usage accounting to the shared IChatClient pipeline by accumulating per-call usage (input/output/total, plus reasoning tokens surfaced via UsageDetails.AdditionalCounts) and printing a final usage summary at the end of the run.

Changes:

  • Extend TokenCapChatClient to track cumulative input, output, reasoning, and total tokens and expose them via a TokenUsageSnapshot.
  • Capture the TokenCapChatClient instance when building the LLM middleware pipeline and print a “TOKEN USAGE” summary after workflow results.
File summaries
File Description
TokenCapChatClient.cs Adds cumulative token counters, reasoning-token extraction from AdditionalCounts, and a UsageSnapshot accessor.
Program.cs Captures the TokenCapChatClient from the pipeline and prints a final token usage summary block.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread TokenCapChatClient.cs
: throw new ArgumentOutOfRangeException(nameof(maxTotalTokens), maxTotalTokens, "Token cap must be a positive number.");
}

/// <summary>Cumulative token usage observed across every model round-trip so far.</summary>
Comment thread TokenCapChatClient.cs
Comment on lines +76 to +82
Interlocked.Add(ref _inputTokens, usage.InputTokenCount ?? 0);
Interlocked.Add(ref _outputTokens, usage.OutputTokenCount ?? 0);
if (usage.AdditionalCounts is { } additionalCounts &&
additionalCounts.TryGetValue(ReasoningTokenCountKey, out long reasoningTokens))
{
Interlocked.Add(ref _reasoningTokens, reasoningTokens);
}
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.

2 participants