Open
Conversation
BYOK models registered via VS Code LM API had their usage data discarded, causing the context window usage circle to always show empty. This fix uses LanguageModelDataPart with a custom 'usage' mime type to bridge usage data from the BYOK provider (CopilotLanguageModelWrapper) through the VS Code LM API stream to ExtensionContributedChatEndpoint.
Prevents malformed or invalid usage data (e.g. string fields instead of numbers) from being accepted. Adds a test for invalid field types.
Add clarifying comment for multiple Usage DataPart semantics. Add tests: Usage before text arrival order, finishedCb provided scenario.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes BYOK (Bring Your Own Key) token usage reporting in the chat panel by bridging real APIUsage through the VS Code Language Model API stream using a custom LanguageModelDataPart, allowing the context window widget to display accurate usage.
Changes:
- Add a new custom data-part mime type (
CustomDataPartMimeTypes.Usage) for usage payloads. - Update
CopilotLanguageModelWrapperto return fetchAPIUsageand emit it as aLanguageModelDataPart('usage'). - Update
ExtensionContributedChatEndpointto extract/validate usage from the stream and include it in theChatResponse, plus add unit tests covering parsing/fallback behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/platform/endpoint/common/endpointTypes.ts | Adds CustomDataPartMimeTypes.Usage constant to identify usage DataParts. |
| src/extension/conversation/vscode-node/languageModelAccess.ts | Returns APIUsage from the wrapper request path and emits it via LanguageModelDataPart on the VS Code LM stream. |
| src/platform/endpoint/vscode-node/extChatEndpoint.ts | Captures usage DataParts from the contributed LM stream and uses them in the returned ChatResponse (with fallback). |
| src/platform/endpoint/vscode-node/test/extChatEndpoint.spec.ts | Adds Vitest coverage to ensure usage extraction/validation and fallback behavior works across stream orderings. |
Comments suppressed due to low confidence (1)
src/extension/conversation/vscode-node/languageModelAccess.ts:32
APIUsageis only used as a type here. Please switch this to a type-only import (e.g.,import type { APIUsage } ...) to match the surrounding import style and avoid accidentally introducing a runtime dependency if TS compiler settings change.
import { ILogService } from '../../../platform/log/common/logService';
import { isAnthropicToolSearchEnabled } from '../../../platform/networking/common/anthropic';
import { FinishedCallback, OpenAiFunctionTool, OptionalChatRequestParams } from '../../../platform/networking/common/fetch';
import { APIUsage } from '../../../platform/networking/common/openai';
import { IChatEndpoint, IEndpoint } from '../../../platform/networking/common/networking';
import { IOTelService, type OTelModelOptions } from '../../../platform/otel/common/otelService';
import { retrieveCapturingTokenByCorrelation, runWithCapturingToken } from '../../../platform/requestLogger/node/requestLogger';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: Pass real usage data from BYOK providers to context window widget
Problem
BYOK (Bring Your Own Key) models registered via the VS Code Language Model API always show an empty context window usage circle in the chat panel. This happens because the usage data from BYOK providers is silently discarded during the stream bridging process.
Root Cause
The data flow for BYOK models is:
Specifically:
_provideLanguageModelResponse()returnedvoid, discarding theAPIUsagefrom the fetcher resultLanguageModelResponsePart2has no native usage part typeExtensionContributedChatEndpointhardcodedusage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }because it had no way to receive actual usageSolution
Use the existing
LanguageModelDataPartpattern (already used forStatefulMarker,ContextManagement,ThinkingData, andPhaseData) to bridge usage data through the VS Code LM API stream.Data flow after fix:
Changes
src/platform/endpoint/common/endpointTypes.tsUsage = 'usage'toCustomDataPartMimeTypesnamespacesrc/extension/conversation/vscode-node/languageModelAccess.ts_provideLanguageModelResponse()return type fromPromise<void>toPromise<APIUsage | undefined>return result.usageat the end of the success pathprovideLanguageModelResponse(), captured the returned usage and reported it viaLanguageModelDataPartwith the customusagemime typesrc/platform/endpoint/vscode-node/extChatEndpoint.tsisApiUsageimport for runtime validationreportedUsagevariable to capture usage from the streamCustomDataPartMimeTypes.UsageDataPart with:isApiUsage()runtime validation (rejects invalid field types){0,0,0}toreportedUsage ?? {0,0,0}fallbacksrc/platform/endpoint/vscode-node/test/extChatEndpoint.spec.ts(NEW)ExtensionContributedChatEndpointfinishedCbis providedStats
Testing