@posthog/ai 7.16.1 → 7.16.3
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/anthropic/index.cjs +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/gemini/index.cjs +1 -1
- package/dist/gemini/index.mjs +1 -1
- package/dist/index.cjs +61 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +61 -28
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/openai/index.cjs +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/openai-agents/index.cjs +1 -1
- package/dist/openai-agents/index.mjs +1 -1
- package/dist/vercel/index.cjs +65 -30
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +65 -30
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { uuidv7 } from '@posthog/core';
|
|
|
5
5
|
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
6
6
|
import { GoogleGenAI } from '@google/genai';
|
|
7
7
|
|
|
8
|
-
var version = "7.16.
|
|
8
|
+
var version = "7.16.3";
|
|
9
9
|
|
|
10
10
|
// Type guards for safer type checking
|
|
11
11
|
const isString = value => {
|
|
@@ -2189,12 +2189,14 @@ const mapVercelOutput = result => {
|
|
|
2189
2189
|
};
|
|
2190
2190
|
}
|
|
2191
2191
|
if (item.type === 'tool-call') {
|
|
2192
|
+
const toolCall = item;
|
|
2193
|
+
const rawArgs = toolCall.input ?? toolCall.args ?? toolCall.arguments ?? {};
|
|
2192
2194
|
return {
|
|
2193
2195
|
type: 'tool-call',
|
|
2194
2196
|
id: item.toolCallId,
|
|
2195
2197
|
function: {
|
|
2196
2198
|
name: item.toolName,
|
|
2197
|
-
arguments:
|
|
2199
|
+
arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs)
|
|
2198
2200
|
}
|
|
2199
2201
|
};
|
|
2200
2202
|
}
|
|
@@ -2278,28 +2280,6 @@ const extractWebSearchCount = (providerMetadata, usage) => {
|
|
|
2278
2280
|
providerMetadata
|
|
2279
2281
|
});
|
|
2280
2282
|
};
|
|
2281
|
-
// Extract additional token values from provider metadata
|
|
2282
|
-
const extractAdditionalTokenValues = providerMetadata => {
|
|
2283
|
-
if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'cacheCreationInputTokens' in providerMetadata.anthropic) {
|
|
2284
|
-
return {
|
|
2285
|
-
cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens
|
|
2286
|
-
};
|
|
2287
|
-
}
|
|
2288
|
-
return {};
|
|
2289
|
-
};
|
|
2290
|
-
// For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
|
|
2291
|
-
// Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
|
|
2292
|
-
// This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
|
|
2293
|
-
const adjustAnthropicV3CacheTokens = (model, provider, usage) => {
|
|
2294
|
-
if (isV3Model(model) && provider.toLowerCase().includes('anthropic')) {
|
|
2295
|
-
const cacheReadTokens = usage.cacheReadInputTokens || 0;
|
|
2296
|
-
const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
|
|
2297
|
-
const cacheTokens = cacheReadTokens + cacheWriteTokens;
|
|
2298
|
-
if (usage.inputTokens && cacheTokens > 0) {
|
|
2299
|
-
usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
|
|
2300
|
-
}
|
|
2301
|
-
}
|
|
2302
|
-
};
|
|
2303
2283
|
// Helper to extract numeric token value from V2 (number) or V3 (object with .total) usage formats
|
|
2304
2284
|
const extractTokenCount = value => {
|
|
2305
2285
|
if (typeof value === 'number') {
|
|
@@ -2334,6 +2314,59 @@ const extractCacheReadTokens = usage => {
|
|
|
2334
2314
|
}
|
|
2335
2315
|
return undefined;
|
|
2336
2316
|
};
|
|
2317
|
+
// Helper to extract cache write tokens from V3 (usage.inputTokens.cacheWrite). Providers like
|
|
2318
|
+
// Amazon Bedrock populate this standardized field instead of providerMetadata.anthropic.
|
|
2319
|
+
const extractCacheWriteTokens = usage => {
|
|
2320
|
+
if ('inputTokens' in usage && usage.inputTokens && typeof usage.inputTokens === 'object' && 'cacheWrite' in usage.inputTokens) {
|
|
2321
|
+
return usage.inputTokens.cacheWrite;
|
|
2322
|
+
}
|
|
2323
|
+
return undefined;
|
|
2324
|
+
};
|
|
2325
|
+
// Extract additional token values from provider metadata, with a V3 standardized fallback
|
|
2326
|
+
// (e.g. Amazon Bedrock exposes cache write tokens via usage.inputTokens.cacheWrite rather
|
|
2327
|
+
// than providerMetadata.anthropic.cacheCreationInputTokens). A cacheWrite of 0 is treated
|
|
2328
|
+
// as absent so we preserve the pre-fallback event shape on providers that simply omit the
|
|
2329
|
+
// field — consumers downstream saw `$ai_cache_creation_input_tokens` missing, not 0.
|
|
2330
|
+
const extractAdditionalTokenValues = (providerMetadata, usage) => {
|
|
2331
|
+
if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'cacheCreationInputTokens' in providerMetadata.anthropic) {
|
|
2332
|
+
return {
|
|
2333
|
+
cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens
|
|
2334
|
+
};
|
|
2335
|
+
}
|
|
2336
|
+
if (usage && typeof usage === 'object') {
|
|
2337
|
+
const cacheWrite = extractCacheWriteTokens(usage);
|
|
2338
|
+
if (typeof cacheWrite === 'number' && cacheWrite > 0) {
|
|
2339
|
+
return {
|
|
2340
|
+
cacheCreationInputTokens: cacheWrite
|
|
2341
|
+
};
|
|
2342
|
+
}
|
|
2343
|
+
}
|
|
2344
|
+
return {};
|
|
2345
|
+
};
|
|
2346
|
+
// Detects Anthropic Claude regardless of host (direct Anthropic, Amazon Bedrock, Google Vertex, etc.).
|
|
2347
|
+
// The server applies exclusive cache token accounting based on the model name, so any Claude model
|
|
2348
|
+
// needs its V3 input tokens adjusted to exclude cache tokens — not just those routed through a
|
|
2349
|
+
// provider whose name contains "anthropic". Accepts the resolved modelId string (not the raw model)
|
|
2350
|
+
// so it sees the same id the server does after posthogModelOverride / response.modelId fallbacks.
|
|
2351
|
+
const isAnthropicClaudeModel = (modelId, provider) => {
|
|
2352
|
+
if (provider.toLowerCase().includes('anthropic')) {
|
|
2353
|
+
return true;
|
|
2354
|
+
}
|
|
2355
|
+
return /claude|anthropic/i.test(modelId);
|
|
2356
|
+
};
|
|
2357
|
+
// For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
|
|
2358
|
+
// Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
|
|
2359
|
+
// This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
|
|
2360
|
+
const adjustAnthropicV3CacheTokens = (model, modelId, provider, usage) => {
|
|
2361
|
+
if (isV3Model(model) && isAnthropicClaudeModel(modelId, provider)) {
|
|
2362
|
+
const cacheReadTokens = usage.cacheReadInputTokens || 0;
|
|
2363
|
+
const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
|
|
2364
|
+
const cacheTokens = cacheReadTokens + cacheWriteTokens;
|
|
2365
|
+
if (usage.inputTokens && cacheTokens > 0) {
|
|
2366
|
+
usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
};
|
|
2337
2370
|
/**
|
|
2338
2371
|
* Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
|
|
2339
2372
|
* Automatically detects the model version and applies appropriate instrumentation.
|
|
@@ -2370,7 +2403,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2370
2403
|
const content = mapVercelOutput(result.content ?? []);
|
|
2371
2404
|
const latency = (Date.now() - startTime) / 1000;
|
|
2372
2405
|
const providerMetadata = result.providerMetadata;
|
|
2373
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
2406
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, result.usage);
|
|
2374
2407
|
const webSearchCount = extractWebSearchCount(providerMetadata, result.usage);
|
|
2375
2408
|
// V2 usage has simple numbers, V3 has objects with .total - normalize both
|
|
2376
2409
|
const usageObj = result.usage;
|
|
@@ -2398,7 +2431,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2398
2431
|
...additionalTokenValues,
|
|
2399
2432
|
rawUsage: rawUsageData
|
|
2400
2433
|
};
|
|
2401
|
-
adjustAnthropicV3CacheTokens(model, provider, usage);
|
|
2434
|
+
adjustAnthropicV3CacheTokens(model, modelId, provider, usage);
|
|
2402
2435
|
// Extract finish reason - V2 returns a string, V3 returns an object with .unified
|
|
2403
2436
|
const rawFinishReason = result.finishReason;
|
|
2404
2437
|
const finishReasonStr = typeof rawFinishReason === 'string' ? rawFinishReason : rawFinishReason && typeof rawFinishReason === 'object' && 'unified' in rawFinishReason ? String(rawFinishReason.unified) : undefined;
|
|
@@ -2522,8 +2555,8 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2522
2555
|
}
|
|
2523
2556
|
if (chunk.type === 'finish') {
|
|
2524
2557
|
providerMetadata = chunk.providerMetadata;
|
|
2525
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
2526
2558
|
const chunkUsage = chunk.usage || {};
|
|
2559
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, chunkUsage);
|
|
2527
2560
|
usage = {
|
|
2528
2561
|
inputTokens: extractTokenCount(chunk.usage?.inputTokens),
|
|
2529
2562
|
outputTokens: extractTokenCount(chunk.usage?.outputTokens),
|
|
@@ -2586,7 +2619,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2586
2619
|
providerMetadata
|
|
2587
2620
|
}
|
|
2588
2621
|
};
|
|
2589
|
-
adjustAnthropicV3CacheTokens(model, provider, finalUsage);
|
|
2622
|
+
adjustAnthropicV3CacheTokens(model, modelId, provider, finalUsage);
|
|
2590
2623
|
await sendEventToPosthog({
|
|
2591
2624
|
client: phClient,
|
|
2592
2625
|
distinctId: mergedOptions.posthogDistinctId,
|