{
  "name": "Track LLM token costs per customer using the Langchain code node",
  "nodes": [
    {
      "id": "8884df86-b7cd-4cf7-8b71-fd21113bfc0f",
      "name": "Client Usage Log",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        420,
        500
      ]
    },
    {
      "id": "1e4aca76-8b79-4780-b0c5-2cd92a41aa0e",
      "name": "Logging Attributes",
      "type": "n8n-nodes-base.set",
      "position": [
        -360,
        -120
      ]
    },
    {
      "id": "d7f37c54-5d96-47ba-b82e-0926a08137df",
      "name": "On form submission",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        -920,
        -120
      ]
    },
    {
      "id": "06da0c8e-2035-45ae-a301-50021650a5f8",
      "name": "Custom LLM Subnode",
      "type": "@n8n/n8n-nodes-langchain.code",
      "position": [
        260,
        340
      ]
    },
    {
      "id": "35993bd5-b521-4239-bf23-aed47d339f54",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        360,
        480
      ],
      "parameters": {
        "width": 200,
        "height": 280,
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n### Update Workbook\nThis is the workbook which will track the token usage and costs."
      }
    },
    {
      "id": "623ca94d-a215-416b-a9fe-62a1f96317e3",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        -280
      ],
      "parameters": {
        "width": 560,
        "height": 380,
        "content": "## 1. Offer AI Service to Clients\nHere, we'll using an n8n form to offer a document extraction service for Resume/CV PDFs. The user simply uploads a PDF and we return it in JSON format. This is just a"
      }
    },
    {
      "id": "ba9eb149-e77f-4bf6-8ec5-7d8d4619485d",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -460,
        -280
      ],
      "parameters": {
        "width": 320,
        "height": 380,
        "content": "## 2. Gather External Variables to Send to Log\nThere are some variables we can't define in the subnode but we can add them here."
      }
    },
    {
      "id": "63bfe329-17dd-4321-94c6-17d306ed7412",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -120,
        -280
      ],
      "parameters": {
        "width": 720,
        "height": 380,
        "content": "## 3. Provide AI Service\nOur service uses an LLM (OpenAI GPT4o-mini in this example) to organise the uploaded PDF's data into a structured JSON format. This conversion is useful for following integrat"
      }
    },
    {
      "id": "f45862e9-7f8e-46bb-900a-807fee981ed5",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -120,
        120
      ],
      "parameters": {
        "width": 720,
        "height": 440,
        "content": "## 4. Use Custom LLM Subnode to Track Usage & Cost\n[Learn more about the Langchain Code Node](https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.code/)\n\nBy creating "
      }
    },
    {
      "id": "9f5014a5-0e9a-4af0-b076-03cdc0a14ab9",
      "name": "Display JSON Document",
      "type": "n8n-nodes-base.form",
      "position": [
        360,
        -120
      ]
    },
    {
      "id": "b977f89c-1118-455f-986e-735a17eecd9b",
      "name": "Filter Last Month",
      "type": "n8n-nodes-base.filter",
      "position": [
        1120,
        -120
      ]
    },
    {
      "id": "10d95dcb-d975-4b20-961e-d1fe63417878",
      "name": "Get Client Logs",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        920,
        -120
      ]
    },
    {
      "id": "f6505545-d57c-443a-9883-2d536f3a973a",
      "name": "Calculate Totals",
      "type": "n8n-nodes-base.summarize",
      "position": [
        1320,
        -120
      ]
    },
    {
      "id": "1c4ae8ff-ec2b-4fd3-974f-cc766385b16b",
      "name": "Every End of Month",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        720,
        -120
      ]
    },
    {
      "id": "f321fbe6-36b1-4bd8-899b-832a8fc6217a",
      "name": "Send Invoice",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1520,
        -120
      ]
    },
    {
      "id": "a7d8de78-c3b7-4687-8994-fe28387d7572",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        620,
        -280
      ],
      "parameters": {
        "width": 1100,
        "height": 380,
        "content": "## 5. Automatically Send Invoice at End of Month (Optional)\nWith our client usage log, it's fairly simple to aggregate the log records for the client within a certain timeframe and calculate the total"
      }
    },
    {
      "id": "169fa40d-c6e8-4315-be35-d2c73f626edf",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1500,
        -920
      ],
      "parameters": {
        "width": 440,
        "height": 1020,
        "content": "## Try It Out!\n### This n8n template demonstrates how to use the Langchain code node to track token usage and cost for every LLM call.\n\nThis is useful if your templates handle multiple clients or cust"
      }
    },
    {
      "id": "922710e3-f92b-4a7f-9ff2-c3d7d55f04d5",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        -420
      ],
      "parameters": {
        "width": 280,
        "height": 120,
        "content": "### SELF-HOSTED N8N ONLY\nPlease note, this template only works in the self-hosted version of n8n only. It will not work in the cloud version."
      }
    },
    {
      "id": "56c23cb5-818f-434d-96a7-0029f6607299",
      "name": "Parse PDF Upload",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        -700,
        -120
      ]
    },
    {
      "id": "f4cc9870-a73e-487c-a131-aca2735b2e60",
      "name": "Extract Resume Data",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        0,
        -120
      ]
    }
  ],
  "connections": {
    "Get Client Logs": {
      "main": [
        [
          {
            "node": "Filter Last Month",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Totals": {
      "main": [
        [
          {
            "node": "Send Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Client Usage Log": {
      "ai_tool": [
        [
          {
            "node": "Custom LLM Subnode",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Parse PDF Upload": {
      "main": [
        [
          {
            "node": "Logging Attributes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Last Month": {
      "main": [
        [
          {
            "node": "Calculate Totals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Custom LLM Subnode": {
      "ai_languageModel": [
        [
          {
            "node": "Extract Resume Data",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Every End of Month": {
      "main": [
        [
          {
            "node": "Get Client Logs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Logging Attributes": {
      "main": [
        [
          {
            "node": "Extract Resume Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On form submission": {
      "main": [
        [
          {
            "node": "Parse PDF Upload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Resume Data": {
      "main": [
        [
          {
            "node": "Display JSON Document",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}