The Cost Ledger: How I Gave Every n8n Workflow Run a Price Tag
说明与用途
在 n8n 中实现了成本账本,记录每次 LLM 调用,按工作流每日聚合费用,并在成本激增时触发警报,帮助快速控制费用。
问题
n8n executions log token usage per run but lack aggregation, making it hard to see which workflow drives cost spikes.
结果
The ledger enabled real‑time cost attribution per workflow, allowing rapid detection of cost spikes and preventing unnoticed budget overruns.
工作流
- Insert a Code node after every AI node to log call details
- Store run_id, event_id, workflow, node, model, in_tokens, out_tokens, usd, timestamp in cost_log table
- Schedule a daily workflow to query cost_log, compute daily spend per workflow, compare to 7‑day median, and calculate spike_ratio
- Trigger Slack alert when spike_ratio >= 2.0
证据
n8n executions log token usage for some AI nodes, buried in execution data you have to open one run at a time. There's no aggregation: no "workflow X spent $Y this week," no per-node attribution, no trend.
The design is boring on purpose. Right after every node that calls a model – OpenAI node, Anthropic node, an HTTP call to an inference endpoint – sits a Cost node.
The alert rule is one line: spike_ratio >= 2.0 fires a Slack alert with the workflow name and yesterday's spend.
- 记录日期: 2026-10-14T22:56:00.396Z