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.

fix(capture): divert only allowlisted AI events to the AI topic (#75970)

Paweł Ledwońaf39b055be9c
diff --git a/rust/capture/src/v0_request.rs b/rust/capture/src/v0_request.rs
index 80d856d97ddc..75bbc0bdb388 100644
--- a/rust/capture/src/v0_request.rs
+++ b/rust/capture/src/v0_request.rs
@@ -184,6 +184,32 @@ pub enum DataType {
AiEvents,
}
+/// Event names diverted to the dedicated AI lane. Must stay in sync with the
+/// ingestion AI subpipeline's allowlist (`AI_EVENT_TYPES` in
+/// `nodejs/src/ingestion/common/subpipelines/ai-event-types.ts`), which DLQs
+/// anything it receives that isn't on the list. Matching on the `$ai_` prefix
+/// instead would divert prefixed-but-unlisted names (e.g. `$ai_call`) into the
+/// AI topic only for the ingestion pipeline to DLQ them.
+pub const AI_EVENT_NAMES: &[&str] = &[
+ "$ai_generation",
+ "$ai_embedding",
+ "$ai_evaluation",
+ "$ai_span",
+ "$ai_trace",
+ "$ai_metric",
+ "$ai_feedback",
+ "$ai_tag",
+ "$ai_generation_summary",
+ "$ai_trace_summary",
+ "$ai_evaluation_report",
+];
+
+/// Whether an event name is diverted to the dedicated AI lane. See
+/// [`AI_EVENT_NAMES`].
+pub fn is_ai_event(name: &str) -> bool {
+ AI_EVENT_NAMES.contains(&name)
+}
+
impl DataType {
/// Classify an event by its name (and historical-migration flag) into a
/// `DataType`. Used by both v0's `process_single_event` and v1's
@@ -191,10 +217,11 @@ impl DataType {
/// ingestion-warning split stays in one place.
///
/// `route_ai_events` reflects the per-batch `AiRouting` decision,
- /// mirroring v1's `destination_for_event_name`: when set, `$ai_*` events
- /// divert to `AiEvents`, winning over historical (in v1 the historical
- /// reroute only applies to the analytics-main destination). When unset
- /// the mapping is a strict no-op relative to the pre-AI-lane behavior.
+ /// mirroring v1's `destination_for_event_name`: when set, AI events (per
+ /// [`is_ai_event`]) divert to `AiEvents`, winning over historical (in v1
+ /// the historical reroute only applies to the analytics-main destination).
+ /// When unset the mapping is a strict no-op relative to the pre-AI-lane
+ /// behavior.
///
/// `SnapshotMain` is not produced here — replay events arrive on a
/// separate endpoint and never flow through analytics processing.
@@ -207,7 +234,7 @@ impl DataType {
"$$client_ingestion_warning" => Self::ClientIngestionWarning,
"$exception" => Self::ExceptionErrorTracking,
"$$heatmap" => Self::HeatmapMain,
- _ if route_ai_events && event_name.starts_with("$ai_") => Self::AiEvents,
+ _ if route_ai_events && is_ai_event(event_name) => Self::AiEvents,
_ if historical_migration => Self::AnalyticsHistorical,
_ => Self::AnalyticsMain,
}
@@ -337,13 +364,20 @@ mod tests {
#[case("custom_event", false, true, DataType::AnalyticsMain)]
#[case("$pageview", true, false, DataType::AnalyticsHistorical)]
#[case("$pageview", true, true, DataType::AnalyticsHistorical)]
- // $ai_* diverts only when AI routing is enabled, and wins over historical.
+ // Allowlisted AI events divert only when AI routing is enabled, and win over historical.
#[case("$ai_generation", false, true, DataType::AiEvents)]
#[case("$ai_span", false, true, DataType::AiEvents)]
#[case("$ai_trace", false, true, DataType::AiEvents)]
+ #[case("$ai_generation_summary", false, true, DataType::AiEvents)]
#[case("$ai_generation", true, true, DataType::AiEvents)]
#[case("$ai_generation", false, false, DataType::AnalyticsMain)]
#[case("$ai_generation", true, false, DataType::AnalyticsHistorical)]
+ // Names matching the $ai_ prefix but absent from the allowlist do NOT divert:
+ // the ingestion AI pipeline would DLQ them, so they stay on the main lane.
+ #[case("$ai_call", false, true, DataType::AnalyticsMain)]
+ #[case("$ai_generation_enriched", false, true, DataType::AnalyticsMain)]
+ #[case("$ai_model_failover", false, true, DataType::AnalyticsMain)]
+ #[case("$ai_model_failover", true, true, DataType::AnalyticsHistorical)]
fn from_event_name_mapping(
#[case] event_name: &str,
#[case] historical_migration: bool,
diff --git a/rust/capture/src/v1/analytics/process.rs b/rust/capture/src/v1/analytics/process.rs
index 111074ad5616..9a314c72cd0e 100644
--- a/rust/capture/src/v1/analytics/process.rs
+++ b/rust/capture/src/v1/analytics/process.rs
@@ -19,6 +19,7 @@ use super::response::BatchResponse;
use super::types::{Batch, Event, EventResult, Options, WrappedEvent};
use crate::event_restrictions::{EventContext, EventRestrictionService};
use crate::global_rate_limiter::{GlobalRateLimitKey, GlobalRateLimiter};
+use crate::v0_request::is_ai_event;
use limiters::overflow::{OverflowLimiter, OverflowLimiterResult};
use tracing::Level;
@@ -43,16 +44,16 @@ use common_ingestion_warnings::{
/// in Kafka headers — removing that fallback would break scroll-depth heatmaps for v1.
///
/// When `route_ai_events` is set (this batch's token routes to the AI topic
-/// per the configured `AiRouting` policy: mode plus token allowlist), `$ai_*`
-/// events are diverted to `Destination::AiEvents`; otherwise they fall through
-/// to `AnalyticsMain` exactly as before, so an unconfigured AI topic or
-/// `primary` mode is a strict no-op.
+/// per the configured `AiRouting` policy: mode plus token allowlist), AI events
+/// (per [`is_ai_event`]) are diverted to `Destination::AiEvents`; otherwise they
+/// fall through to `AnalyticsMain` exactly as before, so an unconfigured AI
+/// topic or `primary` mode is a strict no-op.
fn destination_for_event_name(name: &str, route_ai_events: bool) -> Destination {
match name {
"$exception" => Destination::ExceptionErrorTracking,
"$$heatmap" => Destination::HeatmapMain,
"$$client_ingestion_warning" => Destination::ClientIngestionWarning,
- _ if route_ai_events && name.starts_with("$ai_") => Destination::AiEvents,
+ _ if route_ai_events && is_ai_event(name) => Destination::AiEvents,
_ => Destination::AnalyticsMain,
}
}
@@ -2020,11 +2021,17 @@ mod tests {
#[case("$pageview", true, Destination::AnalyticsMain)]
#[case("custom_event", false, Destination::AnalyticsMain)]
#[case("$autocapture", false, Destination::AnalyticsMain)]
- // $ai_* diverts only when AI routing is enabled; otherwise stays on Main.
+ // Allowlisted AI events divert only when AI routing is enabled; otherwise stay on Main.
#[case("$ai_generation", true, Destination::AiEvents)]
#[case("$ai_span", true, Destination::AiEvents)]
#[case("$ai_trace", true, Destination::AiEvents)]
+ #[case("$ai_generation_summary", true, Destination::AiEvents)]
#[case("$ai_generation", false, Destination::AnalyticsMain)]
+ // $ai_ prefixed names absent from the allowlist stay on Main so the
+ // ingestion AI pipeline doesn't DLQ them.
+ #[case("$ai_call", true, Destination::AnalyticsMain)]
+ #[case("$ai_generation_enriched", true, Destination::AnalyticsMain)]
+ #[case("$ai_model_failover", true, Destination::AnalyticsMain)]
fn destination_for_event_name_mapping(
#[case] event_name: &str,
#[case] route_ai_events: bool,