: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.
Fixing invalid_ai_token_property
An AI event ($ai_generation, $ai_span, …) carried a token property that wasn't a valid number — $ai_input_tokens, $ai_output_tokens, or a similar count sent as a string, object, or malformed value.
The event was ingested but the property was nulled.
Category event, severity warning: token usage and cost analytics silently lose that data point.
What it means in your code
Token counts must arrive as plain numbers. The usual sources of bad values:
- string formatting:
"1204","1,204","1204 tokens", - passing the provider's usage object instead of a field (
usageinstead ofusage.total_tokens), NaN/undefinedfrom a missing usage block on streamed or failed responses.
Diagnose
- Query the warnings with
posthog:execute-sql:SELECT timestamp, details FROM system.ingestion_warnings WHERE type = 'invalid_ai_token_property' AND timestamp > now() - INTERVAL 7 DAY ORDER BY timestamp DESC LIMIT 20. ThedetailsJSON names the exactproperty, the receivedvalue, and itsvalueType— that's usually the whole diagnosis. - Find where the LLM provider's usage data is mapped onto the
$ai_*properties (manual capture or instrumentation wrapper) and check the types.
Fix
Send numbers:
posthog.capture({
distinctId,
event: '$ai_generation',
properties: {
$ai_input_tokens: response.usage.prompt_tokens, // number
$ai_output_tokens: response.usage.completion_tokens, // number
},
})
Guard the missing-usage case (streamed/failed responses): omit the property rather than sending undefined or a placeholder. Prefer PostHog's LLM analytics SDK integrations over hand-mapping usage fields — they handle provider differences.
Verify
Re-run a generation, re-query system.ingestion_warnings with posthog:execute-sql (filter type = 'invalid_ai_token_property', timestamp after your fix) — no new occurrences — and confirm token counts and costs appear for new traces (posthog:query-llm-traces-list).