{
  "name": "Email weekly AI agent cost and token reports with Alephant and Gmail + BillionVerify",
  "nodes": [
    {
      "id": "e4a9ecb0-d35a-47a4-8b45-b8651c4ed9f1",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        176,
        -16
      ],
      "parameters": {
        "width": 480,
        "height": 496,
        "content": "## Track AI agent costs and token usage with Alephant AI Gateway\n\n### How it works\n\nThis workflow runs every Monday at 9 AM to collect AI cost and token usage data from Alephant AI Gateway. It adds report configuration, formats the usage summary into a readable report, and sends it by email through Gmail.\n\n### Setup steps\n\n1. Install the Alephant community node\n2. Create an Alephant Virtual Key credential (docs link below)\n3. Connect your Gmail credential\n4. Edit the 'Set Report Config' node - set your recipient email\n5. Adjust the Schedule Trigger if you want daily/hourly instead of weekly\n\n**Requirements**\n- Alephant account (free tier works) - https://alephant.io\n- Gmail account\n\nDocs: https://developers.alephant.io/\n"
      },
      "typeVersion": 1
    },
    {
      "id": "83adfc10-10d2-4e4a-a678-04b2e0db6fc8",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        736,
        -16
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 336,
        "content": "## Schedule usage fetch\n\nStarts the workflow on the weekly Monday morning schedule and retrieves the Alephant AI Gateway usage summary."
      },
      "typeVersion": 1
    },
    {
      "id": "067b5a54-55d6-4c6f-959c-b6d78d79de91",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1216,
        -16
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 336,
        "content": "## Configure and format report\n\nSets the report recipient configuration, then formats the Alephant usage data into an email-ready report."
      },
      "typeVersion": 1
    },
    {
      "id": "503da710-b1d3-410f-b46f-e5f1c6621b83",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1696,
        -16
      ],
      "parameters": {
        "color": 7,
        "height": 336,
        "content": "## Send report email\n\nDelivers the formatted weekly cost and token usage report through Gmail."
      },
      "typeVersion": 1
    },
    {
      "id": "34486343-b066-491a-95b5-b318a311376a",
      "name": "Weekly Monday Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        784,
        144
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 9
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "4a6cd5bb-7893-49bd-a172-1cce9ababd76",
      "name": "Fetch Alephant Usage Summary",
      "type": "@alephantai/n8n-nodes-alephant-analytics.alephantUsage",
      "position": [
        1024,
        144
      ],
      "parameters": {},
      "credentials": {
        "alephantVirtualKeyApi": {
          "id": "credential-id",
          "name": "Alephant Virtual Key account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "04bc6b5b-5bab-4f5b-a126-1951177d03cd",
      "name": "Set Email Recipient",
      "type": "n8n-nodes-base.set",
      "position": [
        1264,
        144
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "f1+1234567890-+1234567890",
              "name": "recipient_email",
              "type": "string",
              "value": "you@example.com"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "c3c09c8d-c5e7-43a1-965e-f45e1931aa7d",
      "name": "Format Usage Report",
      "type": "n8n-nodes-base.code",
      "position": [
        1504,
        144
      ],
      "parameters": {
        "jsCode": "// Pull the Usage Summary output from the Alephant node\nconst d = $('Fetch Alephant Usage Summary').first().json.data;\n\n// Cost: total_cost_cents is in CENTS - convert to dollars\nconst costUSD = (d.total_cost_cents / 100).toFixed(2);\n\n// Round the week-over-week percentages (they come back as long floats)\nconst costPct = Math.round(d.vs_previous_period.cost_pct);\nconst reqPct  = Math.round(d.vs_previous_period.requests_pct);\n\n// Helper: turn a +/- number into a readable trend phrase\nconst trend = (pct) => {\n  if (pct > 0) return `up ${pct}% vs previous period`;\n  if (pct < 0) return `down ${Math.abs(pct)}% vs previous period`;\n  return `no change vs previous period`;\n};\n\nconst totalTokens = d.total_tokens.input + d.total_tokens.output;\n\n// Build the email body (plain text)\nconst body =\n`AI Agent Cost Report - Alephant AI Gateway\n\nPeriod: ${d.period.from} to ${d.period.to}\n\nTotal cost:     $${costUSD} (${trend(costPct)})\nTotal requests: ${d.total_requests} (${trend(reqPct)})\nTotal tokens:   ${totalTokens} (input ${d.total_tokens.input} / output ${d.total_tokens.output})\n\nData source: ${d.data_source}${d.degraded ? ' (WARNING: data may be degraded)' : ''}\n\nSent automatically by your Alephant AI Gateway workflow.`;\n\n// Pass the body + recipient on to the Gmail node\nreturn [{\n  json: {\n    email_subject: `AI Agent Cost Report (${d.period.from} to ${d.period.to})`,\n    email_body: body,\n    recipient_email: $('Set Email Recipient').first().json.recipient_email\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "6edadefb-9099-4db3-b45c-1be5c2a98e02",
      "name": "Send Usage Summary Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1744,
        144
      ],
      "webhookId": "c17a6bf9-7921-4f34-abfe-1fe84d1e88bb",
      "parameters": {
        "sendTo": "={{ $json.recipient_email }}",
        "message": "={{ $json.email_body }}",
        "options": {},
        "subject": "={{ $json.email_subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "credential-id",
          "name": "Gmail Ash account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "1b7ae0bd-25d6-4f49-9a36-7f782152ffb1",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        736,
        352
      ],
      "parameters": {
        "width": 880,
        "height": 128,
        "content": "## Who's it for\n\nTeams running production AI agents - autonomous workflows that call AI models on their own and can quietly rack up cost you only discover on the invoice. Also useful for solo developers and AI-first startups who want a regular read on AI spend."
      },
      "typeVersion": 1
    },
    {
      "parameters": {
        "operation": "verify",
        "email": "={{ $json.recipient_email }}",
        "additionalOptions": {}
      },
      "type": "n8n-nodes-billionverify.billionVerify",
      "typeVersion": 1,
      "position": [
        1384,
        144
      ],
      "name": "Verify Email (BillionVerify)",
      "credentials": {
        "billionVerifyApi": {
          "id": "",
          "name": "BillionVerify account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "is-deliverable",
              "leftValue": "={{ $json.is_deliverable }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ]
        }
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1564,
        144
      ],
      "name": "IF deliverable"
    }
  ],
  "connections": {
    "Format Usage Report": {
      "main": [
        [
          {
            "node": "Verify Email (BillionVerify)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Email Recipient": {
      "main": [
        [
          {
            "node": "Format Usage Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Weekly Monday Trigger": {
      "main": [
        [
          {
            "node": "Fetch Alephant Usage Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Alephant Usage Summary": {
      "main": [
        [
          {
            "node": "Set Email Recipient",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify Email (BillionVerify)": {
      "main": [
        [
          {
            "node": "IF deliverable",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF deliverable": {
      "main": [
        [
          {
            "node": "Send Usage Summary Email",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  }
}