{"product_id":"budget-capped-ai-agents-in-n8n-openai-client-runs-via-webhook","title":"Budget-Capped AI Agents in n8n: OpenAI Client Runs via Webhook","description":"\u003ch3\u003eRun budget-capped AI agents for multiple clients in n8n—via a secure webhook\u003c\/h3\u003e\n\u003cp\u003eThis n8n automation lets your app trigger an OpenAI-powered AI agent for multiple paying clients while enforcing \u003cstrong\u003eper-client monthly token budgets\u003c\/strong\u003e, \u003cstrong\u003epause controls\u003c\/strong\u003e, and \u003cstrong\u003esafe concurrency\u003c\/strong\u003e using an AI usage ledger.\u003c\/p\u003e\n\n\u003ch3\u003eWhat this workflow does\u003c\/h3\u003e\n\u003cul\u003e\n  \u003cli\u003eYour app \u003cstrong\u003ePOSTs\u003c\/strong\u003e \u003ccode\u003eclient_id\u003c\/code\u003e and a \u003ccode\u003eprompt\u003c\/code\u003e to the workflow’s webhook.\u003c\/li\u003e\n  \u003cli\u003eIf the client is \u003cstrong\u003eunknown\u003c\/strong\u003e or \u003cstrong\u003epaused\u003c\/strong\u003e, the webhook returns \u003cstrong\u003e403\u003c\/strong\u003e. If the client has \u003cstrong\u003eno budget left\u003c\/strong\u003e, it returns \u003cstrong\u003e429\u003c\/strong\u003e.\u003c\/li\u003e\n  \u003cli\u003eTo prevent overspending, the workflow writes a \u003cstrong\u003eworst-case token reservation\u003c\/strong\u003e to a run-specific ledger row in \u003cstrong\u003eai_usage_ledger\u003c\/strong\u003e.\u003c\/li\u003e\n  \u003cli\u003eBefore executing, the ledger is read again; if other parallel runs already consumed budget, the run \u003cstrong\u003ebacks off\u003c\/strong\u003e.\u003c\/li\u003e\n  \u003cli\u003eThe AI Agent runs with \u003cstrong\u003eMax Iterations\u003c\/strong\u003e and a \u003cstrong\u003etoken limit\u003c\/strong\u003e taken from the workflow’s Settings nodes.\u003c\/li\u003e\n  \u003cli\u003eAfter each model call, the workflow uses the \u003cstrong\u003en8n API\u003c\/strong\u003e to read recorded prompt\/completion token usage, then \u003cstrong\u003ebooks tokens to the correct client\u003c\/strong\u003e and returns the answer along with a usage summary.\u003c\/li\u003e\n  \u003cli\u003eYou receive an \u003cstrong\u003eemail alert\u003c\/strong\u003e at the configured threshold and when a client can’t afford another run.\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eHourly\u003c\/strong\u003e stuck reservations expire; \u003cstrong\u003emonthly\u003c\/strong\u003e totals are calculated per client and old ledger rows are deleted. A \u003cstrong\u003eGET endpoint\u003c\/strong\u003e returns usage per client.\u003c\/li\u003e\n  \u003cli\u003eIf the workflow can’t read\/write the Data tables, it refuses the run with \u003cstrong\u003e503\u003c\/strong\u003e.\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003ch3\u003eUse cases\u003c\/h3\u003e\n\u003cul\u003e\n  \u003cli\u003eSaaS agencies running one n8n AI agent for multiple customers with \u003cstrong\u003emonthly token limits\u003c\/strong\u003e.\u003c\/li\u003e\n  \u003cli\u003eFreelancers offering AI automation retainers where each client’s usage is \u003cstrong\u003etracked and capped\u003c\/strong\u003e.\u003c\/li\u003e\n  \u003cli\u003eMulti-tenant internal tools that need \u003cstrong\u003ewebhook-based AI execution\u003c\/strong\u003e and auditable usage reporting.\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003ch3\u003eTechnical details\u003c\/h3\u003e\n\u003cul\u003e\n  \u003cli\u003e\n\u003cstrong\u003eData tables:\u003c\/strong\u003e \u003ccode\u003eai_clients\u003c\/code\u003e, \u003ccode\u003eai_usage_ledger\u003c\/code\u003e\n\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eNodes\/integrations:\u003c\/strong\u003e webhook, Data table, if, set, code, n8n API (read token usage), OpenAI (chat model), SMTP (email alerts)\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eSecurity:\u003c\/strong\u003e Header Auth on both webhooks\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eSetup:\u003c\/strong\u003e configure Settings nodes (keep \u003cem\u003eSave execution progress\u003c\/em\u003e enabled), add client rows, and publish the workflow.\u003c\/li\u003e\n\u003c\/ul\u003e","brand":"N8N Commerce","offers":[{"title":"Default Title","offer_id":46157943472307,"sku":"N8N-20333","price":38.99,"currency_code":"GBP","in_stock":true}],"thumbnail_url":"\/\/cdn.shopify.com\/s\/files\/1\/0749\/6279\/6723\/files\/tgdQKEzo64BQx8Rm9R924_3Ko9sl7A.png?v=1791019204","url":"https:\/\/buyflowscripts.com\/products\/budget-capped-ai-agents-in-n8n-openai-client-runs-via-webhook","provider":"N8N Commerce","version":"1.0","type":"link"}