Skip to content

feat(copilotcli): enhance usage reporting in CopilotCLISession with detailed token metrics#309440

Merged
DonJayamanne merged 3 commits intomainfrom
don/spontaneous-dinosaur
Apr 13, 2026
Merged

feat(copilotcli): enhance usage reporting in CopilotCLISession with detailed token metrics#309440
DonJayamanne merged 3 commits intomainfrom
don/spontaneous-dinosaur

Conversation

@DonJayamanne
Copy link
Copy Markdown
Contributor

Fixes #300491

Copilot AI review requested due to automatic review settings April 13, 2026 10:12
@DonJayamanne DonJayamanne self-assigned this Apr 13, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves token usage reporting for Copilot CLI chat sessions so the chat context-usage indicator can reflect more accurate prompt usage and show a breakdown of prompt token categories, addressing the reported “compaction doesn’t work” symptom.

Changes:

  • Adds reporting of promptTokenDetails (system instructions, tool definitions, messages) alongside prompt/completion token counts.
  • Listens for session.usage_info events to capture current token distribution and report it to the response stream.
  • Attempts to refresh usage after each request by querying _sdkSession.usage.getMetrics().
Show a summary per file
File Description
extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotcliSession.ts Adds usage reporting helpers, hooks session.usage_info, and builds prompt token breakdown for the context usage widget.

Copilot's findings

Comments suppressed due to low confidence (2)

extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotcliSession.ts:585

  • The session.usage_info handler assumes all token fields are numbers, but event.data appears to be untyped (similar to assistant.usage, which guards with typeof === 'number'). Please add type checks (at least for currentTokens) before assigning to lastUsageInfo to avoid propagating NaN/invalid values into the context usage widget.
			disposables.add(toDisposable(this._sdkSession.on('session.usage_info', (event) => {
				lastUsageInfo = {
					currentTokens: event.data.currentTokens,
					systemTokens: event.data.systemTokens,
					conversationTokens: event.data.conversationTokens,

extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotcliSession.ts:590

  • reportUsage(lastUsageInfo.currentTokens, 0) resets completionTokens to 0 whenever session.usage_info fires. The context usage widget computes used tokens as promptTokens + completionTokens, so this will under-report usage and can cause the indicator to jump backwards if this event arrives after assistant.usage. Consider preserving the latest known completion token count (or using metrics.lastCallOutputTokens) when reporting session.usage_info.
				reportUsage(lastUsageInfo.currentTokens, 0);
			})));
  • Files reviewed: 1/1 changed files
  • Comments generated: 1

@DonJayamanne DonJayamanne merged commit 93e2ccb into main Apr 13, 2026
26 checks passed
@DonJayamanne DonJayamanne deleted the don/spontaneous-dinosaur branch April 13, 2026 23:52
@vs-code-engineering vs-code-engineering bot added this to the 1.117.0 milestone Apr 13, 2026
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.

Compaction doesn't seem to work for Copilot CLI

3 participants