@posthog/ai 8.9.0 → 8.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -437,7 +437,7 @@ function sanitizeValues(obj) {
437
437
  return jsonSafe;
438
438
  }
439
439
 
440
- var version = "8.9.0";
440
+ var version = "8.9.2";
441
441
 
442
442
  const DEFAULT_MAX_DEPTH = 3;
443
443
  const MAX_STACK_LINES = 20;
@@ -594,15 +594,22 @@ const captureAiGeneration = async (client, options) => {
594
594
  };
595
595
  }
596
596
  httpStatus = httpStatus ?? 200;
597
- let costOverrideData = {};
597
+
598
+ // A configured price applies only to a count the provider reported, so a call with no
599
+ // reported usage sends no cost instead of asserting $0. $ai_total_cost_usd sums the sides
600
+ // that were priced, which makes it the cost of the known side alone when the other side
601
+ // went unreported: a lower bound on the true total, not an assertion of it.
602
+ const costOverrideData = {};
598
603
  if (options.costOverride) {
599
- const inputCostUSD = (options.costOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
600
- const outputCostUSD = (options.costOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
601
- costOverrideData = {
602
- $ai_input_cost_usd: inputCostUSD,
603
- $ai_output_cost_usd: outputCostUSD,
604
- $ai_total_cost_usd: inputCostUSD + outputCostUSD
605
- };
604
+ if (usage.inputTokens !== undefined) {
605
+ costOverrideData.$ai_input_cost_usd = (options.costOverride.inputCost ?? 0) * usage.inputTokens;
606
+ }
607
+ if (usage.outputTokens !== undefined) {
608
+ costOverrideData.$ai_output_cost_usd = (options.costOverride.outputCost ?? 0) * usage.outputTokens;
609
+ }
610
+ if (Object.keys(costOverrideData).length > 0) {
611
+ costOverrideData.$ai_total_cost_usd = (costOverrideData.$ai_input_cost_usd ?? 0) + (costOverrideData.$ai_output_cost_usd ?? 0);
612
+ }
606
613
  }
607
614
 
608
615
  // The caller's own token counts override the SDK-derived ones further down, via the
@@ -646,7 +653,9 @@ const captureAiGeneration = async (client, options) => {
646
653
  $ai_input: safeInput,
647
654
  $ai_output_choices: safeOutput,
648
655
  $ai_http_status: httpStatus,
649
- $ai_input_tokens: usage.inputTokens ?? 0,
656
+ ...(usage.inputTokens !== undefined ? {
657
+ $ai_input_tokens: usage.inputTokens
658
+ } : {}),
650
659
  ...(usage.outputTokens !== undefined ? {
651
660
  $ai_output_tokens: usage.outputTokens
652
661
  } : {}),
@@ -1164,13 +1173,10 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1164
1173
  provider: model.provider,
1165
1174
  input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1166
1175
  output: [],
1167
- latency: 0,
1176
+ latency: (Date.now() - startTime) / 1000,
1168
1177
  baseURL,
1169
1178
  modelParameters: getModelParams(mergedParams),
1170
- usage: {
1171
- inputTokens: 0,
1172
- outputTokens: 0
1173
- },
1179
+ usage: {},
1174
1180
  error: error,
1175
1181
  tools: availableTools
1176
1182
  });
@@ -1395,13 +1401,10 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1395
1401
  provider: provider,
1396
1402
  input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1397
1403
  output: [],
1398
- latency: 0,
1404
+ latency: (Date.now() - startTime) / 1000,
1399
1405
  baseURL,
1400
1406
  modelParameters: getModelParams(mergedParams),
1401
- usage: {
1402
- inputTokens: 0,
1403
- outputTokens: 0
1404
- },
1407
+ usage: {},
1405
1408
  error: error,
1406
1409
  tools: availableTools
1407
1410
  });