@posthog/ai 8.9.1 → 8.9.3

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.
@@ -172,7 +172,7 @@ const truncate = (input, client) => {
172
172
  return `${truncatedStr}... [truncated]`;
173
173
  };
174
174
 
175
- var version = "8.9.1";
175
+ var version = "8.9.3";
176
176
 
177
177
  // Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway
178
178
  // emits its own $ai_generation, so each call would be captured (and, for billable
@@ -170,7 +170,7 @@ const truncate = (input, client) => {
170
170
  return `${truncatedStr}... [truncated]`;
171
171
  };
172
172
 
173
- var version = "8.9.1";
173
+ var version = "8.9.3";
174
174
 
175
175
  // Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway
176
176
  // emits its own $ai_generation, so each call would be captured (and, for billable
@@ -439,7 +439,7 @@ function sanitizeValues(obj) {
439
439
  return jsonSafe;
440
440
  }
441
441
 
442
- var version = "8.9.1";
442
+ var version = "8.9.3";
443
443
 
444
444
  const DEFAULT_MAX_DEPTH = 3;
445
445
  const MAX_STACK_LINES = 20;
@@ -596,15 +596,22 @@ const captureAiGeneration = async (client, options) => {
596
596
  };
597
597
  }
598
598
  httpStatus = httpStatus ?? 200;
599
- let costOverrideData = {};
599
+
600
+ // A configured price applies only to a count the provider reported, so a call with no
601
+ // reported usage sends no cost instead of asserting $0. $ai_total_cost_usd sums the sides
602
+ // that were priced, which makes it the cost of the known side alone when the other side
603
+ // went unreported: a lower bound on the true total, not an assertion of it.
604
+ const costOverrideData = {};
600
605
  if (options.costOverride) {
601
- const inputCostUSD = (options.costOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
602
- const outputCostUSD = (options.costOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
603
- costOverrideData = {
604
- $ai_input_cost_usd: inputCostUSD,
605
- $ai_output_cost_usd: outputCostUSD,
606
- $ai_total_cost_usd: inputCostUSD + outputCostUSD
607
- };
606
+ if (usage.inputTokens !== undefined) {
607
+ costOverrideData.$ai_input_cost_usd = (options.costOverride.inputCost ?? 0) * usage.inputTokens;
608
+ }
609
+ if (usage.outputTokens !== undefined) {
610
+ costOverrideData.$ai_output_cost_usd = (options.costOverride.outputCost ?? 0) * usage.outputTokens;
611
+ }
612
+ if (Object.keys(costOverrideData).length > 0) {
613
+ costOverrideData.$ai_total_cost_usd = (costOverrideData.$ai_input_cost_usd ?? 0) + (costOverrideData.$ai_output_cost_usd ?? 0);
614
+ }
608
615
  }
609
616
 
610
617
  // The caller's own token counts override the SDK-derived ones further down, via the
@@ -648,7 +655,9 @@ const captureAiGeneration = async (client, options) => {
648
655
  $ai_input: safeInput,
649
656
  $ai_output_choices: safeOutput,
650
657
  $ai_http_status: httpStatus,
651
- $ai_input_tokens: usage.inputTokens ?? 0,
658
+ ...(usage.inputTokens !== undefined ? {
659
+ $ai_input_tokens: usage.inputTokens
660
+ } : {}),
652
661
  ...(usage.outputTokens !== undefined ? {
653
662
  $ai_output_tokens: usage.outputTokens
654
663
  } : {}),
@@ -1166,13 +1175,10 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1166
1175
  provider: model.provider,
1167
1176
  input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1168
1177
  output: [],
1169
- latency: 0,
1178
+ latency: (Date.now() - startTime) / 1000,
1170
1179
  baseURL,
1171
1180
  modelParameters: getModelParams(mergedParams),
1172
- usage: {
1173
- inputTokens: 0,
1174
- outputTokens: 0
1175
- },
1181
+ usage: {},
1176
1182
  error: error,
1177
1183
  tools: availableTools
1178
1184
  });
@@ -1397,13 +1403,10 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1397
1403
  provider: provider,
1398
1404
  input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt, phClient),
1399
1405
  output: [],
1400
- latency: 0,
1406
+ latency: (Date.now() - startTime) / 1000,
1401
1407
  baseURL,
1402
1408
  modelParameters: getModelParams(mergedParams),
1403
- usage: {
1404
- inputTokens: 0,
1405
- outputTokens: 0
1406
- },
1409
+ usage: {},
1407
1410
  error: error,
1408
1411
  tools: availableTools
1409
1412
  });