feat(telemetry): Add context breakdown to API response event (#19699)

This commit is contained in:
Sandy Tao
2026-02-24 15:26:28 -08:00
committed by GitHub
parent 70b650122f
commit 3ff5cfaaf6
8 changed files with 479 additions and 27 deletions
@@ -846,6 +846,40 @@ export class ClearcutLogger {
EventMetadataKey.GEMINI_CLI_API_RESPONSE_TOOL_TOKEN_COUNT,
value: JSON.stringify(event.usage.tool_token_count),
},
// Context breakdown fields are only populated on turn-ending responses
// (when the user gets back control), not during intermediate tool-use
// loops. Values still grow across turns as conversation history
// accumulates, so downstream consumers should use the last event per
// session (MAX) rather than summing across events.
{
gemini_cli_key:
EventMetadataKey.GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_SYSTEM_INSTRUCTIONS,
value: JSON.stringify(
event.usage.context_breakdown?.system_instructions ?? 0,
),
},
{
gemini_cli_key:
EventMetadataKey.GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_TOOL_DEFINITIONS,
value: JSON.stringify(
event.usage.context_breakdown?.tool_definitions ?? 0,
),
},
{
gemini_cli_key:
EventMetadataKey.GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_HISTORY,
value: JSON.stringify(event.usage.context_breakdown?.history ?? 0),
},
{
gemini_cli_key:
EventMetadataKey.GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_TOOL_CALLS,
value: JSON.stringify(event.usage.context_breakdown?.tool_calls ?? {}),
},
{
gemini_cli_key:
EventMetadataKey.GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_MCP_SERVERS,
value: JSON.stringify(event.usage.context_breakdown?.mcp_servers ?? 0),
},
];
this.enqueueLogEvent(this.createLogEvent(EventNames.API_RESPONSE, data));
@@ -7,7 +7,7 @@
// Defines valid event metadata keys for Clearcut logging.
export enum EventMetadataKey {
// Deleted enums: 24
// Next ID: 167
// Next ID: 172
GEMINI_CLI_KEY_UNKNOWN = 0,
@@ -137,6 +137,21 @@ export enum EventMetadataKey {
// Logs the tool use token count of the API call.
GEMINI_CLI_API_RESPONSE_TOOL_TOKEN_COUNT = 29,
// Logs the token count for system instructions.
GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_SYSTEM_INSTRUCTIONS = 167,
// Logs the token count for tool definitions.
GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_TOOL_DEFINITIONS = 168,
// Logs the token count for conversation history.
GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_HISTORY = 169,
// Logs the token count for tool calls (JSON map of tool name to tokens).
GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_TOOL_CALLS = 170,
// Logs the token count from MCP servers (tool definitions + tool inputs/outputs).
GEMINI_CLI_API_RESPONSE_CONTEXT_BREAKDOWN_MCP_SERVERS = 171,
// ==========================================================================
// GenAI API Error Event Keys
// ===========================================================================
+9
View File
@@ -569,6 +569,14 @@ export interface GenAIResponseDetails {
candidates?: Candidate[];
}
export interface ContextBreakdown {
system_instructions: number;
tool_definitions: number;
history: number;
tool_calls: Record<string, number>;
mcp_servers: number;
}
export interface GenAIUsageDetails {
input_token_count: number;
output_token_count: number;
@@ -576,6 +584,7 @@ export interface GenAIUsageDetails {
thoughts_token_count: number;
tool_token_count: number;
total_token_count: number;
context_breakdown?: ContextBreakdown;
}
export const EVENT_API_RESPONSE = 'gemini_cli.api_response';