{
  "name": "LLM calls with a budget and a router",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "task",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "c0ab7abf-534f-4a75-803c-bf34ee21a336",
      "name": "Task in",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "webhookId": "acda497f-05f7-4398-8535-23f7b8723431"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select coalesce(sum(cost_usd),0) as spent from llm_calls where created_at >= current_date;",
        "options": {}
      },
      "id": "c145664a-81b8-437c-bc4c-26643f1d1247",
      "name": "Spend today",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "a506acc7-fd72-44b5-ae3a-c8da315348b4",
              "leftValue": "={{ $json.spent }}",
              "rightValue": "={{ 50 }}",
              "operator": {
                "type": "number",
                "operation": "lt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "c42ac64d-f55d-45ef-9636-0e21c5ee17b5",
      "name": "Under daily budget?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $('Task in').item.json.body.type }}",
                    "rightValue": "extract",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "cheap"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $('Task in').item.json.body.type }}",
                    "rightValue": "judge",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "frontier"
            }
          ]
        },
        "options": {}
      },
      "id": "7c07b47f-074d-41f6-8fb6-6807af033ccf",
      "name": "Route by task type",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        680,
        220
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $('Task in').item.json.body.input }}"
      },
      "id": "2d4a7c8e-5262-4bbb-9be5-0d20f1dfcfff",
      "name": "Small model step",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        940,
        60
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4.1-nano",
          "mode": "list",
          "cachedResultName": "gpt-4.1-nano"
        },
        "options": {
          "maxTokens": 400
        }
      },
      "id": "2c0a4638-95e5-4453-8d91-9dd38393cb30",
      "name": "Small model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        960,
        240
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $('Task in').item.json.body.input }}"
      },
      "id": "a0c32731-dbfb-409b-9b70-f9b1c2e6e7d8",
      "name": "Frontier model step",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        940,
        400
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "claude-sonnet-4-5",
          "mode": "list",
          "cachedResultName": "Claude Sonnet"
        },
        "options": {
          "maxTokensToSample": 1200
        }
      },
      "id": "f0e771cd-23e7-435f-976b-e412ed980706",
      "name": "Frontier model",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.3,
      "position": [
        960,
        580
      ]
    },
    {
      "parameters": {
        "jsCode": "// tokens x your provider's price list\nreturn $input.all();"
      },
      "id": "993a1eee-ca3b-42c4-a08c-99cada50d311",
      "name": "Price the call",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1260,
        220
      ]
    },
    {
      "parameters": {
        "schema": {
          "__rl": true,
          "value": "public",
          "mode": "list"
        },
        "table": {
          "__rl": true,
          "value": "llm_calls",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "212faf1c-eb2f-4c41-b5a9-905955db6813",
      "name": "Write cost row",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1500,
        220
      ]
    },
    {
      "parameters": {
        "respondWith": "allIncomingItems",
        "options": {}
      },
      "id": "0a8c07ac-f1f5-4477-bb25-412ed5632bf4",
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1740,
        220
      ]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "#ai-spend",
          "mode": "name"
        },
        "text": "=Daily LLM budget reached: {{ $json.spent }} USD. New tasks are paused.",
        "otherOptions": {}
      },
      "id": "96c89d0f-708b-47ae-b2e3-0be4873fdf91",
      "name": "Over budget: alert",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        440,
        560
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"error\": \"budget_reached\" }",
        "options": {
          "responseCode": 429
        }
      },
      "id": "bfe2e837-ea18-4648-a4e8-2120ec01450a",
      "name": "Respond 429",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        680,
        640
      ]
    }
  ],
  "connections": {
    "Task in": {
      "main": [
        [
          {
            "node": "Spend today",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spend today": {
      "main": [
        [
          {
            "node": "Under daily budget?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Under daily budget?": {
      "main": [
        [
          {
            "node": "Route by task type",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Over budget: alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Over budget: alert": {
      "main": [
        [
          {
            "node": "Respond 429",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by task type": {
      "main": [
        [
          {
            "node": "Small model step",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Frontier model step",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Small model": {
      "ai_languageModel": [
        [
          {
            "node": "Small model step",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Frontier model": {
      "ai_languageModel": [
        [
          {
            "node": "Frontier model step",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Small model step": {
      "main": [
        [
          {
            "node": "Price the call",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Frontier model step": {
      "main": [
        [
          {
            "node": "Price the call",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Price the call": {
      "main": [
        [
          {
            "node": "Write cost row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write cost row": {
      "main": [
        [
          {
            "node": "Respond",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}