Rate Limiter

Problem: My AI Agents are starting to hit rate limits.

Goal: I want to create a global rate limiter for different APIs that my AI Agents call.

Solved using the Build with AI tool

{“nodes”: [{“parameters”: {“httpMethod”: “POST”,“path”: “rate-limiter”,“responseMode”: “responseNode”,“options”: {}},“id”: “b92f8c5b-7248-40b8-bf9d-8317159c78f2”,“name”: “Rate Limiter Webhook”,“type”: “n8n-nodes-base.webhook”,“typeVersion”: 2.1,“position”: [-1120,96],“webhookId”: “f4d61fee-758d-4e97-a152-33b675f9049b”},{“parameters”: {“assignments”: {“assignments”: [{“id”: “id-1”,“name”: “rateLimitKey”,“value”: “api_rate_limit”,“type”: “string”},{“id”: “id-2”,“name”: “maxRequests”,“value”: 100,“type”: “number”},{“id”: “id-3”,“name”: “timeWindowSeconds”,“value”: 60,“type”: “number”}]},“includeOtherFields”: true,“options”: {}},“id”: “266d85fd-1856-4bb9-af97-79eb05a808fc”,“name”: “Workflow Configuration”,“type”: “n8n-nodes-base.set”,“typeVersion”: 3.4,“position”: [-896,96]},{“parameters”: {“operation”: “get”,“key”: “={{ $(‘Workflow Configuration’).first().json.rateLimitKey }}”,“options”: {}},“id”: “5d7b5cc2-9864-4baa-be94-133359414ac2”,“name”: “Get Current Count”,“type”: “n8n-nodes-base.redis”,“typeVersion”: 1,“position”: [-672,96]},{“parameters”: {“jsCode”: “const config = $(‘Workflow Configuration’).first().json;\nconst currentCount = $input.first().json.value || 0;\nconst maxRequests = config.maxRequests;\nconst withinLimit = currentCount < maxRequests;\n\nreturn {\n  json: {\n    currentCount,\n    maxRequests,\n    withinLimit,\n    remaining: maxRequests - currentCount\n  }\n};”},“id”: “cd835d95-c53d-40da-8fdc-c2275ddccc51”,“name”: “Check Rate Limit”,“type”: “n8n-nodes-base.code”,“typeVersion”: 2,“position”: [-448,96]},{“parameters”: {“conditions”: {“options”: {“caseSensitive”: true,“leftValue”: “”,“typeValidation”: “loose”},“conditions”: [{“id”: “id-1”,“leftValue”: “={{ $json.withinLimit }}”,“operator”: {“type”: “boolean”,“operation”: “true”}}],“combinator”: “and”},“options”: {}},“id”: “0d3e45e8-7bf4-433e-abcd-a4a7dc842f63”,“name”: “Within Limit?”,“type”: “n8n-nodes-base.if”,“typeVersion”: 2.2,“position”: [-224,96]},{“parameters”: {“operation”: “incr”,“key”: “={{ $(‘Workflow Configuration’).first().json.rateLimitKey }}”,“expire”: true,“ttl”: “={{ $(‘Workflow Configuration’).first().json.timeWindowSeconds }}”},“id”: “5f632968-df85-491e-bb2f-3db661e70aa8”,“name”: “Increment Counter”,“type”: “n8n-nodes-base.redis”,“typeVersion”: 1,“position”: [0,0]},{“parameters”: {“assignments”: {“assignments”: [{“id”: “id-1”,“name”: “allowed”,“value”: true,“type”: “boolean”},{“id”: “id-2”,“name”: “message”,“value”: “Request allowed”,“type”: “string”},{“id”: “id-3”,“name”: “statusCode”,“value”: 200,“type”: “number”}]},“includeOtherFields”: true,“options”: {}},“id”: “1562c9d5-c95e-457b-8873-c224c54b1f04”,“name”: “Allow Response”,“type”: “n8n-nodes-base.set”,“typeVersion”: 3.4,“position”: [224,0]},{“parameters”: {“assignments”: {“assignments”: [{“id”: “id-1”,“name”: “allowed”,“value”: false,“type”: “boolean”},{“id”: “id-2”,“name”: “message”,“value”: “Rate limit exceeded. Please wait and try again.”,“type”: “string”},{“id”: “id-3”,“name”: “statusCode”,“value”: 429,“type”: “number”}]},“includeOtherFields”: true,“options”: {}},“id”: “534c88c8-eaf3-4c40-9b3c-7f8868be9377”,“name”: “Throttle Response”,“type”: “n8n-nodes-base.set”,“typeVersion”: 3.4,“position”: [224,192]},{“parameters”: {“respondWith”: “json”,“options”: {“responseCode”: “={{ $json.statusCode }}”}},“id”: “79d1dc88-beb5-41d7-80dd-90837bca8856”,“name”: “Return Response”,“type”: “n8n-nodes-base.respondToWebhook”,“typeVersion”: 1.4,“position”: [448,96]}],“connections”: {“Rate Limiter Webhook”: {“main”: [[{“node”: “Workflow Configuration”,“type”: “main”,“index”: 0}]]},“Workflow Configuration”: {“main”: [[{“node”: “Get Current Count”,“type”: “main”,“index”: 0}]]},“Get Current Count”: {“main”: [[{“node”: “Check Rate Limit”,“type”: “main”,“index”: 0}]]},“Check Rate Limit”: {“main”: [[{“node”: “Within Limit?”,“type”: “main”,“index”: 0}]]},“Within Limit?”: {“main”: [[{“node”: “Increment Counter”,“type”: “main”,“index”: 0}],[{“node”: “Throttle Response”,“type”: “main”,“index”: 0}]]},“Increment Counter”: {“main”: [[{“node”: “Allow Response”,“type”: “main”,“index”: 0}]]},“Allow Response”: {“main”: [[{“node”: “Return Response”,“type”: “main”,“index”: 0}]]},“Throttle Response”: {“main”: [[{“node”: “Return Response”,“type”: “main”,“index”: 0}]]}},“pinData”: {},“meta”: {“templateCredsSetupCompleted”: true,“instanceId”: “e4f19eec8e15650d2478a76d51ed1c67528974e2e0f9ba37a16e5c98f5168629”}}