@posthog/ai 7.4.0 → 7.4.1

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
@@ -6,7 +6,7 @@ import { uuidv7 } from '@posthog/core';
6
6
  import AnthropicOriginal from '@anthropic-ai/sdk';
7
7
  import { GoogleGenAI } from '@google/genai';
8
8
 
9
- var version = "7.4.0";
9
+ var version = "7.4.1";
10
10
 
11
11
  // Type guards for safer type checking
12
12
  const isString = value => {
@@ -1937,6 +1937,10 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1937
1937
  }
1938
1938
  }
1939
1939
 
1940
+ // Type guards
1941
+ function isV3Model(model) {
1942
+ return model.specificationVersion === 'v3';
1943
+ }
1940
1944
  const mapVercelParams = params => {
1941
1945
  return {
1942
1946
  temperature: params.temperature,
@@ -2157,6 +2161,19 @@ const extractAdditionalTokenValues = providerMetadata => {
2157
2161
  }
2158
2162
  return {};
2159
2163
  };
2164
+ // For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
2165
+ // Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
2166
+ // This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
2167
+ const adjustAnthropicV3CacheTokens = (model, provider, usage) => {
2168
+ if (isV3Model(model) && provider.toLowerCase().includes('anthropic')) {
2169
+ const cacheReadTokens = usage.cacheReadInputTokens || 0;
2170
+ const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
2171
+ const cacheTokens = cacheReadTokens + cacheWriteTokens;
2172
+ if (usage.inputTokens && cacheTokens > 0) {
2173
+ usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
2174
+ }
2175
+ }
2176
+ };
2160
2177
  // Helper to extract numeric token value from V2 (number) or V3 (object with .total) usage formats
2161
2178
  const extractTokenCount = value => {
2162
2179
  if (typeof value === 'number') {
@@ -2237,6 +2254,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
2237
2254
  webSearchCount,
2238
2255
  ...additionalTokenValues
2239
2256
  };
2257
+ adjustAnthropicV3CacheTokens(model, provider, usage);
2240
2258
  await sendEventToPosthog({
2241
2259
  client: phClient,
2242
2260
  distinctId: mergedOptions.posthogDistinctId,
@@ -2389,6 +2407,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
2389
2407
  ...usage,
2390
2408
  webSearchCount
2391
2409
  };
2410
+ adjustAnthropicV3CacheTokens(model, provider, finalUsage);
2392
2411
  await sendEventToPosthog({
2393
2412
  client: phClient,
2394
2413
  distinctId: mergedOptions.posthogDistinctId,