AutomationFlowsGeneral › Alephant Cost Control Request Cost Tracking

Alephant Cost Control Request Cost Tracking

Alephant Cost Control Request Cost Tracking. Uses @alephantai/n8n-nodes-alephant. Event-driven trigger; 7 nodes.

Event trigger★★★★☆ complexity7 nodes@Alephantai/N8N Nodes Alephant
General Trigger: Event Nodes: 7 Complexity: ★★★★☆ Added:

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "name": "Alephant Cost Control Request Cost Tracking",
  "nodes": [
    {
      "parameters": {},
      "id": "e7efb1ec-58a1-43b3-9ae5-19d25e5108e1",
      "name": "When clicking 'Test workflow'",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        120,
        260
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "5e550f0b-89a7-4d3f-9e0a-659d2cabd96d",
              "name": "prompt",
              "value": "\u8bf7\u7528\u4e2d\u6587\u7528\u4e09\u53e5\u8bdd\u4ecb\u7ecd Alephant\uff0c\u5e76\u8bf4\u660e\u5b83\u9002\u5408\u89e3\u51b3\u4ec0\u4e48\u4e1a\u52a1\u95ee\u9898\u3002",
              "type": "string"
            },
            {
              "id": "fc4b2016-8d09-41c0-8ba2-bbe2f37bb2cd",
              "name": "model",
              "value": "gpt-4o-mini",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "b8460284-cfa4-4805-977b-7c119a4e8efe",
      "name": "Build AI Request",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        360,
        260
      ]
    },
    {
      "parameters": {
        "operation": "chatCompletion",
        "model": "={{$json.model}}",
        "inputMode": "prompt",
        "prompt": "={{$json.prompt}}",
        "responseFormat": "text",
        "metadata": "{}",
        "additionalOptions": "{}"
      },
      "id": "5df84560-f190-4e76-9197-f067824f81ca",
      "name": "Alephant Cost Control - Chat Completion",
      "type": "@alephantai/n8n-nodes-alephant.alephantAi",
      "typeVersion": 1,
      "position": [
        620,
        260
      ]
    },
    {
      "parameters": {
        "resume": "timeInterval",
        "amount": 3,
        "unit": "seconds"
      },
      "id": "8f12a8d1-75e3-401d-b6bc-93aa1442f6c7",
      "name": "Wait for Request Log",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        880,
        260
      ]
    },
    {
      "parameters": {
        "operation": "requestLogDetail",
        "requestLogId": "={{$json.requestLogId || $json.requestId}}",
        "workspaceId": "={{$json.workspaceId || \"\"}}"
      },
      "id": "53ab1e42-b46f-46fe-8f6f-d305c19d2169",
      "name": "Alephant AI Analytics - Request Log Detail",
      "type": "@alephantai/n8n-nodes-alephant.alephantUsage",
      "typeVersion": 1,
      "position": [
        1140,
        260
      ]
    },
    {
      "parameters": {
        "jsCode": "const request = $items('Build AI Request')[0]?.json ?? {};\nconst ai = $items('Alephant Cost Control - Chat Completion')[0]?.json ?? {};\nconst log = $input.first().json ?? {};\n\nconst firstDefined = (...values) => values.find((value) => value !== undefined && value !== null && value !== '');\nconst nested = (...paths) => {\n  for (const path of paths) {\n    const value = path.split('.').reduce((current, key) => current?.[key], log);\n    if (value !== undefined && value !== null && value !== '') return value;\n  }\n  return undefined;\n};\n\nconst cost = firstDefined(\n  nested('cost'),\n  nested('totalCost'),\n  nested('total_cost'),\n  nested('amount'),\n  nested('data.cost'),\n  nested('data.totalCost'),\n  nested('data.total_cost'),\n  nested('billing.cost'),\n  nested('billing.totalCost'),\n);\n\nconst currency = firstDefined(\n  nested('currency'),\n  nested('data.currency'),\n  nested('billing.currency'),\n  'USD',\n);\n\nreturn [\n  {\n    json: {\n      requestLogId: ai.requestLogId || ai.requestId,\n      model: ai.model || request.model,\n      prompt: request.prompt,\n      responseText: ai.text,\n      tokenUsageFromAiResponse: ai.usage,\n      cost,\n      currency,\n      requestLog: log,\n    },\n  },\n];"
      },
      "id": "ee98c7a7-24c4-4d8d-9863-bf02400f6ca4",
      "name": "Summarize Cost",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1400,
        260
      ]
    },
    {
      "parameters": {
        "content": "Before running:\n\n1. Select the same Alephant Virtual Key credential on both Alephant nodes.\n2. The AI node sends `x-request-id` and outputs it as `requestLogId`.\n3. The Request Log Detail node uses `workspaceId` from input or credentials. If missing, it automatically calls Scope to resolve it.\n\nThen it waits 3 seconds, queries the matching request log, and summarizes cost.",
        "height": 260,
        "width": 460,
        "color": 4
      },
      "id": "6dc0b972-2d74-4565-b678-0990a84fa228",
      "name": "Setup Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        360,
        500
      ]
    }
  ],
  "connections": {
    "When clicking 'Test workflow'": {
      "main": [
        [
          {
            "node": "Build AI Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build AI Request": {
      "main": [
        [
          {
            "node": "Alephant Cost Control - Chat Completion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Alephant Cost Control - Chat Completion": {
      "main": [
        [
          {
            "node": "Wait for Request Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for Request Log": {
      "main": [
        [
          {
            "node": "Alephant AI Analytics - Request Log Detail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Alephant AI Analytics - Request Log Detail": {
      "main": [
        [
          {
            "node": "Summarize Cost",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "c70f1651-a89c-4e7e-95bb-657454033b50",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

Alephant Cost Control Request Cost Tracking. Uses @alephantai/n8n-nodes-alephant. Event-driven trigger; 7 nodes.

Source: https://github.com/AlephantAI/alephant-n8n/blob/main/docs/alephant-ai-request-cost-workflow.json — original creator credit. Request a take-down →

More General workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

General

Reagendamiento. Uses executeWorkflowTrigger, redis, n8n-nodes-evolution-api, dataTable. Event-driven trigger; 73 nodes.

Execute Workflow Trigger, Redis, N8N Nodes Evolution Api +2
General

Blotato. Uses googleSheets, @blotato/n8n-nodes-blotato. Event-driven trigger; 65 nodes.

Google Sheets, @Blotato/N8N Nodes Blotato
General

This template is a hands-on, practical exam designed to help you master n8n Expressions—the key to accessing and manipulating data in your workflows.

Stop And Error
General

This template is a hands-on, practical exam designed to test your understanding of the fundamental JSON data types. It's the perfect way to solidify your knowledge after learning the basics.

Stop And Error
General

Agendamiento. Uses n8n-nodes-evolution-api, redis, dataTable, executeWorkflowTrigger. Event-driven trigger; 60 nodes.

N8N Nodes Evolution Api, Redis, Data Table +2