Skip to content
NEWMeet Alaaddin, your AI genie with a voice. trig8n launches 1 January 2027. See what is new

Library / AI Agents & Assistants

AI Agents & Assistants

Run budget-capped client AI agents with OpenAI and n8n data tables

When an API call/webhook arrives, it runs an AI agent with OpenAI / ChatGPT using Data Table, SMTP Email, n8n API.

What it does

## Client-safe AI agent: monthly budget per client, pause switch and usage ledger For agencies and freelancers who run an AI agent for several paying clients and need each client's usage to stop at that client's budget. ### How it works 1. Your app POSTs { "client_id", "prompt" } to Client request. 2. The client's budget, pause switch and instructions are read from the ai_clients table, and this month's usage from ai_usage_ledger. 3. A worst-case amount is reserved before the model is called. The ledger is read again, and the run backs off if parallel runs took the last of the budget. 4. The AI Agent runs with Max Iterations and a token limit. 5. The token usage n8n recorded for each model call is booked on the run's row, and the answer is returned with a usage summary. 6. You get an email when a client crosses the alert threshold. If a table can't be read or written, the run is refused (fail closed). Also included: an hourly sweep of stuck reservations, a monthly summary with cleanup, and a usage endpoint. ### Setup steps 1. Create the two Data tables in the note below. 2. Credentials: OpenAI on Chat model, n8n API on Read token usage, Header Auth on both webhooks, SMTP on the three email nodes. 3. Fill in Settings, Sweep settings and Monthly settings. 4. Workflow settings: keep Save execution progress on. 5. Add a client row, publish, and send the test request in the note below.

The steps

  1. Client request
  2. Settings
  3. Validate request
  4. Valid request?
  5. Reject: bad request
  6. Look up client
  7. Load month ledger
  8. Check budget
  9. Allowed to run?
  10. Reject: unknown, paused or over budget
  11. Reject: budget check unavailable
  12. Reserve budget
  13. Re-read month ledger
  14. Confirm reservation

44 steps in total.

Similar automations