Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 10, 2026

Addresses feedback from #2779 to validate token counts from external API responses in the TokenUsageMetadata constructor.

Changes

  • Validation logic: Rejects non-finite (NaN, Infinity), negative, or unreasonably large (>10M) token counts
  • Error handling: Throws descriptive errors for invalid inputs to catch API response issues early
  • Documentation: Added rationale for 10M limit (sanity check allowing large context windows like Claude's 200k)
  • Test coverage: 12 unit tests covering valid and invalid input scenarios
  • Code style: Fixed log message formatting inconsistency in agentIntent.ts
export class TokenUsageMetadata {
	private static readonly MAX_TOKEN_COUNT = 10_000_000;

	constructor(
		readonly promptTokens: number,
		readonly outputTokens: number,
	) {
		if (!Number.isFinite(promptTokens) || !Number.isFinite(outputTokens)) {
			throw new Error('Token counts must be finite numbers.');
		}
		if (promptTokens < 0 || outputTokens < 0) {
			throw new Error('Token counts cannot be negative.');
		}
		if (promptTokens > MAX_TOKEN_COUNT || outputTokens > MAX_TOKEN_COUNT) {
			throw new Error('Token counts exceed maximum allowed value.');
		}
	}
}

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Jan 10, 2026
Copilot AI and others added 3 commits January 10, 2026 01:41
…nit tests

Co-authored-by: bhavyaus <25044782+bhavyaus@users.noreply.github.com>
Co-authored-by: bhavyaus <25044782+bhavyaus@users.noreply.github.com>
Co-authored-by: bhavyaus <25044782+bhavyaus@users.noreply.github.com>
Copilot AI changed the title [WIP] Update token usage tracking and summarization triggers Add input validation to TokenUsageMetadata constructor Jan 10, 2026
Copilot AI requested a review from bhavyaus January 10, 2026 01:44
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.

2 participants