n8n OpenAI Cost Tracking: Know Which Workflow Spent the Money
Set up n8n OpenAI cost tracking to attribute spend per workflow, log tokens and cost to Google Sheets daily, and fire a budget-exceeded alert before the bill.
A $400 OpenAI Bill and No Idea Which Workflow Caused It
If you run more than one AI workflow, n8n OpenAI cost tracking is the difference between "the bill went up" and "the resume-screening workflow tripled its token use after last week's prompt change." The OpenAI dashboard can't tell you that. It reports one total per key and stays silent on which workflow, which model, or which run drove the spend.
There's no strong page ranking for this exact problem, which is odd, because it's one of the most common complaints from people running LLMs in production. The fix is straightforward: tag every call, log the tokens and cost, total it daily, and alert before the budget breaks.
n8n is the right place to do this because the spend originates here. Every OpenAI node knows its own workflow, its model, and its token count. You just have to capture it.
What You Can Automate in Cost Tracking
A spend-tracking layer turns an opaque bill into a daily report:
- Per-call logging: capture tokens, model, and workflow name on every OpenAI call
- Cost calculation: convert token counts to dollars using current per-model pricing
- Per-workflow attribution: see which automation spends what, not just the grand total
- Daily rollup: a scheduled summary of yesterday's spend by workflow and model
- Budget alerts: fire a warning when daily or monthly spend crosses a threshold
- Circuit breaker: route calls to a cheaper model or stop them when the cap is hit
- Trend log: a Google Sheets history you can chart to spot a creeping cost regression
The Cost Tracking Pipeline
[any workflow] → OpenAI node (returns usage: prompt/completion tokens)
→ Code (cost = tokens × per-model price; attach workflowName, model)
→ Google Sheets (append: date, workflow, model, tokens, cost)
Schedule Trigger (daily 23:30)
→ Google Sheets (read today's rows)
→ Code (sum cost by workflow + grand total)
→ IF (total > dailyBudget?) → Slack + Gmail (budget alert + top spender)
→ Slack (daily digest: spend by workflow)
1. Read token usage off the OpenAI node
The OpenAI node returns a usage object alongside the completion. Pull the counts straight from the response:
// Code node right after the OpenAI node
const u = $json.usage ?? {};
const promptTokens = u.prompt_tokens ?? 0;
const completionTokens = u.completion_tokens ?? 0;
// per-1K-token prices for the model you're calling (update to current pricing)
const inPer1k = 0.0025, outPer1k = 0.01;
const cost = (promptTokens / 1000) * inPer1k + (completionTokens / 1000) * outPer1k;
return [{ json: {
date: new Date().toISOString().slice(0, 10),
workflow: $workflow.name,
model: $json.model ?? 'gpt-4o',
promptTokens, completionTokens,
cost: Number(cost.toFixed(4)),
} }];
$workflow.name is the attribution the OpenAI dashboard can't give you. That one field is what turns a total into a breakdown.
2. Log every call
Append the cost row to Google Sheets. One row per call is fine; sheets handle tens of thousands of rows without complaint, and you'll roll them up daily anyway. The columns that matter: date, workflow, model, tokens, cost. With those you can answer almost any spend question after the fact.
3. Roll it up on a schedule
A separate Schedule Trigger workflow reads the day's rows near midnight, sums cost by workflow, and posts a digest. This is where the value shows up: a daily message that says "AI spend yesterday: $18.40, of which resume-screening was $12.90" makes a cost regression obvious the day it starts, not the day the bill arrives.
4. Alert before the budget breaks
An IF node compares the running total against your threshold and fires when it crosses. Name the top spender in the alert so the message is actionable, not just "you're over budget." In practice a daily cap catches runaway spend faster than a monthly one, because a prompt change that doubles token use shows up within 24 hours instead of three weeks later.
5. Add a circuit breaker for the expensive workflows
For the workflows that can spike, go further than alerting. Before the OpenAI call, read today's running total. If it's over the cap, route to a cheaper model or a No Op that skips the call. This trades some quality for a hard spending ceiling, which is the right trade for a background batch job even if it isn't for a customer-facing one.
Implementation Patterns
Pattern 1 — Tag at the source. Capture $workflow.name on every call, right after the OpenAI node, before the data flows anywhere else. Attribution added later is guesswork. The AI Agent Spend Tracker template wires this capture into a reusable sub-workflow so you tag once and reuse everywhere.
OpenAI → Code (cost + $workflow.name) → Google Sheets
Pattern 2 — Daily cap beats monthly cap. A daily threshold surfaces a cost regression inside a day. A monthly one lets it run for weeks. Set both, but make the daily one the alert you actually watch.
Schedule (daily) → sum today → IF (> dailyCap) → alert with top spender
Pattern 3 — Circuit-break the batch jobs. For non-interactive workflows, read the running total before the call and downgrade the model or skip when over budget. Keep customer-facing calls on the alert-only path.
n8n Nodes You'll Use Most
| Node | Purpose |
|---|---|
| OpenAI | Make the call and return the token usage object |
| Code | Compute cost, attach the workflow name and model |
| Google Sheets | Log every call; back the daily rollup and the running total |
| Schedule Trigger | Run the daily spend digest and budget check |
| IF | Branch when spend crosses the threshold |
| Slack / Gmail | Send the daily digest and the budget alert |
Getting Started
- After each OpenAI node, add a Code node that reads
usageand computes cost. - Tag the row with
$workflow.nameand the model. - Append every call to Google Sheets.
- Build a second workflow with a Schedule Trigger to roll up daily spend.
- Add an IF node and a budget threshold that fires a Slack and email alert.
- For batch workflows, add a pre-call circuit breaker that downgrades or skips when over the cap.
- Chart the trend monthly, then start from a template instead of rebuilding the cost math per workflow.
Cost tracking is the financial mirror of error tracking; the same logging-and-alert shape powers the n8n log alerting workflow. And if you're building the OpenAI calls these costs come from, the parsing and prompt tips in n8n OpenAI workflows keep token use predictable.
The AI Agent Spend Tracker ships this end-to-end: a reusable sub-workflow that reads the OpenAI usage object, computes per-model cost, tags it with the calling workflow, and logs to Google Sheets, plus the daily rollup and the budget-exceeded alert. It's part of The Complete n8n Templates Bundle, a one-time lifetime license to the whole catalog and every template added later, which makes sense the moment you run more than one AI workflow against the same key.
Common questions
Why doesn't the OpenAI dashboard tell me which n8n workflow spent the money?
Where does the OpenAI node expose token usage in n8n?
Can n8n alert me before I blow the OpenAI budget?
Get the workflow templates this guide is built on
Import-ready n8n JSON, step-by-step setup, and tested end-to-end. One-time payment, own it forever.
Get 3 tested n8n templates, free
The full customer package for three real catalog templates — workflow JSON, step-by-step setup guide, credential checklist. Built through the same live-instance release process as everything we sell. Plus new templates and automation guides in your inbox. No spam, unsubscribe anytime.
- 01Smart To-Do List ManagerPre-built n8n workflow template that automates productivity with OpenAI. Live in about 10 minutes.$14
- 02Email Follow-Up AutomatorPre-built n8n workflow template that automates crm with OpenAI. Live in about 15 minutes.$12
- 03Market Trend AnalyzerPre-built n8n workflow template that automates data processing with OpenAI. Live in about 10 minutes.$14
More automation guides

Automate SEO Internal Linking Across Your Site with n8n
Internal linking is one of the highest-ROI on-page SEO levers, and it's the one that rots fastest. Every new post should link to relevant older ones and earn links back, but nobody remembers the forty…

Build a Self-Filling Content Calendar with n8n
Most content calendars die the same way: the planning sheet looks great in January, then a busy week leaves three empty slots, then a busier week leaves ten, and by March nobody trusts it. The fix isn…

Automate Content Translation and Localization with n8n
A blog that ranks in English is leaving traffic on the table in five other markets. The fix sounds simple, translate the posts, and the popular n8n template does exactly the naive version: title in, b…