Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

16.9 KB
Raw Blame
import { aiCostModalityExtractionCounter } from '~/ingestion/pipelines/ai/metrics'
import { PluginEvent, Properties } from '~/plugin-scaffold'

export interface EventWithProperties extends PluginEvent {
    properties: Properties
}

const isObject = (value: unknown): value is Record<string, unknown> => {
    return typeof value === 'object' && value !== null && !Array.isArray(value)
}

/**
 * Look up a metadata key under either the camelCase or snake_case form. Gemini
 * arrives camelCased through the Vercel AI SDK (Node.js, raw API JSON) and
 * snake_cased through posthog-python (protobuf to_dict / vars). Both shapes are
 * routinely seen in production.
 */
const pick = (metadata: Record<string, unknown>, ...keys: string[]): unknown => {
    for (const key of keys) {
        const value = metadata[key]
        if (value !== undefined && value !== null) {
            return value
        }
    }
    return undefined
}

const isValidTokenCount = (value: unknown): value is number => {
    return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0
}

const tokenCountOf = (detail: Record<string, unknown>): number | null => {
    const tokenCount = detail['tokenCount'] ?? detail['token_count'] ?? detail['tokens']
    return isValidTokenCount(tokenCount) ? tokenCount : null
}

const modalityOf = (detail: Record<string, unknown>): string | null => {
    const modality = detail['modality']
    return typeof modality === 'string' ? modality.toLowerCase() : null
}

type ExtractionSource =
    | 'anthropic_cache'
    | 'bedrock_cache'
    | 'gemini_input'
    | 'gemini_output'
    | 'gemini_cache'
    | 'openai_input'
    | 'openai_cache'

/**
 * Extract token details used by cost calculation from raw provider usage metadata.
 * Supports Anthropic's cache-creation TTL breakdown, Gemini's input/output/cache
 * modality details, and OpenAI's prompt_tokens_details. Removes $ai_usage from
 * properties after extraction so it does not get persisted to ClickHouse.
 */
