AutomationFlowsAI & RAG › Ghl Agent AI - Usage Tracking & Billing

Ghl Agent AI - Usage Tracking & Billing

GHL Agent AI - Usage Tracking & Billing. Uses postgres. Scheduled trigger; 4 nodes.

Cron / scheduled trigger★★★★☆ complexity4 nodesPostgres
AI & RAG Trigger: Cron / scheduled Nodes: 4 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": "GHL Agent AI - Usage Tracking & Billing",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 1
            }
          ]
        }
      },
      "id": "hourly-trigger",
      "name": "Every Hour",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT client_id, COUNT(*) as task_count, SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed_count, AVG(EXTRACT(EPOCH FROM (completed_at - created_at))) as avg_duration_seconds FROM automation_tasks WHERE created_at >= NOW() - INTERVAL '1 hour' GROUP BY client_id",
        "options": {}
      },
      "id": "get-hourly-usage",
      "name": "Get Hourly Usage Stats",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        470,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const usage = $input.item.json;\nconst costPerTask = 0.10;\nconst cost = usage.task_count * costPerTask;\n\nreturn {\n  json: {\n    client_id: usage.client_id,\n    period_start: new Date(Date.now() - 3600000).toISOString(),\n    period_end: new Date().toISOString(),\n    task_count: usage.task_count,\n    completed_count: usage.completed_count,\n    success_rate: (usage.completed_count / usage.task_count * 100).toFixed(2),\n    avg_duration_seconds: Math.round(usage.avg_duration_seconds || 0),\n    cost_usd: cost.toFixed(2)\n  }\n};"
      },
      "id": "calculate-costs",
      "name": "Calculate Costs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        690,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO usage_records (client_id, period_start, period_end, task_count, completed_count, success_rate, avg_duration_seconds, cost_usd, created_at) VALUES ('{{ $json.client_id }}', '{{ $json.period_start }}', '{{ $json.period_end }}', {{ $json.task_count }}, {{ $json.completed_count }}, {{ $json.success_rate }}, {{ $json.avg_duration_seconds }}, {{ $json.cost_usd }}, NOW())",
        "options": {}
      },
      "id": "save-usage-record",
      "name": "Save Usage Record",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        910,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every Hour": {
      "main": [
        [
          {
            "node": "Get Hourly Usage Stats",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Hourly Usage Stats": {
      "main": [
        [
          {
            "node": "Calculate Costs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Costs": {
      "main": [
        [
          {
            "node": "Save Usage Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1",
  "id": "4",
  "tags": []
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

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

About this workflow

GHL Agent AI - Usage Tracking & Billing. Uses postgres. Scheduled trigger; 4 nodes.

Source: https://github.com/Julianb233/bottleneck-bots/blob/bab6bf99199efa86431b64ee1fd7da460e955e2b/n8n-workflows/4-usage-tracking.json — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

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

AI & RAG

Processor — EXECUTE NEXT (5min, dep-aware) ✅. Uses postgres, httpRequest, telegram. Scheduled trigger; 32 nodes.

Postgres, HTTP Request, Telegram
AI & RAG

Trend Monitoring Workflow. Uses postgres, httpRequest. Scheduled trigger; 22 nodes.

Postgres, HTTP Request
AI & RAG

Automatically retrieve AWS invoices and create corresponding bills in FreeAgent, marking them as paid. This workflow eliminates manual data entry for AWS billing. Fetches invoices from AWS Invoicing A

Postgres, HTTP Request
AI & RAG

Escalation Trigger — /approve /disapprove /requeue. Uses postgres, httpRequest, telegram. Scheduled trigger; 16 nodes.

Postgres, HTTP Request, Telegram
AI & RAG

Gmail-Cache-Manager. Uses postgres, gmail. Scheduled trigger; 11 nodes.

Postgres, Gmail