Skip to content
This repository was archived by the owner on May 20, 2026. It is now read-only.

Add Context Window Widget support for Claude Agent - #3816

Merged
TylerLeonhardt merged 7 commits into
mainfrom
copilot/add-context-window-widget-claude
Feb 19, 2026
Merged

TylerLeonhardt merged 7 commits into
mainfrom
copilot/add-context-window-widget-claude

Conversation

Copilot AI commented Feb 18, 2026 •

Copy link
Copy Markdown
Contributor

Claude sessions receive usage data from CAPI but weren't reporting it to the UI. This wires up the existing usage information to stream.usage().

Implementation

Uses session state service as intermediary to bridge language model server and content provider:

Session State Service

  • Added UsageHandler type: (usage: vscode.ChatResultUsage) => void
  • Store/retrieve handler per session ID

Content Provider

  • Set handler before request: stream.usage(usage)
  • Clear handler after completion to avoid leaks

Language Model Server

  • Extract usage from AnthropicMessagesProcessor completion
  • Call handler when sessionId present
  • Include cached token percentage in promptTokenDetails

Flow

AnthropicMessagesProcessor (usage extracted)
  ↓
ClaudeStreamingPassThroughEndpoint (receives completion)
  ↓
UsageHandler (session state)
  ↓
stream.usage() (Context Window Widget)

Testing

  • Added 6 unit tests for usage handler in claudeSessionStateService.spec.ts
  • All tests passing (26 total)
Original prompt

This section details on the original issue you should resolve

<issue_title>Context Window Widget should be shown for Claude Agent</issue_title>
<issue_description>Testing microsoft/vscode#290398

Claude Chat Sessions are going through our Chat Endpoint infra so going to CAPI... no reason we can't have this for Claude as well.</issue_description>

Comments on the Issue (you are Copilot in this section)

TylerLeonhardt Bhavya U (@bhavyaus) was telling me that the usage information is included in the response. The chat endpoint that is used in claudeLanguageModelServer.

I think we need to hook up the state service used there as an intermediary between:

  • setting the usage handler (which can be a function set by the request handler in the content provider that calls stream.usage)
  • somewhere in the Claude language model server could then call that handler passing in the usage

Copilot CLI won't have the best example since it's quite different but it'll help a bit it'll have usage of the usage function </comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 18, 2026 06:01
- Add UsageHandler type and methods to IClaudeSessionStateService
- Set usage handler in claudeChatSessionContentProvider request handler
- Wire up usage reporting in ClaudeStreamingPassThroughEndpoint
- Extract usage from AnthropicMessagesProcessor completion and call handler

Co-authored-by: TylerLeonhardt <[email protected]>
- Add tests for getUsageHandlerForSession
- Add tests for setUsageHandlerForSession
- Verify handler preservation across state changes
- Verify handler can be called correctly

Co-authored-by: TylerLeonhardt <[email protected]>
Copilot AI changed the title [WIP] Add context window widget for Claude agent Add Context Window Widget support for Claude Agent Feb 18, 2026
Copilot AI requested a review from TylerLeonhardt February 18, 2026 06:05
@TylerLeonhardt
TylerLeonhardt marked this pull request as ready for review February 18, 2026 20:55
Copilot AI review requested due to automatic review settings February 18, 2026 20:55
@vs-code-engineering vs-code-engineering Bot added this to the February 2026 milestone Feb 18, 2026

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 enables the Context Window Widget for Claude Agent sessions by wiring up usage data that was already being received from CAPI but not reported to the UI. The implementation uses the session state service as an intermediary to bridge the language model server and the content provider.

Changes:

  • Added usage handler mechanism to report token usage from Claude language model server to the UI's context window widget
  • Extended session state service to store/retrieve usage handlers per session
  • Added comprehensive unit tests for the new usage handler functionality

Reviewed changes

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

File Description
src/extension/chatSessions/vscode-node/claudeChatSessionContentProvider.ts Sets and clears usage handler before/after request to report token usage via stream.usage()
src/extension/agents/claude/node/claudeSessionStateService.ts Adds UsageHandler type and getter/setter methods to store handlers per session
src/extension/agents/claude/node/claudeLanguageModelServer.ts Extracts usage from AnthropicMessagesProcessor completion and calls handler when sessionId present
src/extension/agents/claude/node/test/claudeSessionStateService.spec.ts Adds 6 unit tests covering usage handler storage, retrieval, clearing, and event behavior

@TylerLeonhardt
TylerLeonhardt added this pull request to the merge queue Feb 18, 2026
Merged via the queue into main with commit 0e32303 Feb 19, 2026
19 checks passed
@TylerLeonhardt
TylerLeonhardt deleted the copilot/add-context-window-widget-claude branch February 19, 2026 00:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context Window Widget should be shown for Claude Agent

4 participants