Downloads
Download the n8n workflow (JSON)
ai-governance-rebilling-n8n-claude.json · n8n workflow · 38 steps · starts with When Called by Another Workflow · uses Google Sheets, Slack, Guardrails, Anthropic, Gmail · 74 KB
How to import it into n8n
- In n8n, create a new workflow.
- Open the ⋯ menu at the top right and choose Import from File, then pick the downloaded .json. (Or open the file, copy everything and paste it on the canvas.)
- Open each node with a warning and connect your own account: the file carries no passwords or keys.
- Run it once with Test workflow, check the result, then switch it to Active.
What this workflow does
Add cost tracking, output checks and prompt-injection protection to every AI workflow you run, then bill each client for their AI usage. Your workflows call this one with an Execute Sub-workflow node. It prices every AI call, stops broken output before it reaches your CRM or customer, masks personal data and blocks prompt injection with Claude Haiku. Every morning it totals spend per client, warns before a budget runs out and on billing day emails finance a rebilling table with your markup.
- When Called by Another Workflow receives a request with a
guardfield (cost,outputorsecurity). Route by Guard sends it to the right section and unknown names get an error back. - Cost guard: Calculate AI Cost prices prompt and completion tokens from the table in Workflow Configuration, Log to Cost Ledger saves the call and Alert High AI Cost posts to Slack when one call is over your limit.
- Output guard: Validate Workflow Output checks for empty results, too few items, missing required fields and error flags. Broken output goes to the Dead Letter tab and Alert Silent Failure posts to Slack.
- Security guard: Mask Personal Data removes emails, phone numbers, card numbers, ID numbers and secret keys. Detect Prompt Injection checks your blocked phrases and asks Claude Haiku for a jailbreak score. Build Security Verdict returns allow, block or review, and risky input is logged and posted to Slack. If Claude fails, the input is held for review, never allowed.
- Daily Usage Check reads the Cost Ledger, totals this month and last month per client, warns in Slack at 80 percent of a budget and on billing day saves rows to the Rebilling tab and emails finance the table.
Who it is for
Agencies and automation freelancers who run AI workflows for clients and need to bill AI usage back, and in-house teams who want one place to watch AI spend, silent failures and risky input across many workflows.
What you need
- n8n (Cloud or self-hosted)
- Google Sheets
- Slack workspace
- Gmail account for the finance email
- Anthropic API key (Claude Haiku 5.5)
The workflow, step by step
Step 1 · Receive and route
Other workflows call this one with a guard name. The daily schedule starts the budget report. Unknown guard names get a clear error back.
- When Called by Another Workflow (executeWorkflowTrigger)
- Daily Usage Check (Schedule)
- Workflow Configuration (Set)
- Route by Guard (Switch)
- Return Unknown Guard Error (Set)
Step 2 · Cost guard
Prices each AI call, logs it to the Cost Ledger tab and alerts Slack when one call costs more than your limit.
- Calculate AI Cost (Code)
- Log to Cost Ledger (Google Sheets)
- Is Cost Above Limit? (IF)
- Alert High AI Cost (Slack)
- Join Cost Paths (merge)
- Return Cost Result (Set)
Step 3 · Output guard
Catches silent failures. Empty results, missing fields and error flags go to the Dead Letter tab and Slack.
- Validate Workflow Output (Code)
- Is Output Broken? (IF)
- Save to Dead Letter Sheet (Google Sheets)
- Alert Silent Failure (Slack)
- Join Output Paths (merge)
- Return Validation Result (Set)
Step 4 · Security guard
Masks personal data, checks for prompt injection with Claude Haiku and holds risky input for review.
- Mask Personal Data (guardrails)
- Detect Prompt Injection (guardrails)
- Claude Haiku Classifier (Claude model)
- Collect Security Verdict (merge)
- Build Security Verdict (Code)
- Is Input Risky? (IF)
- Log Security Incident (Google Sheets)
- Alert Security Team (Slack)
- Join Security Paths (merge)
- Return Security Result (Set)
Step 5 · Budget alerts and rebilling
Totals AI spend per client every morning, warns near the budget and emails finance the rebilling table on billing day.
- Read Cost Ledger (Google Sheets)
- Total Usage per Client (Code)
- Clients Near Budget (Filter)
- Clients to Bill Today (Filter)
- Alert Budget Warning (Slack)
- Save Rebilling Rows (Google Sheets)
- Build Rebilling Email (Set)
- Email Rebilling Summary (Gmail)
Step 6 · Try it with sample data
Sends five sample requests through the guards so you can see each result.
- Run Demo Checks (manualTrigger)
- Sample Guard Requests (Code)
- Call AutoGuard (executeWorkflow)
Setup
- Download the JSON below and import it in n8n (Create workflow > ⋯ > Import from File).
- Create a Google Sheet (for example "AutoGuard Ledger") with four tabs: Cost Ledger, Dead Letter, Security Log and Rebilling. Add the header row each Google Sheets node shows in its column list, then pick the sheet in every Google Sheets node.
- Add credentials: Google Sheets, Slack, Gmail and Anthropic. Pick your Slack channel in the four Slack nodes.
- Open Workflow Configuration and set the price table,
cost_alert_usd, client budgets and markups,billing_day,finance_emailand blocked phrases. - Click Run Demo Checks. Expect a Slack alert for the claude-opus-5-5 call ($0.84, over the $0.50 limit), a Dead Letter row for the broken order sync, and a blocked chat message with the email and phone masked.
- In your own workflow, add Execute Sub-workflow, choose this workflow and fill the inputs for the guard you need. Activate this workflow for the daily report.
How to customise
The sample prices, budgets, phrases and message texts are a starting point, not a fixed format.
- Workflow Configuration: prices, limits, budgets, markups, billing day, finance email, blocked phrases, jailbreak threshold
- Set block_when_over_cost_limit to true to return allowed: false for calls over the limit
- Mask Personal Data: add or remove personal data types and your own regex patterns
- Alert High AI Cost, Alert Silent Failure, Alert Security Team, Alert Budget Warning, Build Rebilling Email: change the text, channel or recipients
Why Claude
Claude Haiku 5.5 (claude-haiku-5-5) in Claude Haiku Classifier, used only by the jailbreak check in Detect Prompt Injection. The job is a short yes or no style score on one message, so a small fast model is the right fit: at $0.10 per million input tokens and $0.50 per million output tokens it costs a tiny fraction of a cent per check and answers in about a second. Claude models are trained to resist prompt injection, which matters for a classifier that reads attacker text. Everything else (cost math, output checks, masking, keyword matching) runs without an LLM, because money and rule checks must be exact. If Claude fails or has no credential, the verdict is "review", so a broken model never lets risky input through. You can swap the model sub-node for any chat model n8n supports.
Troubleshooting
- Credential errors after import: imported workflows never include credentials. Open each node marked red and select your own account.
- Nothing runs on its own: test runs only fire once. Activate the workflow so the trigger keeps running.
- Odd times or dates: set your timezone in Workflow Settings.
Want the members-only resources too?
A free account unlocks them all, including the ones shared in DMs.

