AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

exploring-llm-costs

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. 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.

Original source
posthog
License
MIT
Pinned revision
cac7c80a9e5b73484d18eda1bffb47297c938e0c

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read posthog--exploring-llm-costs at revision cac7c80a9e5b73484d18eda1bffb47297c938e0c, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 7

references/cost-sources.md

How costs get set: SDK, custom pricing, ingestion

Costs can arrive on the event in three ways; ingestion applies them in this precedence (see Calculating LLM costs for the authoritative rules):

  1. Pre-calculated — the SDK / manual capture sets $ai_input_cost_usd, $ai_output_cost_usd, $ai_request_cost_usd, $ai_web_search_cost_usd directly. Ingestion preserves them and fills $ai_total_cost_usd as the sum. Use when the caller already knows the cost.
  2. Custom pricing — the SDK sets $ai_input_token_price / $ai_output_token_price (required pair) plus optionally $ai_cache_read_token_price, $ai_cache_write_token_price, $ai_request_price, $ai_web_search_price. Ingestion multiplies by the token counts. Token prices are per token, not per million.
  3. Automatic model matching — ingestion looks up pricing by $ai_model + $ai_provider (OpenRouter first, manual fallback).

Three metadata properties tell you which path was taken — read them whenever a cost looks wrong:

| Property | Meaning | | ------------------------- | --------------------------------------------------------------------------- | | $ai_model_cost_used | Canonical model id the pricing lookup matched (may differ from $ai_model) | | $ai_cost_model_source | openrouter | manual | custom | passthrough | | $ai_cost_model_provider | Provider the lookup used |

Diagnostic: zero or null cost by model and source

When $ai_total_cost_usd is null or zero for a model, group by model and $ai_cost_model_source so you can see, per model, how many of its zero-cost calls came from each ingestion path. A model that has only source = NULL rows means ingestion never matched a pricing entry (fix: add custom pricing or correct $ai_model / $ai_provider); a model with source = 'custom' and zero cost is an explicitly-zero custom price (usually a misconfigured $ai_input_token_price / $ai_output_token_price). Without the source grouping the two look the same.

posthog:execute-sql
SELECT
    properties.$ai_model AS model,
    properties.$ai_cost_model_source AS source,
    count() AS calls,
    countIf(toFloat(properties.$ai_total_cost_usd) = 0 OR properties.$ai_total_cost_usd IS NULL) AS zero_cost_calls
FROM events
WHERE event = '$ai_generation'
    AND timestamp >= now() - INTERVAL 7 DAY
GROUP BY model, source
ORDER BY zero_cost_calls DESC