@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.
@@ -11,7 +11,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
11
 
12
12
  var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
13
13
 
14
- var version = "7.4.0";
14
+ var version = "7.4.1";
15
15
 
16
16
  // Type guards for safer type checking
17
17
 
@@ -3,7 +3,7 @@ import { Buffer } from 'buffer';
3
3
  import { v4 } from 'uuid';
4
4
  import { uuidv7 } from '@posthog/core';
5
5
 
6
- var version = "7.4.0";
6
+ var version = "7.4.1";
7
7
 
8
8
  // Type guards for safer type checking
9
9
 
@@ -7,7 +7,7 @@ var buffer = require('buffer');
7
7
  var uuid = require('uuid');
8
8
  var core = require('@posthog/core');
9
9
 
10
- var version = "7.4.0";
10
+ var version = "7.4.1";
11
11
 
12
12
  // Type guards for safer type checking
13
13
 
@@ -3,7 +3,7 @@ import { Buffer } from 'buffer';
3
3
  import { v4 } from 'uuid';
4
4
  import { uuidv7 } from '@posthog/core';
5
5
 
6
- var version = "7.4.0";
6
+ var version = "7.4.1";
7
7
 
8
8
  // Type guards for safer type checking
9
9
 
package/dist/index.cjs CHANGED
@@ -30,7 +30,7 @@ function _interopNamespace(e) {
30
30
  var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
31
31
  var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
32
32
 
33
- var version = "7.4.0";
33
+ var version = "7.4.1";
34
34
 
35
35
  // Type guards for safer type checking
36
36
  const isString = value => {
@@ -1961,6 +1961,10 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1961
1961
  }
1962
1962
  }
1963
1963
 
1964
+ // Type guards
1965
+ function isV3Model(model) {
1966
+ return model.specificationVersion === 'v3';
1967
+ }
1964
1968
  const mapVercelParams = params => {
1965
1969
  return {
1966
1970
  temperature: params.temperature,
@@ -2181,6 +2185,19 @@ const extractAdditionalTokenValues = providerMetadata => {
2181
2185
  }
2182
2186
  return {};
2183
2187
  };
2188
+ // For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
2189
+ // Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
2190
+ // This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
2191
+ const adjustAnthropicV3CacheTokens = (model, provider, usage) => {
2192
+ if (isV3Model(model) && provider.toLowerCase().includes('anthropic')) {
2193
+ const cacheReadTokens = usage.cacheReadInputTokens || 0;
2194
+ const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
2195
+ const cacheTokens = cacheReadTokens + cacheWriteTokens;
2196
+ if (usage.inputTokens && cacheTokens > 0) {
2197
+ usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
2198
+ }
2199
+ }
2200
+ };
2184
2201
  // Helper to extract numeric token value from V2 (number) or V3 (object with .total) usage formats
2185
2202
  const extractTokenCount = value => {
2186
2203
  if (typeof value === 'number') {
@@ -2261,6 +2278,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
2261
2278
  webSearchCount,
2262
2279
  ...additionalTokenValues
2263
2280
  };
2281
+ adjustAnthropicV3CacheTokens(model, provider, usage);
2264
2282
  await sendEventToPosthog({
2265
2283
  client: phClient,
2266
2284
  distinctId: mergedOptions.posthogDistinctId,
@@ -2413,6 +2431,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
2413
2431
  ...usage,
2414
2432
  webSearchCount
2415
2433
  };
2434
+ adjustAnthropicV3CacheTokens(model, provider, finalUsage);
2416
2435
  await sendEventToPosthog({
2417
2436
  client: phClient,
2418
2437
  distinctId: mergedOptions.posthogDistinctId,