@posthog/ai 8.9.1 → 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.
@@ -338,7 +338,7 @@ function addDefaults(params) {
338
338
  };
339
339
  }
340
340
 
341
- var version = "8.9.1";
341
+ var version = "8.9.2";
342
342
 
343
343
  const DEFAULT_MAX_DEPTH = 3;
344
344
  const MAX_STACK_LINES = 20;
@@ -495,15 +495,22 @@ const captureAiGeneration = async (client, options) => {
495
495
  };
496
496
  }
497
497
  httpStatus = httpStatus ?? 200;
498
- let costOverrideData = {};
498
+
499
+ // A configured price applies only to a count the provider reported, so a call with no
500
+ // reported usage sends no cost instead of asserting $0. $ai_total_cost_usd sums the sides
501
+ // that were priced, which makes it the cost of the known side alone when the other side
502
+ // went unreported: a lower bound on the true total, not an assertion of it.
503
+ const costOverrideData = {};
499
504
  if (options.costOverride) {
500
- const inputCostUSD = (options.costOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
501
- const outputCostUSD = (options.costOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
502
- costOverrideData = {
503
- $ai_input_cost_usd: inputCostUSD,
504
- $ai_output_cost_usd: outputCostUSD,
505
- $ai_total_cost_usd: inputCostUSD + outputCostUSD
506
- };
505
+ if (usage.inputTokens !== undefined) {
506
+ costOverrideData.$ai_input_cost_usd = (options.costOverride.inputCost ?? 0) * usage.inputTokens;
507
+ }
508
+ if (usage.outputTokens !== undefined) {
509
+ costOverrideData.$ai_output_cost_usd = (options.costOverride.outputCost ?? 0) * usage.outputTokens;
510
+ }
511
+ if (Object.keys(costOverrideData).length > 0) {
512
+ costOverrideData.$ai_total_cost_usd = (costOverrideData.$ai_input_cost_usd ?? 0) + (costOverrideData.$ai_output_cost_usd ?? 0);
513
+ }
507
514
  }
508
515
 
509
516
  // The caller's own token counts override the SDK-derived ones further down, via the
@@ -547,7 +554,9 @@ const captureAiGeneration = async (client, options) => {
547
554
  $ai_input: safeInput,
548
555
  $ai_output_choices: safeOutput,
549
556
  $ai_http_status: httpStatus,
550
- $ai_input_tokens: usage.inputTokens ?? 0,
557
+ ...(usage.inputTokens !== undefined ? {
558
+ $ai_input_tokens: usage.inputTokens
559
+ } : {}),
551
560
  ...(usage.outputTokens !== undefined ? {
552
561
  $ai_output_tokens: usage.outputTokens
553
562
  } : {}),
@@ -927,10 +936,6 @@ class WrappedMessages extends AnthropicOriginal__default.default.Messages {
927
936
  let firstTokenTime;
928
937
  let stopReason;
929
938
  const usage = {
930
- inputTokens: 0,
931
- outputTokens: 0,
932
- cacheCreationInputTokens: 0,
933
- cacheReadInputTokens: 0,
934
939
  webSearchCount: 0
935
940
  };
936
941
  let rawUsage = {};
@@ -1074,19 +1079,21 @@ class WrappedMessages extends AnthropicOriginal__default.default.Messages {
1074
1079
  tools: availableTools
1075
1080
  });
1076
1081
  } catch (error) {
1082
+ // The final usage delta may never arrive; whatever raw usage did
1083
+ // still belongs on the event.
1084
+ if (Object.keys(rawUsage).length > 0) {
1085
+ usage.rawUsage = rawUsage;
1086
+ }
1077
1087
  await captureAiGeneration(this.phClient, {
1078
1088
  ...posthogParams,
1079
1089
  model: anthropicParams.model,
1080
1090
  provider: 'anthropic',
1081
1091
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams), this.phClient),
1082
1092
  output: [],
1083
- latency: 0,
1093
+ latency: (Date.now() - startTime) / 1000,
1084
1094
  baseURL: this.baseURL,
1085
1095
  modelParameters: getModelParams(body),
1086
- usage: {
1087
- inputTokens: 0,
1088
- outputTokens: 0
1089
- },
1096
+ usage,
1090
1097
  error: error
1091
1098
  });
1092
1099
  throw error;
@@ -1141,14 +1148,11 @@ class WrappedMessages extends AnthropicOriginal__default.default.Messages {
1141
1148
  provider: 'anthropic',
1142
1149
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams), this.phClient),
1143
1150
  output: [],
1144
- latency: 0,
1151
+ latency: (Date.now() - startTime) / 1000,
1145
1152
  baseURL: this.baseURL,
1146
1153
  modelParameters: getModelParams(body),
1147
1154
  httpStatus: error?.status ? error.status : 500,
1148
- usage: {
1149
- inputTokens: 0,
1150
- outputTokens: 0
1151
- },
1155
+ usage: {},
1152
1156
  error: error
1153
1157
  });
1154
1158
  throw error;