Skip to content

Commit b32098c

Browse files
authored
[Agent Builder] token usage telemetry (#243304)
1 parent 28b7e62 commit b32098c

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

x-pack/platform/plugins/private/telemetry_collection_xpack/schema/xpack_platform.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@
395395
}
396396
}
397397
}
398+
},
399+
"tokens_used": {
400+
"type": "long",
401+
"_meta": {
402+
"description": "Total tokens used across all conversations (input + output)"
403+
}
404+
},
405+
"average_tokens_per_conversation": {
406+
"type": "float",
407+
"_meta": {
408+
"description": "Average tokens per conversation"
409+
}
398410
}
399411
}
400412
},

x-pack/platform/plugins/shared/onechat/server/telemetry/query_utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ export class QueryUtils {
193193
size: 100,
194194
},
195195
},
196+
total_tokens: {
197+
sum: {
198+
script: {
199+
source: `
200+
def source = params._source;
201+
def roundsArray = source.conversation_rounds != null ? source.conversation_rounds : source.rounds;
202+
def totalTokens = 0;
203+
if (roundsArray != null) {
204+
for (def round : roundsArray) {
205+
if (round.model_usage != null) {
206+
def inputTokens = round.model_usage.input_tokens != null ? round.model_usage.input_tokens : 0;
207+
def outputTokens = round.model_usage.output_tokens != null ? round.model_usage.output_tokens : 0;
208+
totalTokens += inputTokens + outputTokens;
209+
}
210+
}
211+
}
212+
return totalTokens;
213+
`,
214+
lang: 'painless',
215+
},
216+
},
217+
},
196218
},
197219
});
198220

@@ -225,11 +247,18 @@ export class QueryUtils {
225247
const avgRoundsPerConversation =
226248
totalConversations > 0 ? totalRounds / totalConversations : 0;
227249

250+
const totalTokensAgg = response.aggregations?.total_tokens as any;
251+
const tokensUsed = totalTokensAgg?.value || 0;
252+
const averageTokensPerConversation =
253+
totalConversations > 0 ? tokensUsed / totalConversations : 0;
254+
228255
return {
229256
total: totalConversations,
230257
total_rounds: totalRounds,
231258
avg_rounds_per_conversation: Math.round(avgRoundsPerConversation * 100) / 100,
232259
rounds_distribution: roundsDistribution,
260+
tokens_used: Math.round(tokensUsed),
261+
average_tokens_per_conversation: Math.round(averageTokensPerConversation * 100) / 100,
233262
};
234263
} catch (error) {
235264
if (!isIndexNotFoundError(error)) {
@@ -240,6 +269,8 @@ export class QueryUtils {
240269
total_rounds: 0,
241270
avg_rounds_per_conversation: 0,
242271
rounds_distribution: [],
272+
tokens_used: 0,
273+
average_tokens_per_conversation: 0,
243274
};
244275
}
245276
}

x-pack/platform/plugins/shared/onechat/server/telemetry/telemetry_collector.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export interface OnechatTelemetry {
3535
bucket: string;
3636
count: number;
3737
}>;
38+
tokens_used: number;
39+
average_tokens_per_conversation: number;
3840
};
3941
query_to_result_time: {
4042
p50: number;
@@ -160,6 +162,18 @@ export function registerTelemetryCollector(
160162
},
161163
},
162164
},
165+
tokens_used: {
166+
type: 'long',
167+
_meta: {
168+
description: 'Total tokens used across all conversations (input + output)',
169+
},
170+
},
171+
average_tokens_per_conversation: {
172+
type: 'float',
173+
_meta: {
174+
description: 'Average tokens per conversation',
175+
},
176+
},
163177
},
164178
query_to_result_time: {
165179
p50: {
@@ -433,6 +447,8 @@ export function registerTelemetryCollector(
433447
total_rounds: 0,
434448
avg_rounds_per_conversation: 0,
435449
rounds_distribution: [],
450+
tokens_used: 0,
451+
average_tokens_per_conversation: 0,
436452
},
437453
query_to_result_time: {
438454
p50: 0,

0 commit comments

Comments
 (0)