: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
fix(llma): point AI trace tools to ai_events for input/output content (#64849)
Carlos Marchal2013d113cdae
diff --git a/ee/hogai/tools/execute_sql/prompts.py b/ee/hogai/tools/execute_sql/prompts.pyindex d8edddc27928..168f57a27549 100644--- a/ee/hogai/tools/execute_sql/prompts.py+++ b/ee/hogai/tools/execute_sql/prompts.py@@ -25,6 +25,22 @@ Use this tool to generate a HogQL query, which is PostHog's variant of SQL that# Events and propertiesStandardized events/properties such as pageview or screen start with `$`. Custom events/properties start with any other character.+# LLM / AI observability events+For LLM events ($ai_generation, $ai_trace, $ai_span, $ai_embedding, etc.) the heavy content keys are NOT stored on `events.properties` — they live as native columns on a dedicated table, referenced as `posthog.ai_events` (a bare `FROM ai_events` errors with "Unknown table"):++| `events` property | `posthog.ai_events` column |+| -------------------- | -------------------------- |+| $ai_input | input |+| $ai_output | output |+| $ai_output_choices | output_choices |+| $ai_input_state | input_state |+| $ai_output_state | output_state |+| $ai_tools | tools |++Metadata (token counts, costs, model, $ai_trace_id, latency, error flags) stays on `events` and is safe to query there. `posthog.ai_events` is `ORDER BY (team_id, trace_id, timestamp)`, so anchor on `trace_id`, never scan by timestamp. Rows are dropped after the retention period (30 days by default), so an empty result means the content aged out, not that nothing happened.+- Single trace: `SELECT input, output_choices FROM posthog.ai_events WHERE trace_id = '<id>' ORDER BY timestamp`+- Batch: filter the timestamp-indexed `events` table for trace IDs first, then fetch content from `posthog.ai_events` anchored on `trace_id`.+# Linked tables`virtual_table` and `lazy_table` fields are connections to linked tables, e.g. the virtual table field `person` allows accessing person properties like so: `person.properties.foo`.diff --git a/ee/hogai/tools/search_traces.py b/ee/hogai/tools/search_traces.pyindex 3a0206611371..910184177f86 100644--- a/ee/hogai/tools/search_traces.py+++ b/ee/hogai/tools/search_traces.py@@ -27,6 +27,14 @@ Search for LLM traces based on criteria like date range, model, error status, anA formatted list of matching traces with their name, timestamp, latency, cost, token counts, and error count.If more results are available, a cursor will be provided for pagination.All timestamps are in UTC timezone.++# What this tool does NOT return:+This is metadata only. It does NOT include prompt/response content ($ai_input, $ai_output, $ai_output_choices, $ai_tools).+That content is retained for 30 days by default.++# Do not infer bugs or "no problems" from this list alone:+- To inspect what actually happened in a trace (the prompt, the model output, the tools called), use `read_data` with kind="llm_trace" and the trace's ID from this list.+- A trace older than the 30-day retention window still shows metadata here but has no content left to read — that is aged-out data, not evidence that nothing went wrong.""".strip()SEARCH_TRACES_QUERY_DESCRIPTION = """@@ -129,6 +137,12 @@ class SearchLLMTracesTool(MaxTool):for i, trace in enumerate(results, 1):content += self._format_trace(i, trace)+ content += (+ "\nThis list is metadata only — prompt/response content ($ai_input, $ai_output, $ai_output_choices) "+ 'is not included. To inspect a trace\'s content, read it with read_data (kind="llm_trace") using the '+ "trace's ID above (content is retained 30 days; older traces show metadata but no content)."+ )+if has_more and next_cursor:content += f'\nMore traces are available. Pass cursor="{next_cursor}" to see the next page.'