{
  "name": "429 Retry-After loop (TaskToAgent)",
  "nodes": [
    {
      "parameters": {},
      "name": "When clicking 'Execute workflow'",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "6b9f1958-4c79-46e9-9c62-ff63290e33f9"
    },
    {
      "parameters": {
        "jsCode": "// Sample items. Replace this node with your own data.\nreturn [1, 2, 3].map(id => ({ json: { id } }));"
      },
      "name": "Your items",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "id": "f283db2a-b1fb-4f82-a0cf-43019dcec2e4"
    },
    {
      "parameters": {
        "url": "=https://api.example.com/items/{{ $json.id }}",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true
            }
          }
        }
      },
      "name": "Call the API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "id": "0c3ae6b0-d4f6-4d8f-9b7a-bf6eb0ab415f"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "2fe26752-8228-4643-aad9-bbc369ba210a",
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 429,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "name": "Is 429?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        660,
        0
      ],
      "id": "f49e95c0-2fe6-4243-9e43-88780530fe98"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Runs once for each item that came back with 429.\n// Waits as long as the API's Retry-After header asks (seconds or an HTTP date),\n// or 2, 4, 8 ... seconds if there is no header, plus up to 20% random jitter.\nconst attempt = $runIndex + 1;\nif (attempt > 5) throw new Error('Still rate limited after 5 retries');\nconst v = ($json.headers || {})['retry-after'];\nlet seconds;\nif (v === undefined) seconds = Math.min(2 ** attempt, 60);\nelse if (isNaN(v)) seconds = (Date.parse(v) - Date.now()) / 1000;\nelse seconds = Number(v);\nseconds = Math.ceil(Math.min(Math.max(seconds, 1), 300) * (1 + Math.random() * 0.2));\n// Send the original item back to the HTTP Request node, plus the wait.\nreturn { json: { ...$('Your items').item.json, waitSeconds: seconds, attempt } };"
      },
      "name": "Work out the wait",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        -120
      ],
      "id": "923f1bdf-1a0c-46b2-bd90-44e67ce56c53"
    },
    {
      "parameters": {
        "amount": "={{ $json.waitSeconds }}",
        "unit": "seconds"
      },
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1100,
        -120
      ],
      "id": "3a0fef84-1731-42f5-a9a6-61412d8e38ae"
    },
    {
      "parameters": {},
      "name": "Done",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        880,
        120
      ],
      "id": "85322cc8-b00c-4b29-939a-91fd274c67f7"
    },
    {
      "parameters": {
        "content": "## 429 Retry-After loop\n1. Replace **Your items** with the node that produces your items.\n2. Put your real API call in **Call the API**. Keep Options > Response: *Include Response Headers and Status* and *Never Error* ON, and Settings > Retry On Fail OFF.\n3. Successful responses leave through **Done**. Items still rate limited after 5 tries stop the workflow with an error, so your error workflow hears about it.\n\nGuide: https://tasktoagent.com/guides/n8n-429-rate-limit-retry/",
        "height": 300,
        "width": 420,
        "color": 6
      },
      "name": "Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -360
      ],
      "id": "a5d4f4a0-d7b2-4772-8286-25a5a1f4cdbe"
    }
  ],
  "connections": {
    "When clicking 'Execute workflow'": {
      "main": [
        [
          {
            "node": "Your items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Your items": {
      "main": [
        [
          {
            "node": "Call the API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call the API": {
      "main": [
        [
          {
            "node": "Is 429?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is 429?": {
      "main": [
        [
          {
            "node": "Work out the wait",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Done",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Work out the wait": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Call the API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "pinData": {}
}