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.

4.1 KB
Raw Blame
import clsx from 'clsx'

import { LemonTag } from '@posthog/lemon-ui'

import { dayjs } from 'lib/dayjs'
import { lowercaseFirstLetter } from 'lib/utils/strings'

import { CostBreakdownTooltip } from '../components/CostBreakdownTooltip'
import { MetadataTag } from '../components/MetadataTag'
import { CostContext, aiTokenCount, formatLLMCost, hasCostBreakdown } from '../utils'

export function MetadataHeader({
    inputTokens,
    outputTokens,
    costContext,
    model,
    latency,
    className,
    isError,
    cacheReadTokens,
    cacheWriteTokens,
    timestamp,
    timeToFirstToken,
    isStreaming,
}: {
    /** Token props read leniently, so a string or `{ total, … }` count still renders its tag. */
    inputTokens?: unknown
    outputTokens?: unknown
    cacheReadTokens?: unknown
    cacheWriteTokens?: unknown
    costContext?: CostContext
    model?: string
    latency?: number
    isError?: boolean
    className?: string
    timestamp?: string
    timeToFirstToken?: number
    isStreaming?: boolean
}): JSX.Element {
    const inputTokenCount = aiTokenCount(inputTokens)
    const outputTokenCount = aiTokenCount(outputTokens)
    const cacheReadTokenCount = aiTokenCount(cacheReadTokens)
    const cacheWriteTokenCount = aiTokenCount(cacheWriteTokens)

    return (
        <div className={clsx('flex flex-wrap gap-2', className)}>
            {isError && <LemonTag type="danger">Error</LemonTag>}
            {typeof latency === 'number' && (
                <MetadataTag
                    label="Latency"
                    tooltipContent="End-to-end latency from request to final response."
                >{`${Math.round(latency * 10e2) / 10e2}s latency`}</MetadataTag>
            )}
            {typeof timeToFirstToken === 'number' && isStreaming && (
                <MetadataTag
                    label="Time to first token"
                    tooltipContent="Time between sending the request and receiving the first streamed token."
                >
                    {timeToFirstToken < 1
                        ? `${Math.round(timeToFirstToken * 1000)} ms`
                        : `${timeToFirstToken.toFixed(2)}s TTFT`}
                </MetadataTag>
            )}
            {timestamp && <MetadataTag label="Timestamp">{dayjs(timestamp).format('MMM D, YYYY h:mm A')}</MetadataTag>}
            {inputTokenCount !== null && outputTokenCount !== null && (
                <MetadataTag
                    label="Token usage"
                    tooltipContent="Input and output tokens consumed by this generation call."
                >
                    {`${inputTokenCount} prompt tokens → ${outputTokenCount} completion tokens (∑ ${
                        inputTokenCount + outputTokenCount
                    })`}
                </MetadataTag>
            )}
            {cacheReadTokenCount !== null && cacheReadTokenCount > 0 && (
                <MetadataTag
                    label="Cache read"
                    tooltipContent="Prompt tokens served from the provider's cache (cheaper, faster)."
                >{`${cacheReadTokenCount} cache read tokens`}</MetadataTag>
            )}
            {cacheWriteTokenCount !== null && cacheWriteTokenCount > 0 && (
                <MetadataTag
                    label="Cache write"
                    tooltipContent="Prompt tokens written to the provider's cache for reuse on subsequent calls."
                >{`${cacheWriteTokenCount} cache write tokens`}</MetadataTag>
            )}
            {typeof model === 'string' && model && (
                <MetadataTag label="Model" textToCopy={lowercaseFirstLetter(model)}>
                    {model}
                </MetadataTag>
            )}
            {costContext && (
                <MetadataTag
                    label="Total generation cost"
                    tooltipContent={
                        hasCostBreakdown(costContext) ? <CostBreakdownTooltip costContext={costContext} /> : undefined
                    }
                >
                    {formatLLMCost(costContext.totalCost)}
                </MetadataTag>
            )}
        </div>
    )
}