@posthog/ai 7.3.1 → 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.
package/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ import { v4 } from 'uuid';
5
5
  import AnthropicOriginal from '@anthropic-ai/sdk';
6
6
  import { GoogleGenAI } from '@google/genai';
7
7
 
8
- var version = "7.3.1";
8
+ var version = "7.3.2";
9
9
 
10
10
  // Type guards for safer type checking
11
11
  const isString = value => {
@@ -3818,7 +3818,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
3818
3818
  eventProperties['$ai_output_tokens'] = outputTokens;
3819
3819
  // Add additional token data to properties
3820
3820
  if (additionalTokenData.cacheReadInputTokens) {
3821
- eventProperties['$ai_cache_read_tokens'] = additionalTokenData.cacheReadInputTokens;
3821
+ eventProperties['$ai_cache_read_input_tokens'] = additionalTokenData.cacheReadInputTokens;
3822
+ }
3823
+ if (additionalTokenData.cacheWriteInputTokens) {
3824
+ eventProperties['$ai_cache_creation_input_tokens'] = additionalTokenData.cacheWriteInputTokens;
3822
3825
  }
3823
3826
  if (additionalTokenData.reasoningTokens) {
3824
3827
  eventProperties['$ai_reasoning_tokens'] = additionalTokenData.reasoningTokens;
@@ -3982,6 +3985,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
3982
3985
  additionalTokenData.cacheReadInputTokens = usage.input_token_details.cache_read;
3983
3986
  } else if (usage.cachedPromptTokens != null) {
3984
3987
  additionalTokenData.cacheReadInputTokens = usage.cachedPromptTokens;
3988
+ } else if (usage.cache_read_input_tokens != null) {
3989
+ additionalTokenData.cacheReadInputTokens = usage.cache_read_input_tokens;
3990
+ }
3991
+ // Check for cache write/creation tokens in various formats
3992
+ if (usage.cache_creation_input_tokens != null) {
3993
+ additionalTokenData.cacheWriteInputTokens = usage.cache_creation_input_tokens;
3994
+ } else if (usage.input_token_details?.cache_creation != null) {
3995
+ additionalTokenData.cacheWriteInputTokens = usage.input_token_details.cache_creation;
3985
3996
  }
3986
3997
  // Check for reasoning tokens in various formats
3987
3998
  if (usage.completion_tokens_details?.reasoning_tokens != null) {
@@ -4027,8 +4038,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
4027
4038
  if (webSearchCount !== undefined) {
4028
4039
  additionalTokenData.webSearchCount = webSearchCount;
4029
4040
  }
4030
- // For Anthropic providers, LangChain reports input_tokens as the sum of input and cache read tokens.
4041
+ // For Anthropic providers, LangChain reports input_tokens as the sum of all input tokens.
4031
4042
  // Our cost calculation expects them to be separate for Anthropic, so we subtract cache tokens.
4043
+ // Both cache_read and cache_write tokens should be subtracted since Anthropic's raw API
4044
+ // reports input_tokens as tokens NOT read from or used to create a cache.
4032
4045
  // For other providers (OpenAI, etc.), input_tokens already excludes cache tokens as expected.
4033
4046
  // Match logic consistent with plugin-server: exact match on provider OR substring match on model
4034
4047
  let isAnthropic = false;
@@ -4037,8 +4050,11 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
4037
4050
  } else if (model && model.toLowerCase().includes('anthropic')) {
4038
4051
  isAnthropic = true;
4039
4052
  }
4040
- if (isAnthropic && parsedUsage.input && additionalTokenData.cacheReadInputTokens) {
4041
- parsedUsage.input = Math.max(parsedUsage.input - additionalTokenData.cacheReadInputTokens, 0);
4053
+ if (isAnthropic && parsedUsage.input) {
4054
+ const cacheTokens = (additionalTokenData.cacheReadInputTokens || 0) + (additionalTokenData.cacheWriteInputTokens || 0);
4055
+ if (cacheTokens > 0) {
4056
+ parsedUsage.input = Math.max(parsedUsage.input - cacheTokens, 0);
4057
+ }
4042
4058
  }
4043
4059
  return [parsedUsage.input, parsedUsage.output, additionalTokenData];
4044
4060
  }