exploring-llm-costs
它能做什么
本技能根据 PostHog 为每次模型调用记录的成本数据,回答关于 AI 模型使用成本的问题。适合管理 AI 预算、或想知道哪个模型或功能最昂贵的人。
适用场景
- ✓当你想了解团队随时间在 AI 模型上的支出时。
- ✓当成本突然上涨,你需要查明是哪个模型、用户或功能导致的时候。
- ✓当你想设置成本图表或提醒,以持续关注 AI 支出的时候。
技能链接
https://github.com/PostHog/posthog/blob/master/products/ai_observability/skills/exploring-llm-costs/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“上个月我们在 AI 模型上花了多少钱?哪个模型成本最高?”
每周 5 个新技能,1 分钟读完。
工作原理
它会汇总 PostHog 中每次 AI 模型调用的成本,并按你选择的维度分组,例如模型、提供商、用户或自定义属性。它可以比较不同时期来解释峰值,并将结果保存为图表或仪表盘项目。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Exploring LLM costs
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.
Tools
| 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 |
Core rules
Three rules cover most of what goes wrong:
- Sum
$ai_total_cost_usdfor rollups, never the components. Components drop request and web-search fees. The UI's cost cells sum$ai_total_cost_usdoverevent IN ('$ai_generation', '$ai_embedding'); mirror that. Full schema and rationale in cost properties. - Always include both
$ai_generationand$ai_embeddingin cost queries unless the project demonstrably does not use embeddings — missing them silently under-counts.$ai_traceand$ai_spancarry no rollup cost; some SDK wrappers duplicate$ai_total_cost_usdonto$ai_traceso don't include it in rollups or you'll double-count. - Always set a time range. Cost queries without one scan the full events table.
$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
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 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.
Workflow: total spend in a window
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 DAY
Workflow: cost breakdowns
Every cost question is a variation of the same template — group by a dimension,
aggregate $ai_total_cost_usd. See breakdown patterns
for ready-to-run recipes:
- Cost over time (daily)
- Cost by model
- Cost by user (top spenders)
- Cost by trace (top expensive traces)
- Cost by custom dimension
- Cost-per-call distribution
- Input vs output vs cache economics
Workflow: inspect a single trace's cost
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.
Workflow: debug a cost regression
"Our LLM bill jumped — w
常见问题
成本数字从哪里来?
PostHog 在记录每次 AI 调用时会附带成本信息,因此总计数字来源于这些数据。
它也能显示 token 用量吗?
可以。它可以同时查看 token 用量和缓存命中情况。
我可以把它放到仪表盘上吗?
可以。它可以将成本图表保存为洞察,并添加到仪表盘中。
详情
作者的原始描述
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.
repo: PostHog/posthog
热度
- GitHub 星标
- 41k
- 排名
- #6831
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
depot-ci
41k帮你的智能体设置并运行 Depot CI 工作流(GitHub Actions 的替代方案),包括迁移现有工作流和读取运行日志。
depot-container-builds
41k帮你的智能体在 Depot 的远程构建器上构建 Docker 镜像,包括多平台镜像、推送到镜像仓库,以及修复构建失败。
depot-github-runners
41k帮你的智能体把 GitHub Actions 任务迁移到 Depot 托管的运行器上,选择运行器规格和标签,并排查运行器设置问题。
editing-agents-md
41k帮你的智能体判断某条规则应写入 AGENTS.md 还是 CLAUDE.md,并以不会让文件臃肿的方式写下,确保规则真正生效。