Capture token usage for conversation turns in telementry (#4703)
Adds capture of token usage data for conversation turns in the telemetry service. This includes tokensIn, tokensOut, cacheWriteTokens, cacheReadTokens, and totalCost. The changes involve: - Modifying the `captureConversationTurnEvent` method in `TelemetryService.ts` to accept and include token usage data in the captured event properties. - Updating the `Task` class in `src/core/task/index.ts` to pass token usage information when capturing conversation turn events. This ensures that token usage is tracked for both regular and cached responses. - Refactor capture event to use object destructuring for easier readability Co-authored-by: Beatrix Woo <beatrix@cline.bot>
This commit is contained in:
1 parent
6d2cf55fc5
commit
0262e13ac4
2 files changed
+32
-11
No files matched your search
@@ -2124,6 +2124,13 @@ export class Task {
|
||||
this.api.getModel().id,
|
||||
"assistant",
|
||||
true,
|
||||
{
|
||||
tokensIn: inputTokens,
|
||||
tokensOut: outputTokens,
|
||||
cacheWriteTokens,
|
||||
cacheReadTokens,
|
||||
totalCost,
|
||||
},
|
||||
)
|
||||
|
||||
// signals to provider that it can retrieve the saved messages from disk, as abortTask can not be awaited on in nature
|
||||
@@ -2297,6 +2304,13 @@ export class Task {
|
||||
this.api.getModel().id,
|
||||
"assistant",
|
||||
true,
|
||||
{
|
||||
tokensIn: inputTokens,
|
||||
tokensOut: outputTokens,
|
||||
cacheWriteTokens,
|
||||
cacheReadTokens,
|
||||
totalCost,
|
||||
},
|
||||
)
|
||||
|
||||
await this.messageStateHandler.addToApiConversationHistory({
|
||||
|
||||
@@ -202,26 +202,23 @@ class TelemetryService {
|
||||
|
||||
const propertiesWithVersion = this.addProperties(event.properties)
|
||||
|
||||
const capturedEvent = {
|
||||
event: event.event,
|
||||
properties: propertiesWithVersion,
|
||||
}
|
||||
|
||||
if (collect && taskId) {
|
||||
const existingTask = this.collectedTasks.find((task) => task.taskId === taskId)
|
||||
if (existingTask) {
|
||||
existingTask.collection.push({
|
||||
event: event.event,
|
||||
properties: propertiesWithVersion,
|
||||
})
|
||||
existingTask.collection.push(capturedEvent)
|
||||
} else {
|
||||
this.collectedTasks.push({
|
||||
taskId,
|
||||
collection: [
|
||||
{
|
||||
event: event.event,
|
||||
properties: propertiesWithVersion,
|
||||
},
|
||||
],
|
||||
collection: [capturedEvent],
|
||||
})
|
||||
}
|
||||
} else {
|
||||
this.client.capture({ distinctId: this.distinctId, event: event.event, properties: propertiesWithVersion })
|
||||
this.client.capture({ ...capturedEvent, distinctId: this.distinctId })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,6 +285,8 @@ class TelemetryService {
|
||||
* @param provider The API provider (e.g., OpenAI, Anthropic)
|
||||
* @param model The specific model used (e.g., GPT-4, Claude)
|
||||
* @param source The source of the message ("user" | "model"). Used to track message patterns and identify when users need to correct the model's responses.
|
||||
* @param collect If true, collect event instead of sending
|
||||
* @param tokenUsage Optional token usage data
|
||||
*/
|
||||
public captureConversationTurnEvent(
|
||||
taskId: string,
|
||||
@@ -295,6 +294,13 @@ class TelemetryService {
|
||||
model: string = "unknown",
|
||||
source: "user" | "assistant",
|
||||
collect: boolean = false,
|
||||
tokenUsage: {
|
||||
tokensIn?: number
|
||||
tokensOut?: number
|
||||
cacheWriteTokens?: number
|
||||
cacheReadTokens?: number
|
||||
totalCost?: number
|
||||
} = {},
|
||||
) {
|
||||
// Ensure required parameters are provided
|
||||
if (!taskId || !provider || !model || !source) {
|
||||
@@ -308,6 +314,7 @@ class TelemetryService {
|
||||
model,
|
||||
source,
|
||||
timestamp: new Date().toISOString(), // Add timestamp for message sequencing
|
||||
...tokenUsage,
|
||||
}
|
||||
|
||||
this.capture(
|
||||
|
||||
Reference in new issue
Block a user