The Cost Ledger: How I Gave Every n8n Workflow Run a Price Tag
Описание и применение
Создана система учёта стоимости вызовов LLM в n8n, позволяющая ежедневно агрегировать расходы по каждому workflow и оповещать о резких ростах затрат.
Задача
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