: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { IconChevronDown, IconChevronRight } from '@posthog/icons'
import { LemonTag, Tooltip } from '@posthog/lemon-ui'
import type { LLMSentimentResult } from '~/queries/schema/schema-general'
import { EventDetails } from '~/scenes/activity/explore/EventDetails'
import { EventType } from '~/types'
import { asString, costContextFromProperties, formatLLMCost, hasCostBreakdown } from '../utils'
import { CostBreakdownTooltip } from './CostBreakdownTooltip'
import { SentimentBar } from './SentimentTag'
interface AIObservabilityEventCardProps {
event: {
id: string
event: string
createdAt: string
properties: Record<string, any>
sentiment?: LLMSentimentResult
}
isExpanded: boolean
onToggleExpand: () => void
}
export function AIObservabilityEventCard({
event,
isExpanded,
onToggleExpand,
}: AIObservabilityEventCardProps): JSX.Element {
const isGeneration = event.event === '$ai_generation'
const isEmbedding = event.event === '$ai_embedding'
const eventForDetails: EventType = {
id: event.id,
distinct_id: '',
properties: event.properties,
event: event.event,
timestamp: event.createdAt,
elements: [],
}
const latency = event.properties.$ai_latency
const hasError = event.properties.$ai_error || event.properties.$ai_is_error
// Generation-specific properties
const model = asString(event.properties.$ai_model) || 'Unknown model'
const costContext = isGeneration || isEmbedding ? costContextFromProperties(event.properties) : undefined
// Span-specific properties
const spanName = asString(event.properties.$ai_span_name) || 'Unnamed span'
return (
<div className="border rounded bg-bg-3000">
<div className="p-2 hover:bg-side-light cursor-pointer flex items-center gap-2" onClick={onToggleExpand}>
<div className="flex-shrink-0">
{isExpanded ? (
<IconChevronDown className="text-base" />
) : (
<IconChevronRight className="text-base" />
)}
</div>
<div className="flex-1 flex items-center gap-2 flex-wrap min-w-0">
<LemonTag
type={isGeneration ? 'success' : isEmbedding ? 'warning' : 'default'}
size="small"
className="uppercase"
>
{isGeneration ? 'Generation' : isEmbedding ? 'Embedding' : 'Span'}
</LemonTag>
{hasError && (
<LemonTag type="danger" size="small">
Error
</LemonTag>
)}
<span className="text-xs truncate">{isGeneration || isEmbedding ? model : spanName}</span>
{typeof latency === 'number' && (
<LemonTag type="muted" size="small">
{latency.toFixed(2)}s
{event.properties.$ai_stream &&
typeof event.properties.$ai_time_to_first_token === 'number' && (
<span className="ml-1 opacity-75">
(TTFT:{' '}
{event.properties.$ai_time_to_first_token < 0.001
? `${(event.properties.$ai_time_to_first_token * 1000).toFixed(2)}ms`
: `${Math.round(event.properties.$ai_time_to_first_token * 1000)}ms`}
)
</span>
)}
</LemonTag>
)}
{costContext && (
<Tooltip
title={
hasCostBreakdown(costContext) ? (
<CostBreakdownTooltip costContext={costContext} />
) : (
'Total cost'
)
}
>
<LemonTag type="muted" size="small">
{formatLLMCost(costContext.totalCost)}
</LemonTag>
</Tooltip>
)}
{isGeneration && event.sentiment && (
<SentimentBar
label={event.sentiment.label}
score={event.sentiment.score}
messages={event.sentiment.messages}
/>
)}
</div>
</div>
{isExpanded && (
<div className="border-t">
<EventDetails event={eventForDetails} />
</div>
)}
</div>
)
}