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.

3.0 KB
Raw Blame
import { isAiEventName } from '~/ingestion/common/ai-event-types'
import { PipelineWarning } from '~/ingestion/framework/pipeline.interface'
import { ok } from '~/ingestion/framework/results'
import { ProcessingStep } from '~/ingestion/framework/steps'
import { PipelineEvent } from '~/types'

const TOKEN_PROPERTIES = [
    '$ai_input_tokens',
    '$ai_output_tokens',
    '$ai_reasoning_tokens',
    '$ai_cache_read_input_tokens',
    '$ai_cache_creation_input_tokens',
    '$ai_cache_creation_5m_input_tokens',
    '$ai_cache_creation_1h_input_tokens',
] as const

/**
 * Validates that a value can be safely used with js-big-decimal for cost calculations.
 * Valid values are:
 * - Finite numbers (not NaN, not Infinity)
 * - Numeric strings (e.g., '100', '100.5', '-100')
 * - null/undefined (will be defaulted to 0 downstream)
 */
function isValidBigDecimalInput(value: unknown): boolean {
    if (value === null || value === undefined) {
        return true
    }

    if (typeof value === 'number') {
        return Number.isFinite(value)
    }

    if (typeof value === 'string') {
        if (value === '') {
            return true
        }
        const parsed = Number(value)
        return !Number.isNaN(parsed) && Number.isFinite(parsed)
    }

    return false
}

/**
 * Some SDKs (e.g. posthog-ai < 7.3.0 with Vercel AI SDK V3) send token counts
 * as nested objects like `{ total: 10585, noCache: 10585, cacheRead: 0 }` instead
 * of plain numbers. Extract the numeric `total` field when present.
 */
function normalizeTokenValue(value: unknown): unknown {
    if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
        const obj = value as Record<string, unknown>
        if ('total' in obj) {
            return obj.total
        }
    }
    return value
}

export function createValidateAiEventTokensStep<T extends { event: PipelineEvent }>(): ProcessingStep<T, T> {
    return async function validateAiEventTokensStep(input) {
        const { event } = input

        if (!isAiEventName(event.event)) {
            return Promise.resolve(ok(input))
        }

        const properties = event.properties

        if (!properties) {
            return Promise.resolve(ok(input))
        }

        const warnings: PipelineWarning[] = []

        for (const prop of TOKEN_PROPERTIES) {
            if (!(prop in properties)) {
                continue
            }
            const raw = properties[prop]
            const normalized = normalizeTokenValue(raw)
            properties[prop] = normalized

            if (!isValidBigDecimalInput(normalized)) {
                warnings.push({
                    type: 'invalid_ai_token_property',
                    details: {
                        property: prop,
                        value: String(normalized),
                        valueType: typeof normalized,
                    },
                })
                properties[prop] = null
            }
        }

        return Promise.resolve(ok(input, [], warnings))
    }
}