@posthog/ai 7.3.0 → 7.3.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.
@@ -3,7 +3,8 @@
3
3
  require('buffer');
4
4
  var uuid = require('uuid');
5
5
 
6
- function _interopNamespaceDefault(e) {
6
+ function _interopNamespace(e) {
7
+ if (e && e.__esModule) return e;
7
8
  var n = Object.create(null);
8
9
  if (e) {
9
10
  Object.keys(e).forEach(function (k) {
@@ -20,9 +21,9 @@ function _interopNamespaceDefault(e) {
20
21
  return Object.freeze(n);
21
22
  }
22
23
 
23
- var uuid__namespace = /*#__PURE__*/_interopNamespaceDefault(uuid);
24
+ var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
24
25
 
25
- var version = "7.3.0";
26
+ var version = "7.3.2";
26
27
 
27
28
  // Type guards for safer type checking
28
29
 
@@ -971,7 +972,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
971
972
 
972
973
  // Add additional token data to properties
973
974
  if (additionalTokenData.cacheReadInputTokens) {
974
- eventProperties['$ai_cache_read_tokens'] = additionalTokenData.cacheReadInputTokens;
975
+ eventProperties['$ai_cache_read_input_tokens'] = additionalTokenData.cacheReadInputTokens;
976
+ }
977
+ if (additionalTokenData.cacheWriteInputTokens) {
978
+ eventProperties['$ai_cache_creation_input_tokens'] = additionalTokenData.cacheWriteInputTokens;
975
979
  }
976
980
  if (additionalTokenData.reasoningTokens) {
977
981
  eventProperties['$ai_reasoning_tokens'] = additionalTokenData.reasoningTokens;
@@ -1139,6 +1143,15 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1139
1143
  additionalTokenData.cacheReadInputTokens = usage.input_token_details.cache_read;
1140
1144
  } else if (usage.cachedPromptTokens != null) {
1141
1145
  additionalTokenData.cacheReadInputTokens = usage.cachedPromptTokens;
1146
+ } else if (usage.cache_read_input_tokens != null) {
1147
+ additionalTokenData.cacheReadInputTokens = usage.cache_read_input_tokens;
1148
+ }
1149
+
1150
+ // Check for cache write/creation tokens in various formats
1151
+ if (usage.cache_creation_input_tokens != null) {
1152
+ additionalTokenData.cacheWriteInputTokens = usage.cache_creation_input_tokens;
1153
+ } else if (usage.input_token_details?.cache_creation != null) {
1154
+ additionalTokenData.cacheWriteInputTokens = usage.input_token_details.cache_creation;
1142
1155
  }
1143
1156
 
1144
1157
  // Check for reasoning tokens in various formats
@@ -1188,8 +1201,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1188
1201
  additionalTokenData.webSearchCount = webSearchCount;
1189
1202
  }
1190
1203
 
1191
- // For Anthropic providers, LangChain reports input_tokens as the sum of input and cache read tokens.
1204
+ // For Anthropic providers, LangChain reports input_tokens as the sum of all input tokens.
1192
1205
  // Our cost calculation expects them to be separate for Anthropic, so we subtract cache tokens.
1206
+ // Both cache_read and cache_write tokens should be subtracted since Anthropic's raw API
1207
+ // reports input_tokens as tokens NOT read from or used to create a cache.
1193
1208
  // For other providers (OpenAI, etc.), input_tokens already excludes cache tokens as expected.
1194
1209
  // Match logic consistent with plugin-server: exact match on provider OR substring match on model
1195
1210
  let isAnthropic = false;
@@ -1198,8 +1213,11 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1198
1213
  } else if (model && model.toLowerCase().includes('anthropic')) {
1199
1214
  isAnthropic = true;
1200
1215
  }
1201
- if (isAnthropic && parsedUsage.input && additionalTokenData.cacheReadInputTokens) {
1202
- parsedUsage.input = Math.max(parsedUsage.input - additionalTokenData.cacheReadInputTokens, 0);
1216
+ if (isAnthropic && parsedUsage.input) {
1217
+ const cacheTokens = (additionalTokenData.cacheReadInputTokens || 0) + (additionalTokenData.cacheWriteInputTokens || 0);
1218
+ if (cacheTokens > 0) {
1219
+ parsedUsage.input = Math.max(parsedUsage.input - cacheTokens, 0);
1220
+ }
1203
1221
  }
1204
1222
  return [parsedUsage.input, parsedUsage.output, additionalTokenData];
1205
1223
  }