Skip to content

Account for Copilot reasoning tokens in usage details - #553

Merged
Quim Muntal (qmuntal) merged 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix-copilot-reasoning-token-count
Jul 20, 2026
Merged

Account for Copilot reasoning tokens in usage details#553
Quim Muntal (qmuntal) merged 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix-copilot-reasoning-token-count

Conversation

@PratikDhanave

Copy link
Copy Markdown
Contributor

When converting a Copilot assistant.usage event, assistantUsageUpdate mapped input, output, total, and cache-read tokens but never read AssistantUsageData.ReasoningTokens:

details := message.UsageDetails{
    InputTokenCount:       inputTokens,
    OutputTokenCount:      outputTokens,
    TotalTokenCount:       inputTokens + outputTokens,
    CachedInputTokenCount: int64Value(data.CacheReadTokens),
    AdditionalCounts:      additionalUsageCounts(data),
}

The SDK documents ReasoningTokens as "Number of output tokens used for reasoning (e.g., chain-of-thought)." For a reasoning model those tokens were silently dropped: the framework’s dedicated UsageDetails.ReasoningTokenCount field (populated by the OpenAI and Gemini providers, summed by UsageDetails.Add) stayed 0, so callers had no reasoning-token breakdown from Copilot.

Fix

    CachedInputTokenCount: int64Value(data.CacheReadTokens),
    ReasoningTokenCount:   int64Value(data.ReasoningTokens),

ReasoningTokens is a subset of the output tokens, so TotalTokenCount (input + output) already accounts for it and is left unchanged — only the missing breakdown field is added, consistent with the OpenAI providers.

Test

TestConvertToAgentResponseUpdate_UsageEvent_SurfacesReasoningTokens drives an assistant.usage event with reasoningTokens: 8 and asserts ReasoningTokenCount == 8. It fails on the old code (0) and passes with the fix.

assistantUsageUpdate mapped input, output, total, and cache-read tokens
but dropped AssistantUsageData.ReasoningTokens, so the framework's
dedicated ReasoningTokenCount field stayed zero for reasoning models.
ReasoningTokens is a subset of the output tokens, so TotalTokenCount is
unaffected; only the breakdown field was missing.

Map ReasoningTokens to UsageDetails.ReasoningTokenCount, matching the
OpenAI and Gemini providers.
@PratikDhanave
PratikDhanave (PratikDhanave) requested a review from a team as a code owner July 19, 2026 11:08
Copilot AI review requested due to automatic review settings July 19, 2026 11:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the Copilot provider’s assistant.usage event conversion to preserve the reasoning-token breakdown by mapping AssistantUsageData.ReasoningTokens into the framework’s UsageDetails.ReasoningTokenCount, matching the behavior of other providers (e.g., OpenAI).

Changes:

  • Populate UsageDetails.ReasoningTokenCount from Copilot reasoningTokens when converting assistant.usage events.
  • Add a unit test ensuring reasoning tokens are surfaced in the resulting UsageContent details.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
provider/copilotprovider/copilot.go Maps Copilot ReasoningTokens into UsageDetails.ReasoningTokenCount during usage-event conversion.
provider/copilotprovider/copilot_test.go Adds a regression test asserting reasoningTokens is preserved in UsageDetails.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@qmuntal
Quim Muntal (qmuntal) added this pull request to the merge queue Jul 20, 2026
Merged via the queue into microsoft:main with commit e5c3ec3 Jul 20, 2026
19 checks passed
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.

3 participants