This query might return a very large blob of JSON data. You should either only include data you need in case it’s minimal or dump the results to a file and use bash commands to explore it.
This query must always have time ranges set. You can calculate the time range as -30 to +30 minutes from the source event.
The typical order of event capture for a trace is: $ai_span -> $ai_generation/$ai_embedding -> $ai_trace.
Explore -prefixed properties to find data related to traces, generations, embeddings, spans, feedback, and metric.
Key properties of the $ai_generation event: $ai_input and $ai_output_choices.
IMPORTANT: The $ai_input, $ai_input_state, and $ai_output_state properties can be extremely large (containing full conversation histories, system prompts, or application state). When your query selects these properties, you MUST dump the results to a file and use bash commands to explore the output. Never output them directly into the conversation.
This content lives only on posthog.ai_events (read it directly by trace_id), not on events.properties — see where heavy content lives.
sql
SELECT deduped.trace_id AS id, any(deduped.session_id) AS ai_session_id, min(deduped.timestamp) AS first_timestamp, max(deduped.timestamp) AS last_timestamp, ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(