export const extractModalityTokens = (event: EventWithProperties): EventWithProperties => {
    const usage = event.properties['$ai_usage']

    if (!usage || typeof usage !== 'object') {
        delete event.properties['$ai_usage']
        return event
    }

    try {
        const extractedSources = new Set<ExtractionSource>()

        // Gemini's promptTokensDetails / prompt_tokens_details shape (input modality).
        // Array form: [{ modality: "AUDIO", tokenCount: 750 }, { modality: "TEXT", tokenCount: 598 }]
        // (token_count snake_case also accepted via tokenCountOf).
        // Object form: { audioTokens: 750, textTokens: 598 } (defensive fallback).
        const extractInputModality = (tokenDetails: unknown): void => {
            if (!tokenDetails) {
                return
            }

            if (Array.isArray(tokenDetails)) {
                for (const detail of tokenDetails) {
                    if (!isObject(detail)) {
                        continue
                    }
                    const modality = modalityOf(detail)
                    const tokenCount = tokenCountOf(detail)
                    if (modality === null || tokenCount === null) {
                        continue
                    }
                    if (modality === 'audio' && tokenCount > 0) {
                        event.properties['$ai_audio_input_tokens'] = tokenCount
                        extractedSources.add('gemini_input')
                    }
                    if (modality === 'image' && tokenCount > 0) {
                        event.properties['$ai_image_input_tokens'] = tokenCount
                        extractedSources.add('gemini_input')
                    }
                    if (modality === 'text') {
                        event.properties['$ai_text_input_tokens'] = tokenCount
                        extractedSources.add('gemini_input')
                    }
                }
            } else if (isObject(tokenDetails)) {
                if (isValidTokenCount(tokenDetails['audioTokens']) && tokenDetails['audioTokens'] > 0) {
                    event.properties['$ai_audio_input_tokens'] = tokenDetails['audioTokens']
                    extractedSources.add('gemini_input')
                }
                if (isValidTokenCount(tokenDetails['imageTokens']) && tokenDetails['imageTokens'] > 0) {
                    event.properties['$ai_image_input_tokens'] = tokenDetails['imageTokens']
                    extractedSources.add('gemini_input')
                }
                if (isValidTokenCount(tokenDetails['textTokens'])) {
                    event.properties['$ai_text_input_tokens'] = tokenDetails['textTokens']
                    extractedSources.add('gemini_input')
                }
            }
        }

        // Gemini's candidatesTokensDetails / candidates_tokens_details shape (output modality).
        const extractOutputModality = (tokenDetails: unknown): void => {
            if (!tokenDetails) {
                return
            }

            if (Array.isArray(tokenDetails)) {
                for (const detail of tokenDetails) {
                    if (!isObject(detail)) {
                        continue
                    }
                    const modality = modalityOf(detail)
                    const tokenCount = tokenCountOf(detail)
                    if (modality === null || tokenCount === null) {
                        continue
                    }
                    if (modality === 'image' && tokenCount > 0) {
                        event.properties['$ai_image_output_tokens'] = tokenCount
                        extractedSources.add('gemini_output')
                    }
                    if (modality === 'audio' && tokenCount > 0) {
                        event.properties['$ai_audio_output_tokens'] = tokenCount
                        extractedSources.add('gemini_output')
                    }
                    if (modality === 'text') {
                        event.properties['$ai_text_output_tokens'] = tokenCount
                        extractedSources.add('gemini_output')
                    }
                }
            } else if (isObject(tokenDetails)) {
                if (isValidTokenCount(tokenDetails['audioTokens']) && tokenDetails['audioTokens'] > 0) {
                    event.properties['$ai_audio_output_tokens'] = tokenDetails['audioTokens']
                    extractedSources.add('gemini_output')
                }
                if (isValidTokenCount(tokenDetails['imageTokens']) && tokenDetails['imageTokens'] > 0) {
                    event.properties['$ai_image_output_tokens'] = tokenDetails['imageTokens']
                    extractedSources.add('gemini_output')
                }
                if (isValidTokenCount(tokenDetails['textTokens'])) {
                    event.properties['$ai_text_output_tokens'] = tokenDetails['textTokens']
                    extractedSources.add('gemini_output')
                }
            }
        }

        // Gemini's cacheTokensDetails / cache_tokens_details shape (cache modality).
        const extractCacheModality = (tokenDetails: unknown): void => {
            if (!tokenDetails) {
                return
            }

            if (Array.isArray(tokenDetails)) {
                for (const detail of tokenDetails) {
                    if (!isObject(detail)) {
                        continue
                    }
                    const modality = modalityOf(detail)
                    const tokenCount = tokenCountOf(detail)
                    if (modality === null || tokenCount === null) {
                        continue
                    }
                    if (modality === 'audio' && tokenCount > 0) {
                        event.properties['$ai_cache_read_audio_tokens'] = tokenCount
                        extractedSources.add('gemini_cache')
                    }
                    if (modality === 'image' && tokenCount > 0) {
                        event.properties['$ai_cache_read_image_tokens'] = tokenCount
                        extractedSources.add('gemini_cache')
                    }
                }
            } else if (isObject(tokenDetails)) {
                if (isValidTokenCount(tokenDetails['audioTokens']) && tokenDetails['audioTokens'] > 0) {
                    event.properties['$ai_cache_read_audio_tokens'] = tokenDetails['audioTokens']
                    extractedSources.add('gemini_cache')
                }
                if (isValidTokenCount(tokenDetails['imageTokens']) && tokenDetails['imageTokens'] > 0) {
                    event.properties['$ai_cache_read_image_tokens'] = tokenDetails['imageTokens']
                    extractedSources.add('gemini_cache')
                }
            }
        }

        // OpenAI shape: { prompt_tokens_details: { audio_tokens: 200, cached_tokens_details: { audio_tokens: 50 } } }
        // gpt-audio / gpt-realtime expose total prompt audio at audio_tokens, and
        // cached audio inside cached_tokens_details.audio_tokens.
        const extractOpenAIInputModality = (metadata: Record<string, unknown>): void => {
            const promptDetails = metadata['prompt_tokens_details']
            if (!isObject(promptDetails)) {
                return
            }
            const audioTokens = promptDetails['audio_tokens']
            if (isValidTokenCount(audioTokens) && audioTokens > 0) {
                event.properties['$ai_audio_input_tokens'] = audioTokens
                extractedSources.add('openai_input')
            }
        }

        const extractOpenAICacheModality = (metadata: Record<string, unknown>): void => {
            const promptDetails = metadata['prompt_tokens_details']
            if (!isObject(promptDetails)) {
                return
            }
            const cachedDetails = promptDetails['cached_tokens_details']
            if (!isObject(cachedDetails)) {
                return
            }
            const audioTokens = cachedDetails['audio_tokens']
            if (isValidTokenCount(audioTokens) && audioTokens > 0) {
                event.properties['$ai_cache_read_audio_tokens'] = audioTokens
                extractedSources.add('openai_cache')
            }
        }

        const extractAnthropicCacheCreation = (metadata: Record<string, unknown>): void => {
            const cacheCreation = metadata['cache_creation']
            if (!isObject(cacheCreation)) {
                return
            }

            const fiveMinuteTokens = cacheCreation['ephemeral_5m_input_tokens']
            const oneHourTokens = cacheCreation['ephemeral_1h_input_tokens']
            if (typeof fiveMinuteTokens === 'number' && fiveMinuteTokens >= 0) {
                event.properties['$ai_cache_creation_5m_input_tokens'] = fiveMinuteTokens
                extractedSources.add('anthropic_cache')
            }
            if (typeof oneHourTokens === 'number' && oneHourTokens >= 0) {
                event.properties['$ai_cache_creation_1h_input_tokens'] = oneHourTokens
                extractedSources.add('anthropic_cache')
            }
        }

        const extractBedrockCacheCreation = (metadata: Record<string, unknown>): void => {
            const amazonBedrockMetadata = metadata['amazonBedrock']
            const bedrockMetadata = isObject(amazonBedrockMetadata) ? amazonBedrockMetadata : metadata['bedrock']
            if (!isObject(bedrockMetadata) || !isObject(bedrockMetadata['usage'])) {
                return
            }

            const cacheDetails = bedrockMetadata['usage']['cacheDetails']
            if (!Array.isArray(cacheDetails)) {
                return
            }

            let fiveMinuteTokens = 0
            let oneHourTokens = 0
            let foundCacheDetails = false

            for (const detail of cacheDetails) {
                if (!isObject(detail)) {
                    continue
                }

                const inputTokens = detail['inputTokens']
                const ttl = typeof detail['ttl'] === 'string' ? detail['ttl'].toLowerCase() : null
                if (typeof inputTokens !== 'number' || !Number.isFinite(inputTokens) || inputTokens < 0) {
                    continue
                }

                if (ttl === 't5m' || ttl === '5m') {
                    fiveMinuteTokens += inputTokens
                    foundCacheDetails = true
                } else if (ttl === 't1h' || ttl === '1h') {
                    oneHourTokens += inputTokens
                    foundCacheDetails = true
                }
            }

            if (foundCacheDetails) {
                event.properties['$ai_cache_creation_5m_input_tokens'] = fiveMinuteTokens
                event.properties['$ai_cache_creation_1h_input_tokens'] = oneHourTokens
                extractedSources.add('bedrock_cache')
            }
        }

        // Walk each `usage`-shaped metadata object encountered across the SDK
        // wrapper variants and pull every modality breakdown it exposes.
        // Gemini metadata arrives camelCased through the Vercel AI SDK and
        // snake_cased through posthog-python; check both forms for each key.
        const extractFromMetadata = (metadata: unknown): void => {
            if (!isObject(metadata)) {
                return
            }
            extractAnthropicCacheCreation(metadata)
            extractInputModality(
                pick(metadata, 'promptTokensDetails', 'prompt_tokens_details', 'input_tokens_by_modality')
            )
            extractOutputModality(
                pick(
                    metadata,
                    'candidatesTokensDetails',
                    'candidates_tokens_details',
                    'outputTokenDetails',
                    'output_tokens_by_modality'
                )
            )
            extractCacheModality(
                pick(metadata, 'cacheTokensDetails', 'cache_tokens_details', 'cached_tokens_by_modality')
            )
            extractOpenAIInputModality(metadata)
            extractOpenAICacheModality(metadata)
        }

        extractFromMetadata(usage)

        // @posthog/ai preserves raw Vercel usage under usage.raw.
        const usageDetails = (usage as Record<string, unknown>)['usage']
        if (isObject(usageDetails)) {
            extractFromMetadata(usageDetails['raw'])
        }

        // Vercel AI SDK with rawResponse at top level: { rawResponse: { usageMetadata: {...} } }
        const topLevelRawResponse = (usage as Record<string, unknown>)['rawResponse']
        if (isObject(topLevelRawResponse)) {
            extractFromMetadata(topLevelRawResponse['usageMetadata'])
        }

        // Vercel AI SDK V2 structure: { providerMetadata: { google: {...} } }
        const providerMetadata = (usage as Record<string, unknown>)['providerMetadata']
        if (isObject(providerMetadata)) {
            extractFromMetadata(providerMetadata['google'])
            extractBedrockCacheCreation(providerMetadata)

            // Anthropic's original usage is also available in provider metadata.
            const anthropicProviderMetadata = providerMetadata['anthropic']
            if (isObject(anthropicProviderMetadata)) {
                extractFromMetadata(anthropicProviderMetadata['usage'])
            }
        }

        // Vercel AI SDK V3 / nested rawUsage variants:
        //   { rawUsage: { providerMetadata: { google: {...} } } }
        //   { rawUsage: { usage: { raw: {...} } } }
        //   { rawUsage: { rawResponse: { usageMetadata: {...} } } }
        const rawUsage = (usage as Record<string, unknown>)['rawUsage']
        if (isObject(rawUsage)) {
            const rawProviderMetadata = rawUsage['providerMetadata']
            if (isObject(rawProviderMetadata)) {
                extractFromMetadata(rawProviderMetadata['google'])
                extractBedrockCacheCreation(rawProviderMetadata)
            }

            const rawUsageUsage = rawUsage['usage']
            if (isObject(rawUsageUsage)) {
                extractFromMetadata(rawUsageUsage['raw'])
            }

            const rawResponse = rawUsage['rawResponse']
            if (isObject(rawResponse)) {
                extractFromMetadata(rawResponse['usageMetadata'])
            }
        }

        // Emit one counter increment per source that found something. An event
        // with both Gemini output and Gemini cache extraction increments twice
        // (once with source=gemini_output, once with source=gemini_cache). Sort
        // for deterministic ordering across runs — useful for tests and for
        // anyone reasoning about the metric stream.
        if (extractedSources.size === 0) {
            aiCostModalityExtractionCounter.labels({ status: 'no_details', source: 'none' }).inc()
        } else {
            for (const source of [...extractedSources].sort()) {
                aiCostModalityExtractionCounter.labels({ status: 'extracted', source }).inc()
            }
        }
    } finally {
        // CRITICAL: Always delete $ai_usage to prevent it from being stored in ClickHouse
        // This must happen regardless of whether extraction succeeds or fails
        delete event.properties['$ai_usage']
    }

    return event
}