Skip to main content
Lifetime license included with every purchase
n8n workflowsOpenAI costsAI automationbudget alerts

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.

Nn8n Marketplace Team·July 27, 2026·Updated July 27, 2026·7 min read

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

NodePurpose
OpenAIMake the call and return the token usage object
CodeCompute cost, attach the workflow name and model
Google SheetsLog every call; back the daily rollup and the running total
Schedule TriggerRun the daily spend digest and budget check
IFBranch when spend crosses the threshold
Slack / GmailSend the daily digest and the budget alert

Getting Started

  1. After each OpenAI node, add a Code node that reads usage and computes cost.
  2. Tag the row with $workflow.name and the model.
  3. Append every call to Google Sheets.
  4. Build a second workflow with a Schedule Trigger to roll up daily spend.
  5. Add an IF node and a budget threshold that fires a Slack and email alert.
  6. For batch workflows, add a pre-call circuit breaker that downgrades or skips when over the cap.
  7. Chart the trend monthly, then start from a template instead of rebuilding the cost math per workflow.
Browse AI ops templates

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.

Skip the build

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.

Get the AI Agent Spend Tracker
FAQ

Common questions

Why doesn't the OpenAI dashboard tell me which n8n workflow spent the money?
The OpenAI dashboard reports a single total per API key. It has no idea which of your workflows made which call, so a $400 bill arrives with no attribution. To know that your resume-screening workflow ate 70% of the spend, you tag each call inside n8n with the workflow name and log the token usage yourself. The dashboard gives you the total; n8n gives you the breakdown.
Where does the OpenAI node expose token usage in n8n?
The OpenAI node returns a usage object on the response with prompt and completion token counts. Read prompt_tokens and completion_tokens from the node output, multiply each by the model's per-token price, and you have the cost of that single call. A Code node does the math and writes the result, plus the workflow name and model, to your log.
Can n8n alert me before I blow the OpenAI budget?
Yes. Sum the day's logged cost in Google Sheets, and an IF node fires a Slack or email alert when it crosses your daily threshold. You can also hard-stop: if the running total exceeds the cap, route new calls to a No Op or a cheaper model instead of the expensive one. The alert is the early warning; the routing is the circuit breaker.
Stop reading. Start running.

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.

Free — $40 value

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