Measure what your n8n AI workflows cost: arithmetic, a ledger pattern, and a free calculator
Description and use
A method to attribute AI model costs to individual n8n workflows using token-count arithmetic from API usage objects, a nine-line JavaScript ledger Code node that records each AI call's model, prompt tokens, completion tokens, and USD cost per row, and a free browser calculator that estimates per-run, monthly, and yearly costs for each workflow plus a total and biggest spender.
Problem
Scheduled AI workflows run silently with no per-workflow cost attribution, making it impossible to identify which workflow is driving the monthly bill.
Outcome
A method to attribute AI model costs to individual n8n workflows using token-count arithmetic from API usage objects, a nine-line JavaScript ledger Code node that records each AI call's model, prompt tokens, completion tokens, and USD cost per row, and a free browser calculator that estimates per-run, monthly, and yearly costs for each workflow plus a total and biggest spender.
Workflow
- Split spend into build-time (in-app assistant subscription) and runtime (AI nodes in scheduled/webhook workflows)
- Compute per-run cost as (prompt tokens x price in + completion tokens x price out) / 1,000,000
- Compute monthly cost as runs per day x 30 x cost per run
- Add a Code node after each AI step that reads item.json.usage for prompt_tokens, completion_tokens, and model, then pushes a ledger row with workflow name, model, token counts, and cost_usd
- Feed ledger rows into Google Sheets or Postgres, or keep a running total in workflow staticData via getWorkflowStaticData('global')
- Use the free n8n AI workflow cost calculator at https://vlotstroom.github.io/n8n-cost-calculator/ to estimate totals before building the ledger
- Review ledger output to identify the biggest spender and right-size models per step
Evidence
Scheduled workflows are the quiet ones. Nobody clicks anything, so nobody watches them, and every day they make a few model calls with prices attached.
(prompt tokens x price in + completion tokens x price out) / 1,000,000
cost per month = runs per day x 30 x cost per run
GPT-4o: $2.50 in, $10.00 out; GPT-4o mini: $0.15 in, $0.60 out; Claude Sonnet: $3.00 in, $15.00 out; Gemini Flash: $0.10 in, $0.40 out
On OpenAI-compatible APIs, the chat completion includes a usage object with prompt_tokens, completion_tokens and total_tokens (this is in OpenAI's published API spec, and the other vendors mirror the shape).
- Record date: 2026-10-21T05:56:07.419Z