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/cache-accounting.md

Cache token accounting (exclusive vs inclusive)

Providers report cache tokens two ways, and the cache-hit-rate math changes accordingly:

  • Exclusive — $ai_input_tokens does not include cache tokens. Total input volume is input_tokens + cache_read + cache_creation. Anthropic currently reports this way on most SDKs.
  • Inclusive — $ai_input_tokens already includes cache tokens. OpenAI and most others currently report this way.

Don't hardcode provider behavior — it varies by SDK and by SDK version, and providers can change their own reporting style over time. Instead, trust the per-event flag: ingestion auto-detects and writes the resolved value to $ai_cache_reporting_exclusive (boolean) on every $ai_generation. Callers can also override with $ai_cache_reporting_exclusive: true|false when manually capturing.

Cache-hit rate, branching on the per-event flag

posthog:execute-sql
SELECT
    properties.$ai_model AS model,
    if(properties.$ai_cache_reporting_exclusive = 'true',
       sum(toInt(properties.$ai_cache_read_input_tokens))
         / nullIf(sum(toInt(properties.$ai_input_tokens))
                + sum(toInt(properties.$ai_cache_read_input_tokens))
                + sum(toInt(properties.$ai_cache_creation_input_tokens)), 0),
       sum(toInt(properties.$ai_cache_read_input_tokens))
         / nullIf(sum(toInt(properties.$ai_input_tokens)), 0)
    ) AS cache_hit_rate
FROM events
WHERE event = '$ai_generation'
    AND timestamp >= now() - INTERVAL 30 DAY
GROUP BY model, properties.$ai_cache_reporting_exclusive

The same provider-aware if(...) formula is what powers cache_hit_rate in the breakdown patterns "input vs output vs cache economics" recipe.