{
  "id": "4cZxXe3I4X0DTPfH",
  "name": "Send AI agent budget overspend alerts with Alephant AI Gateway and Gmail",
  "tags": [],
  "nodes": [
    {
      "id": "153ee560-4e09-4f77-9a98-a6b20c4c160a",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1184,
        368
      ],
      "parameters": {
        "width": 480,
        "height": 592,
        "content": "## Alert on AI agent budget overspend with Alephant AI Gateway\n\n### How it works\n\nThis workflow monitors Alephant AI Gateway budget usage every six hours. It retrieves the current budget status, evaluates whether usage is above the configured threshold, and sends an email alert when overspend is detected.\n\n### Setup steps\n\n- Configure the Alephant AI Gateway usage node with the required Alephant credentials and budget or project settings.\n- Set the threshold logic in the \u201cOver Threshold?\u201d IF node to match the desired budget limit.\n- Update the recipient email in the \u201cSet Alert Config\u201d node.\n- Connect and authorize the Gmail node for the account that should send budget alerts.\n\n### Customization\n\nAdjust the schedule frequency, budget threshold, recipient list, or alert message formatting to fit your monitoring and notification requirements.\n\n**Requirements**\n- Alephant account (free tier works) - https://alephant.io\n- Gmail account\n\nDocs: https://developers.alephant.io"
      },
      "typeVersion": 1
    },
    {
      "id": "1a9205ee-1c04-4fe5-9a97-8398214a4aee",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -624,
        480
      ],
      "parameters": {
        "color": 2,
        "width": 672,
        "height": 304,
        "content": "## Check budget status\n\nRuns on a six-hour schedule, retrieves the Alephant AI Gateway budget usage, and checks whether the usage is over the configured threshold."
      },
      "typeVersion": 1
    },
    {
      "id": "36c7767c-5181-4455-bb20-670582bcc21e",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        96,
        368
      ],
      "parameters": {
        "color": 2,
        "width": 640,
        "height": 320,
        "content": "## Prepare and send alert\n\nSets the alert recipient, formats the budget overspend message, and sends the notification email through Gmail when the threshold condition is met."
      },
      "typeVersion": 1
    },
    {
      "id": "a741ed79-af2d-4849-831d-4b619735d114",
      "name": "Every 6 Hours Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -576,
        608
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "6324af8d-956b-40b3-9765-2549c29a6133",
      "name": "Check Budget Over Threshold",
      "type": "n8n-nodes-base.if",
      "position": [
        -96,
        608
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "c+1234567890-+1234567890",
              "operator": {
                "type": "number",
                "operation": "gte"
              },
              "leftValue": "={{ $json.data.virtual_key_budget.usage_pct }}",
              "rightValue": 1
            },
            {
              "id": "c+1234567890-+1234567890",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.data.data_source }}",
              "rightValue": "live"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "30d85440-fa15-4351-a038-1a8027cd7c06",
      "name": "Build Alert Message",
      "type": "n8n-nodes-base.code",
      "position": [
        368,
        528
      ],
      "parameters": {
        "jsCode": "// Pull the budget block from the Budget Status node\nconst b = $('Fetch Alephant Budget Status').first().json.data.virtual_key_budget;\n\nconst spentUSD     = (b.spent_cents / 100).toFixed(2);\nconst budgetUSD    = (b.budget_cents / 100).toFixed(2);\nconst remainingUSD = (b.remaining_cents / 100).toFixed(2);\nconst pct          = Math.round(b.usage_pct);\n\n// Explain what the gateway will do on its own, based on circuit-breaker mode\nlet breaker;\nswitch (b.budget_action) {\n  case 'alert_only':\n    breaker = 'alert_only - the gateway will NOT auto-stop this key. Your action may be needed.';\n    break;\n  case 'throttle':\n    breaker = 'throttle - the gateway will slow requests as the limit approaches.';\n    break;\n  case 'kill':\n    breaker = 'kill - the gateway will hard-stop this key at 100%.';\n    break;\n  default:\n    breaker = b.budget_action;\n}\n\nconst body =\n`AI Agent Budget Alert - Alephant AI Gateway\n\nThis virtual key has crossed your alert threshold.\n\nUsage:      ${pct}% of budget\nSpent:      $${spentUSD} of $${budgetUSD}\nRemaining:  $${remainingUSD}\nWindow:     ${b.budget_window}\nResets at:  ${b.resets_at}\n\nCircuit breaker: ${breaker}\n\nReview this key's activity in your Alephant dashboard: https://alephant.io\n\nSent automatically by your Alephant AI Gateway workflow.`;\n\nreturn [{\n  json: {\n    email_subject: `[Alephant] AI agent budget at ${pct}%`,\n    email_body: body,\n    recipient_email: $('Set Alert Recipient').first().json.recipient_email\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "cfbe7b40-f3ef-41a5-9a2d-1b967cd4515a",
      "name": "Set Alert Recipient",
      "type": "n8n-nodes-base.set",
      "position": [
        144,
        528
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "f2+1234567890-+1234567890",
              "name": "recipient_email",
              "type": "string",
              "value": "you@example.com"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "1f961b6f-5315-42e3-81ba-07a54a67935e",
      "name": "Send Alert Email via Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        592,
        528
      ],
      "parameters": {
        "sendTo": "={{ $json.recipient_email }}",
        "message": "={{ $json.email_body }}",
        "options": {},
        "subject": "={{ $json.email_subject }}",
        "emailType": "text"
      },
      "typeVersion": 2.1
    },
    {
      "id": "1b93b4b0-560c-49b1-ad50-2017a1bc60f8",
      "name": "Fetch Alephant Budget Status",
      "type": "@alephantai/n8n-nodes-alephant-analytics.alephantUsage",
      "position": [
        -336,
        608
      ],
      "parameters": {
        "operation": "budgetStatus"
      },
      "typeVersion": 1
    },
    {
      "id": "deaadccb-7998-4efe-b56a-3e8b0a7cd020",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -624,
        816
      ],
      "parameters": {
        "width": 1376,
        "height": 112,
        "content": "## Who's it for\n\nTeams running production AI agents that can quietly burn through budget - agent loops, retry storms, oversized prompts. This workflow watches an Alephant virtual key's budget and emails you the moment it crosses your alert threshold, so you find out in hours instead of on the invoice."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "10b765f7-d7c5-4e0a-98d5-f0fb630b6492",
  "connections": {
    "Build Alert Message": {
      "main": [
        [
          {
            "node": "Send Alert Email via Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Alert Recipient": {
      "main": [
        [
          {
            "node": "Build Alert Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Every 6 Hours Trigger": {
      "main": [
        [
          {
            "node": "Fetch Alephant Budget Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Budget Over Threshold": {
      "main": [
        [
          {
            "node": "Set Alert Recipient",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Alephant Budget Status": {
      "main": [
        [
          {
            "node": "Check Budget Over Threshold",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}