{
  "name": "Track AI agent token usage and estimate costs in Google Sheets",
  "nodes": [
    {
      "id": "f78ee73d-3d5e-4162-9b40-74cc183cb59b",
      "name": "When clicking ‘Test workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -540,
        -520
      ]
    },
    {
      "id": "9fa5f512-fd5d-4e5b-9d73-f70ec0d4b13d",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -260,
        -520
      ]
    },
    {
      "id": "35e230ce-7413-4675-b2f7-cb837cccf936",
      "name": "Call sub-workflow",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        200,
        -520
      ]
    },
    {
      "id": "81bf6364-cdb1-47ac-840d-8c7d940bf616",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        60,
        -880
      ],
      "parameters": {
        "width": 380,
        "height": 540,
        "content": "## Wait for the workflow to finish before calling the subworkflow\nIf the execution is still running, too much data is retrieved and it becomes messy.\n\nSo put this node at the end of the workflow and d"
      }
    },
    {
      "id": "11b56bfb-170d-480a-a478-5022e540a5b5",
      "name": "Think",
      "type": "@n8n/n8n-nodes-langchain.toolThink",
      "position": [
        -60,
        -280
      ]
    },
    {
      "id": "2e9101bd-73cb-4fab-91c6-e884ddb51fbf",
      "name": "Extract token usage data",
      "type": "n8n-nodes-base.set",
      "position": [
        -100,
        180
      ]
    },
    {
      "id": "1bbefdea-cd7a-46e4-8b6f-02e3ba7c4569",
      "name": "Get execution data",
      "type": "n8n-nodes-base.n8n",
      "position": [
        -320,
        180
      ]
    },
    {
      "id": "8cfd62f0-d3a6-4259-ae3f-471b31c177e6",
      "name": "When Executed by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -540,
        180
      ]
    },
    {
      "id": "6acea4e7-3fc9-4f11-8a2e-631ab187a66d",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -840,
        160
      ],
      "parameters": {
        "width": null,
        "height": 180,
        "content": "After the main workflow calls the subworkflow, you'll be able to see the total tokens in the Executions spreadsheet."
      }
    },
    {
      "id": "88253810-84bb-4640-9fb9-9da116d10f4f",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        120,
        180
      ]
    },
    {
      "id": "c91c32de-6357-47ee-abaf-fbf639362b1e",
      "name": "Sum Token Totals - aggregate by model",
      "type": "n8n-nodes-base.summarize",
      "position": [
        320,
        180
      ]
    },
    {
      "id": "022cd2fc-3d1c-43fd-910e-02af46eeb4fa",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        740,
        -240
      ],
      "parameters": {
        "width": 660,
        "height": 580,
        "content": "## Limitations\n### 1. This workflow doesn't account for [Prompt caching](https://platform.openai.com/docs/guides/prompt-caching)\nIf you're consecutively sending similar prompts to OpenAI it'll automat"
      }
    },
    {
      "id": "5a687ff7-2f31-4541-8d8c-245cb345de2b",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -840,
        -520
      ],
      "parameters": {
        "width": null,
        "height": 120,
        "content": "This is an example AI Agent.\n\nUse this only to understand how to call the subworkflow and obtain the token amount."
      }
    },
    {
      "id": "6b96057d-150e-4a97-aff7-317c369af156",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1560,
        -620
      ],
      "parameters": {
        "width": 660,
        "height": 260,
        "content": "## Where to find LLM pricing?\nYou can enter each provider's website or use one of these:\n- [llm-price.com](https://llm-price.com)\n- [llm-prices.com](https://llm-prices.com)\n- [llmprices.dev](https://l"
      }
    },
    {
      "id": "18797895-4302-422c-b1a4-44a1e5e72e5c",
      "name": "Record token usage",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        540,
        180
      ]
    },
    {
      "id": "b958bc0c-9142-4d1c-a72f-e05fb0f007c6",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1560,
        -880
      ],
      "parameters": {
        "width": 660,
        "height": 240,
        "content": "## Make a copy of this Sheets file\n👉 [**[TEMPLATE] Calculate LLM Token Usage**](https://docs.google.com/spreadsheets/d/1c9CeePI6ebNnIKogyJKHUpDWT6UEowpH9OwVtViadyE/edit?usp=sharing)\n\nThere are two she"
      }
    },
    {
      "id": "1f7fb4ae-ca87-484a-9b45-38d4a7f14c6d",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        740,
        -620
      ],
      "parameters": {
        "width": 400,
        "height": 200,
        "content": "# Need help?\nCreate a topic on the community forums here:\nhttps://community.n8n.io/c/questions/\n\n\nOr join our exclusive [Scrapes Academy](https://www.skool.com/scrapes/about?ref=21f10ad99f4d46ba9b8aae"
      }
    },
    {
      "id": "a4370528-00d7-4b04-8eb2-8aab6d5d77dc",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1560,
        -340
      ],
      "parameters": {
        "width": 660,
        "height": 680,
        "content": "# Author\n![Solomon](https://gravatar.com/avatar/79aa147f090807fe0f618fb47a1de932669e385bb0c84bf3a7f891ae7d174256?r=pg&d=retro&size=200)\n### Solomon\n🎓 AI & Automation Educator at the [Scrapes Academy]("
      }
    },
    {
      "id": "17cf9314-b6a2-4abc-9223-846c08d53237",
      "name": "Sticky Note16",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1540,
        240
      ],
      "parameters": {
        "width": 620,
        "height": 80,
        "content": "### 💡 **Want to learn advanced n8n skills and earn money building workflows?**\n‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎Check out [Scrapes Academy](https://www.skool.com/scrapes/about?ref=21f10ad99f4d46ba9b8aaea8c9f58c34)"
      }
    },
    {
      "id": "9cd12907-4b88-4bed-a997-53b7a0d07d12",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2280,
        -880
      ],
      "parameters": {
        "width": 700,
        "height": 640,
        "content": "## Description\n### This n8n template demonstrates how to obtain token usage from AI Agents and places the data into a spreadsheet that calculates the estimated cost of the execution.\n\nObtaining the to"
      }
    },
    {
      "id": "51c0beee-ca42-4d3b-a725-1922a778cfcc",
      "name": "Gemini",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -400,
        -280
      ]
    },
    {
      "id": "2e02c00e-375e-4fdb-adc5-0b49f6217cd1",
      "name": "OpenAI",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -320,
        -280
      ]
    },
    {
      "id": "7dcc1116-ba80-4fad-89f9-ddae07a906e9",
      "name": "Anthropic",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "position": [
        -240,
        -280
      ]
    },
    {
      "id": "e222998d-669c-47f6-9ddc-037a2b2c3166",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -420,
        -160
      ],
      "parameters": {
        "width": 280,
        "height": 200,
        "content": "Works well with all the providers above.\n\nIf you use another provider, you might need to adjust this node:\n\n`Extract token usage data`\n\nOn the below workflow 👇"
      }
    }
  ],
  "connections": {
    "Think": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Gemini": {
      "ai_languageModel": [
        []
      ]
    },
    "OpenAI": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Call sub-workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Anthropic": {
      "ai_languageModel": [
        []
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Sum Token Totals - aggregate by model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get execution data": {
      "main": [
        [
          {
            "node": "Extract token usage data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract token usage data": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Get execution data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Test workflow’": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sum Token Totals - aggregate by model": {
      "main": [
        [
          {
            "node": "Record token usage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}