diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 2457c320..1c166b5b 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -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({ diff --git a/src/services/posthog/telemetry/TelemetryService.ts b/src/services/posthog/telemetry/TelemetryService.ts index 089cafdc..549bff58 100644 --- a/src/services/posthog/telemetry/TelemetryService.ts +++ b/src/services/posthog/telemetry/TelemetryService.ts @@ -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(