Skip to content

Avoid reporting subagent token usage to the context window widget#3515

Merged
lramos15 merged 3 commits intomicrosoft:mainfrom
bharatvansh:fix/subagent-usage-context-widget
Mar 6, 2026
Merged

Avoid reporting subagent token usage to the context window widget#3515
lramos15 merged 3 commits intomicrosoft:mainfrom
bharatvansh:fix/subagent-usage-context-widget

Conversation

@bharatvansh
Copy link
Copy Markdown
Contributor

Summary

When a request is executed in a subagent context (i.e. request.subAgentInvocationId is set), we should not report token usage to the context window widget in the chat input. The widget should reflect only the parent request’s usage; otherwise subagent/tool traffic can inflate what the user sees.

Changes

  • Gate stream.usage(...) reporting in ToolCallingLoop so it only runs for non-subagent requests.
  • Add a unit test verifying:
    • regular requests report usage
    • subagent requests do not report usage

Why

Subagents may run additional model calls and tool loops. Reporting their usage to the parent request’s context widget misrepresents the request’s actual context usage and can confuse users.

Testing

  • npm run test:unit -- src/extension/intents/test/node/toolCallingLoopUsage.spec.ts

Notes

This change only affects usage reporting for the context window widget and does not change server requests or token accounting elsewhere.

Avoid reporting token usage from subagent requests to the context window widget. Add unit tests covering subagent vs regular usage reporting.
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 prevents subagent token usage from being reported to the context window widget in the chat input, ensuring users only see token usage from the parent request.

Changes:

  • Added shouldReportUsageToContextWidget() method that returns false when subAgentInvocationId is set
  • Gated the stream.usage(...) call to only report usage for non-subagent requests
  • Added comprehensive unit tests verifying usage reporting behavior for both regular and subagent requests

Reviewed changes

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

File Description
src/extension/intents/node/toolCallingLoop.ts Added private helper method to check if usage should be reported and updated the usage reporting condition to exclude subagent requests
src/extension/intents/test/node/toolCallingLoopUsage.spec.ts New test file with comprehensive tests for usage reporting behavior in regular vs subagent contexts

@jruales jruales assigned lramos15 and unassigned jruales Feb 26, 2026
lramos15
lramos15 previously approved these changes Feb 27, 2026
@lramos15 lramos15 enabled auto-merge February 27, 2026 13:11
@vs-code-engineering vs-code-engineering bot added this to the February 2026 milestone Feb 27, 2026
bpasero
bpasero previously approved these changes Feb 27, 2026
@rzhao271 rzhao271 modified the milestones: February 2026, March 2026 Feb 27, 2026
@lramos15
Copy link
Copy Markdown
Member

lramos15 commented Mar 2, 2026

@bharatvansh Can you updaet and resync with main to unsitck the test? I don't know why GitHub isn't showing me the button to do just that

auto-merge was automatically disabled March 2, 2026 16:55

Head branch was pushed to by a user without write access

@bharatvansh bharatvansh dismissed stale reviews from lramos15 and bpasero via 8fc245c March 2, 2026 16:55
@bharatvansh
Copy link
Copy Markdown
Contributor Author

Hi @lramos15 , I've resynced with main and fixed the toolCallingLoopUsage test failures. The CI are green now. You can take another look and re-enable auto-merge. Thanks!

@bharatvansh bharatvansh requested a review from bpasero March 2, 2026 18:04
@bpasero bpasero removed their request for review March 2, 2026 20:07
@joaomorenoalt joaomorenoalt removed this from the March 2026 milestone Mar 3, 2026
@bharatvansh
Copy link
Copy Markdown
Contributor Author

@lramos15 Happy to address any remaining feedback. is there anything blocking this from merging?

@vs-code-engineering vs-code-engineering bot added this to the 1.111.0 milestone Mar 6, 2026
@lramos15 lramos15 added this pull request to the merge queue Mar 6, 2026
Merged via the queue into microsoft:main with commit 2128b11 Mar 6, 2026
13 of 16 checks passed
@bharatvansh bharatvansh deleted the fix/subagent-usage-context-widget branch March 6, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants