> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Attribute AI cost to users and features – AI Observability pocket guide [](/pocket-guides.md)Aa [](/pocket-guides/ai-observability/invented-functions.md)[](/pocket-guides/ai-observability/throw-away-answers.md) # Attribute AI cost to users and features – AI Observability pocket guide If your AI bill is one mysterious number, you need to break it down. Cost attribution turns single number bills into detailed, feature-by-feature breakdowns. PostHog already [computes](/docs/ai-observability/calculating-costs.md) `$ai_total_cost_usd` on every [generation](/docs/ai-observability/generations.md), cache reads included. All it needs from you is dimensions to split by. ## Add properties You can take advantage of PostHog's properties to make sense of your AI bill: - Use a [custom property](/docs/ai-observability/custom-properties.md) to name specific features - Default properties like `$ai_model` and `$ai_provider` are included automatically - `distinct_id` tells you which customers your spend goes to Once you've added properties, your AI bill is no longer one line: Fig. 1 – three surfaces on the same account, two of them steady, one that surfaces the change. agent update Support summarizer flat at ~$40/day Docs search flat at ~$25/day Onboarding agent $35 → $190/day Fig. 1 – Three features on one bill. The total went up 60% and only one of them changed. ## Divide by requests A rising AI bill has two possible causes: more people used it, or each use got more expensive. Every call carries an `$ai_trace_id` that groups it with one interaction. Distinct [traces](/docs/ai-observability/traces.md) give you the denominator to divide by: - More traces means you're growing - More cost per trace means something regressed If your product is a multi-turn chat, `$ai_session_id` groups traces into a whole conversation, and cost per session is the number you should pay attention to. Input-to-output token ratio is worth the same treatment: - Output tokens rising means the model got chattier - Input tokens rising while output holds steady means your context is growing ## What the breakdown catches Four things account for most surprise bills, and each one has a different signature in the split: | What happened | How it shows up | | --- | --- | | A model swap | Cost steps up on one $ai_model, volume unchanged | | A prompt that grew | $ai_input_tokens climbs, output holds | | Caching stopped hitting | Cost per call rises while token counts stay flat | | A retry loop | Generations per trace climbs, cost per trace with it | Retry loops are the expensive ones, because it looks like usage. Every failed attempt bills, and if your code retries silently the only trace of the failure is the invoice. ## Build the dashboard Add all of this data into an [AI Observability dashboard](/docs/ai-observability/dashboard.md) you can open when you need to understand your AI spend. The following prompt will build that dashboard for you: Prompt for PostHog AI ```text Build me an AI cost dashboard from my $ai_generation events. First list the custom properties on those events and pick the one that identifies which feature made the call. Include daily total $ai_total_cost_usd; cost broken down by that feature property, by $ai_model, and by the top 20 distinct_ids; cost per trace using distinct $ai_trace_id as the denominator; and the input-to-output token ratio over time. Exclude events from dev and local environments. Then tell me which single breakdown accounts for the largest share of the last 7 days. If my project has no real $ai_generation traffic yet, build the full dashboard anyway and tell me how to instrument AI observability using the wizard or manual install. ``` [Build this with PostHog AI](https://app.posthog.com/#panel=max:!Build%20me%20an%20AI%20cost%20dashboard%20from%20my%20%24ai_generation%20events.%20First%20list%20the%20custom%20properties%20on%20those%20events%20and%20pick%20the%20one%20that%20identifies%20which%20feature%20made%20the%20call.%20Include%20daily%20total%20%24ai_total_cost_usd%3B%20cost%20broken%20down%20by%20that%20feature%20property%2C%20by%20%24ai_model%2C%20and%20by%20the%20top%2020%20distinct_ids%3B%20cost%20per%20trace%20using%20distinct%20%24ai_trace_id%20as%20the%20denominator%3B%20and%20the%20input-to-output%20token%20ratio%20over%20time.%20Exclude%20events%20from%20dev%20and%20local%20environments.%20Then%20tell%20me%20which%20single%20breakdown%20accounts%20for%20the%20largest%20share%20of%20the%20last%207%20days.%20If%20my%20project%20has%20no%20real%20%24ai_generation%20traffic%20yet%2C%20build%20the%20full%20dashboard%20anyway%20and%20tell%20me%20how%20to%20instrument%20AI%20observability%20using%20the%20wizard%20or%20manual%20install.)It builds the insights and the dashboard, then tells you which breakdown dominates. Needs AI Observability instrumented. ``` npx @posthog/wizard ai-observability ``` See also: [Tell an AI cost spike from real growth](/pocket-guides/self-driving/ai-spend-that-jumps.md) [‹ Catch a code assistant inventing functions](/pocket-guides/ai-observability/invented-functions.md)[All guides](/pocket-guides.md)p. 4 of 6[Measure whether users keep the answer ›](/pocket-guides/ai-observability/throw-away-answers.md) Attribute AI cost to users and features[Build this with PostHog AI](https://app.posthog.com/#panel=max:!Build%20me%20an%20AI%20cost%20dashboard%20from%20my%20%24ai_generation%20events.%20First%20list%20the%20custom%20properties%20on%20those%20events%20and%20pick%20the%20one%20that%20identifies%20which%20feature%20made%20the%20call.%20Include%20daily%20total%20%24ai_total_cost_usd%3B%20cost%20broken%20down%20by%20that%20feature%20property%2C%20by%20%24ai_model%2C%20and%20by%20the%20top%2020%20distinct_ids%3B%20cost%20per%20trace%20using%20distinct%20%24ai_trace_id%20as%20the%20denominator%3B%20and%20the%20input-to-output%20token%20ratio%20over%20time.%20Exclude%20events%20from%20dev%20and%20local%20environments.%20Then%20tell%20me%20which%20single%20breakdown%20accounts%20for%20the%20largest%20share%20of%20the%20last%207%20days.%20If%20my%20project%20has%20no%20real%20%24ai_generation%20traffic%20yet%2C%20build%20the%20full%20dashboard%20anyway%20and%20tell%20me%20how%20to%20instrument%20AI%20observability%20using%20the%20wizard%20or%20manual%20install.)