Back to Blog
Integration

How to Cap OpenAI Spend in an n8n Workflow

Can You Set a Hard Spend Cap in n8n?

No — n8n has no spending cap. There is no setting, anywhere in n8n Cloud or self-hosted n8n, that stops a workflow once it has spent a certain number of dollars on AI calls. n8n meters its own executions, not what your AI provider charges you for them. The only way to enforce a real dollar ceiling is to put something between n8n and the provider that can refuse a call before it is forwarded.

That is what a gateway does. You point your AI nodes at a gateway endpoint instead of at OpenAI directly, set a budget in dollars, and the gateway checks accumulated spend against that budget before forwarding each call. Once spend has reached the budget, the next call is refused — it never reaches OpenAI, so it never appears on your OpenAI invoice. With TokenSense that takes about five minutes and no code — the rest of this guide walks through it.

Interactive Demo

Workspace Budget Guardrail

Monthly AI Spend$45
Budget Cap$100
80%
Cap
System Operational
AI requests are flowing through your TokenSense gateway.
Set a dollar cap. Requests are warned on as you approach it, and refused once accumulated spend has reached it.

Why "Limit" Does Not Mean What You Think in n8n

Search for how to limit an n8n workflow and most of what comes back is about something else entirely. n8n has a node literally called Limit — it caps how many items pass through a step, not how much money they cost. Separately, n8n's docs cover handling OpenAI rate limits, which is about requests per minute, not dollars. Neither has anything to do with spend.

The three things get conflated constantly, so it is worth being precise. An item limit controls how many records a step processes. A rate limit controls how fast you may call the provider. A spend cap controls how much money the workflow is allowed to cost. You can hit an item limit and a rate limit and still run up an unbounded bill, because nothing in that chain is counting dollars.

If you are searching for this, use the words spend or cap rather than limit — you will find far more of what you actually want.

What OpenAI’s Own Spend Limits Do and Do Not Do

OpenAI does let you set monthly usage limits on your organisation, and they are worth setting as a last line of defence. But they solve a different problem from the one an automation team has. They apply to the whole organisation, so they cannot distinguish one workflow from another, one client from another, or a production automation from an experiment. When the org limit trips, everything stops at once — including the workflows that were behaving.

They are also coarse in time. Usage is tallied on OpenAI's side and enforcement follows the tally, so a burst of calls inside a short window can land before the limit registers. For a workflow on a schedule trigger that is fine. For an AI Agent in a retry loop at three in the morning, the gap between spending the money and being told about it is exactly where the surprise bill comes from.

And they are one number for one provider. If your workflows also call Anthropic, Gemini, xAI, Mistral, or Kimi, you have a separate limit in a separate dashboard for each, with no combined view and no shared ceiling.

Setting a Real Cap With TokenSense

Four steps, about five minutes, and nothing in your workflow logic changes. You are only changing where the AI calls are routed.

Interactive walkthrough

Set up the TokenSense node in n8n

Step 1 of 4In n8n, open Settings → Community Nodes and install the package n8n-nodes-tokensense.

npm packagen8n-nodes-tokensense

Self-hosted today. The verified node is rolling out to n8n Cloud with n8n’s next release.

Click through the four steps: install the node, add the credential, set your endpoint and key, then swap the model node.

1. Add your provider key. In the TokenSense dashboard, go to Settings → Providers and paste your OpenAI key. TokenSense forwards calls on your behalf; keys are encrypted at rest and never exposed inside your workflow.

2. Point n8n at TokenSense. Drop the TokenSense Chat Model sub-node into your AI node's model slot, or set your existing OpenAI node's Base URL to https://api.tokensense.io/v1 and use your TokenSense key. On self-hosted n8n, install the verified n8n-nodes-tokensense community node first, under Settings → Community Nodes.

3. Set the cap. Set a workspace budget for a total ceiling across everything, or a per-project budget to cap one client or one group of workflows independently. Per-project budgets are available on the Pro and Agency plans.

4. Prove it works. Run the workflow once — the call shows up in Logs within seconds with its exact cost, model, and originating workflow. Then set a deliberately tiny budget, say ten cents, and run it again. The next call is refused and nothing is billed.

The full reference is in the budgets documentation and the n8n setup guide.

What Happens When the Cap Is Hit

TokenSense returns HTTP 402 Payment Required, and the request stops there. It is never forwarded to OpenAI, so no tokens are consumed and nothing lands on your provider invoice. The node fails, the execution stops or takes your error branch, and your TokenSense logs show precisely which workflow tried to spend past its ceiling.

The status code matters more than it sounds. 402 means you hit a spending ceiling you set. 429 means the provider is throttling you and the same request will probably succeed if you wait. Some gateways return 429 for a budget event, which collapses those two into one signal — and a retry-on-429 branch will then hammer a wall it can never get past. TokenSense keeps them distinct: 402 for your budget, 429 for rate limits, and 503 when we genuinely could not verify your budget. A 503 blocks that request too, but retryably — nothing was forwarded, nothing was charged, and no billing decision was made. Retry it with backoff; never treat it as a budget block.

One practical caveat if you are branching on this inside n8n. n8n wraps failed HTTP responses in its own error object and replaces the response body with a generic per-status message, so the detail field TokenSense sends does not survive into your workflow. Branch on the status code, not on the error text. This catches people out with every gateway, not just ours — it is worth knowing before you build error handling around it.

Per-Client Caps for Agencies

One ceiling for the whole account is fine when you are running your own automations. It is not fine when you are running twenty workflows for eight clients, because the first client to run away with it stops everybody. Put each client in their own TokenSense project with its own budget, and the caps become independent: when one client's budget is exhausted their workflows stop at a 402, and every other client keeps running untouched.

That also gives you a number you can put in front of a client. Instead of an estimate, you have a defensible per-client cost line and a ceiling you have committed to in writing — which is a considerably easier conversation than explaining a spike after the fact.

There is more on that in our guide to multi-client AI management for agencies, and on diagnosing a spike that already happened in why your n8n AI bill spiked.

Set your first cap in about five minutes. The TokenSense Starter plan is free and includes 10,000 requests a month, full cost tracking, and budget enforcement.