Omnibus
Skill 63 of 200
Investigate LLM spend in PostHog — total cost over time, cost by model, provider, user, trace, or custom dimension, token and cache-hit economics, and cost regressions.
5 minutes · 1,188 words · 12 sections
Install
npx skills add PostHog/skills --skill exploring-llm-costsnpx skills add PostHog/skills/plugin marketplace add PostHog/skillsThe first command installs just this skill, by the name in its SKILL.md; the second installs the whole repository.
PostHog attaches per-call cost metadata to every $ai_generation and $ai_embedding
event at ingestion time. Every cost question reduces to an aggregation over those
two event types — the interesting variation is only in how you group, filter, and
compare.
This skill covers the common cost investigations: total spend, breakdowns (model, provider, user, trace, custom property), token and cache-hit analysis, regression debugging, and materializing results as insights, dashboards, or alerts.
| Tool | Purpose |
|---|---|
posthog:query-trends | Native cost trends with supported sums, breakdowns, and comparisons |
posthog:execute-sql | Ad-hoc HogQL for any cost aggregation — the workhorse of this skill |
posthog:query-llm-traces-list | List traces with rolled-up cost, token, and error metrics |
posthog:query-llm-trace | Cost breakdown of a single trace across all its events |
posthog:read-data-schema | Discover which custom properties exist for breakdowns |
posthog:insight-create | Materialize a cost chart as a saved insight |
posthog:dashboard-create | Bundle cost insights into a dashboard |
posthog:alert-create | Alert when cost crosses a threshold |
posthog:generate-app-url | Build region- and project-qualified links back to the UI |
Three rules cover most of what goes wrong:
$ai_total_cost_usd for rollups, never the components. Components drop
request and web-search fees. The UI’s cost cells sum $ai_total_cost_usd
over event IN ('$ai_generation', '$ai_embedding'); mirror that. Full
schema and rationale in cost properties (opens in a new tab).$ai_generation and $ai_embedding in cost queries
unless the project demonstrably does not use embeddings — missing them silently
under-counts. $ai_trace and $ai_span carry no rollup cost; some SDK
wrappers duplicate $ai_total_cost_usd onto $ai_trace so don’t include
it in rollups or you’ll double-count.$ai_total_cost_usd is set at ingestion via one of three paths (passthrough,
custom pricing, automatic lookup). When a cost looks wrong, read
$ai_cost_model_source first — see cost sources (opens in a new tab)
for the precedence rules and a diagnostic query.
Cache-hit math depends on whether the provider reports cache tokens inclusively
or exclusively of $ai_input_tokens. Always branch on the per-event
$ai_cache_reporting_exclusive flag, never on provider name — see
cache accounting (opens in a new tab) for the exclusive-vs-inclusive
formula.
distinct_id is the canonical user dimension. Customers often attach custom
properties (feature, tenant_id, workflow_name) — discover them with
posthog:read-data-schema before grouping. Don’t guess names.
posthog:execute-sql
SELECT round(sum(toFloat(properties.$ai_total_cost_usd)), 4) AS total_cost_usd
FROM events
WHERE event IN ('$ai_generation', '$ai_embedding')
AND timestamp >= now() - INTERVAL 30 DAYEvery cost question is a variation of the same template — group by a dimension,
aggregate $ai_total_cost_usd. See breakdown patterns (opens in a new tab)
for ready-to-run recipes:
When the user pastes a trace URL and asks about its cost, fetch the trace and surface the per-event breakdown:
posthog:query-llm-trace
{ "traceId": "<trace_id>", "dateRange": {"date_from": "-30d"} }Sum $ai_total_cost_usd across the returned events, grouped by span name or
model, to show which step(s) drove the cost. The trace response already
includes totalCost as a convenience.
“Our LLM bill jumped — why?” is almost always one of: more calls, bigger prompts, a new model, or a change in cache-hit rate. Work through them in order — see regression debugging (opens in a new tab) for the 5-step playbook.
After ad-hoc queries answer the question, persist them as insights, bundle
into a dashboard, or wire up alerts. See materializing (opens in a new tab)
for ready-to-run JSON for posthog:insight-create, posthog:dashboard-create,
and posthog:alert-create.
Never hand-write https://app.posthog.com/... links. That host drops the region and the
project prefix, so the user is redirected to login instead of the page you meant.
query-llm-traces-list and query-llm-trace
return _posthogUrl — surface that value. For a single trace, append
?timestamp=<url_encoded_iso> (the trace’s earliest event time) to that URL; the returned link
carries no timestamp, and without one the trace page scans from a fixed early date instead of
the ten-minute window around the trace.generate-app-url. It resolves the correct region host and
/project/<id>/ prefix (e.g. https://us.posthog.com/project/2/ai-observability/traces). Pass
concrete ids via params, never inline them into the path.
generate-app-url {url: "/ai-observability/dashboard"}generate-app-url {url: "/ai-observability/traces"}generate-app-url {url: "/ai-observability/generations"}generate-app-url {url: "/ai-observability/users"}generate-app-url {url: "/ai-observability/traces/{id}", params: {id: "<trace_id>"}}generate-app-url cannot express query params, so append the ?timestamp=<url_encoded_iso>
described above to a single-trace link yourself.
Always surface a UI link so the user can verify visually.
Provider reporting behavior (which tokens are inclusive vs exclusive, which costs show up where) shifts over time and can differ between SDK versions for the same provider. To avoid rot:
$ai_cache_reporting_exclusive,
$ai_cost_model_source) rather than hardcoded provider or model names.
When set, those flags are ingestion’s resolved answer for the specific
event. $ai_cache_reporting_exclusive is often missing; see
cache accounting (opens in a new tab) for the fallback.$ai_total_cost_usd is always authoritative for rollups — prefer it
over summing components, which can drift as new cost categories are
added.posthog:docs-search for
“calculating costs” or “AI observability” first rather than trusting a
hardcoded rule in this file.$ai_trace_id properties are on events — but message content ($ai_input / $ai_output_choices) lives only on the posthog.ai_events table; load the exploring-llm-traces skill if you need content alongside cost$ai_embedding alongside $ai_generation when summing cost; embeddings are cheap per-call but add up at scale$ai_total_cost_usd is missing or zero, read $ai_cost_model_source first: passthrough means the SDK supplied costs; custom means custom token prices; openrouter / manual mean automatic lookup; missing means the model wasn’t matched (unusual custom model, fine-tune). Grep: countIf(properties.$ai_total_cost_usd IS NULL) per (model, source)distinct_id = properties.$ai_trace_id — some SDKs default distinct_id to the trace ID when no user is set$ai_generation + $ai_embedding events within a trace; summing on $ai_span gives zero. $ai_trace may carry $ai_total_cost_usd from some SDK wrappers — don’t include it in rollups or you’ll double-count. $ai_evaluation events also carry cost but are not part of the stock UI rollups; include them only when the user explicitly wants evaluation spend in the total$ai_cache_reporting_exclusive — resolve it per event, never per group. Most events with a precomputed cost carry no flag, so never default a missing flag to inclusive; use the fallback in cache accounting (opens in a new tab)/ai-observability/dashboard tiles already answer the question — re-creating them is churninsight-query; ad-hoc SQL is fine for one-offs but re-running it on every dashboard load is expensiveanalyzing-expensive-users — who drives the spend, and whether their usage pattern explains itexploring-llm-traces — inspect the expensive traces the breakdowns point atInvestigate LLM spend in PostHog — total cost over time, cost by model, provider, user, trace, or custom dimension, token and cache-hit economics, and cost regressions. Use when the user asks "how much are we spending on LLMs?", "which model / user / feature is most expensive?", "why did cost spike?", wants to build a cost dashboard or alert, or pastes a trace URL and asks about its cost.
The verbatim description from this skill’s front matter — the string an agent matches on to decide whether to load it.
main, last pushed 8 November 2026.SKILL.md, not by matching a directory convention. 2 distinct layouts observed: skills/omnibus/*/SKILL.md, skills/posthog/all/skills/*/SKILL.md.h1 and no skipped levels:.claude-plugin/marketplace.json by PostHog, declaring 6 plugins. It is read for editorial metadata only — never as the skill index, which is always the repository tree./PostHog/skills.md, and each skill at its own .md URL.6 files · 25 KB
Everything this skill ships beside its prose. All of it is set here, as subchapters of skill 63.
Documentation the agent loads on demand, rather than up front.