8
n8n 中文网amn8n.com

在 Google Sheets 中跟踪 AI 代理令牌使用量和估算成本

高级

这是一个Engineering领域的自动化工作流,包含 24 个节点。主要使用 N8n, Set, SplitOut, Summarize, GoogleSheets 等节点。 在 Google Sheets 中跟踪 AI 代理令牌使用量和估算成本

前置要求
  • Google Sheets API 凭证
  • OpenAI API Key
  • Anthropic API Key
  • Google Gemini API Key

分类

工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "d6b502dfa4d9dd072cdc5c2bb763558661053f651289291352a84403e01b3d1b",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "f78ee73d-3d5e-4162-9b40-74cc183cb59b",
      "name": "当点击\"测试工作流\"时",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -540,
        -520
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "9fa5f512-fd5d-4e5b-9d73-f70ec0d4b13d",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -260,
        -520
      ],
      "parameters": {
        "text": "Help me test something and output the text \"This is a test workflow\" after calling the think tool twice.",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 1.8
    },
    {
      "id": "35e230ce-7413-4675-b2f7-cb837cccf936",
      "name": "调用子工作流",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        200,
        -520
      ],
      "parameters": {
        "mode": "each",
        "options": {
          "waitForSubWorkflow": false
        },
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $workflow.id }}"
        },
        "workflowInputs": {
          "value": {
            "execution_id": "={{ $execution.id }}"
          },
          "schema": [
            {
              "id": "execution_id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "execution_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "execution_id"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "81bf6364-cdb1-47ac-840d-8c7d940bf616",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        60,
        -880
      ],
      "parameters": {
        "color": 3,
        "width": 380,
        "height": 540,
        "content": "## 在调用子工作流前等待主工作流完成"
      },
      "typeVersion": 1
    },
    {
      "id": "11b56bfb-170d-480a-a478-5022e540a5b5",
      "name": "思考",
      "type": "@n8n/n8n-nodes-langchain.toolThink",
      "position": [
        -60,
        -280
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "2e9101bd-73cb-4fab-91c6-e884ddb51fbf",
      "name": "提取令牌使用数据",
      "type": "n8n-nodes-base.set",
      "position": [
        -100,
        180
      ],
      "parameters": {
        "include": "selected",
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "2e6b9daf-495c-44e3-a39e-40fc8e654eae",
              "name": "execution_id",
              "type": "number",
              "value": "={{ $('When Executed by Another Workflow').item.json.execution_id }}"
            },
            {
              "id": "1ba39074-c67e-453c-9a64-07e0376e64bf",
              "name": "tokenUsage",
              "type": "array",
              "value": "={{$jmespath(\n  $json,\n  \"data.resultData.runData.*[] | [?data.ai_languageModel] | [].{model: data.ai_languageModel[0][0].json.response.generations[0][0].generationInfo.model_name || inputOverride.ai_languageModel[0][0].json.options.model_name || inputOverride.ai_languageModel[0][0].json.options.model, tokenUsage: data.ai_languageModel[0][0].json.tokenUsage || data.ai_languageModel[0][0].json.tokenUsageEstimate}\"\n)}}"
            }
          ]
        },
        "includeFields": "workflowData.id, workflowData.name",
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "1bbefdea-cd7a-46e4-8b6f-02e3ba7c4569",
      "name": "获取执行数据",
      "type": "n8n-nodes-base.n8n",
      "position": [
        -320,
        180
      ],
      "parameters": {
        "options": {
          "activeWorkflows": true
        },
        "resource": "execution",
        "operation": "get",
        "executionId": "={{ $json.execution_id }}",
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "id": "1SDBLwjifPzb02W8",
          "name": "n8n account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8cfd62f0-d3a6-4259-ae3f-471b31c177e6",
      "name": "当被其他工作流执行时",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -540,
        180
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "execution_id"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "6acea4e7-3fc9-4f11-8a2e-631ab187a66d",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -840,
        160
      ],
      "parameters": {
        "color": 7,
        "height": 180,
        "content": "主工作流调用子工作流后,您将能够在\"执行\"电子表格中看到总令牌数。"
      },
      "typeVersion": 1
    },
    {
      "id": "88253810-84bb-4640-9fb9-9da116d10f4f",
      "name": "分离输出",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        120,
        180
      ],
      "parameters": {
        "include": "allOtherFields",
        "options": {},
        "fieldToSplitOut": "tokenUsage"
      },
      "typeVersion": 1
    },
    {
      "id": "c91c32de-6357-47ee-abaf-fbf639362b1e",
      "name": "汇总令牌总数 - 按模型聚合",
      "type": "n8n-nodes-base.summarize",
      "position": [
        320,
        180
      ],
      "parameters": {
        "options": {},
        "fieldsToSplitBy": "id, name, tokenUsage.model, execution_id",
        "fieldsToSummarize": {
          "values": [
            {
              "field": "tokenUsage.tokenUsage.promptTokens",
              "aggregation": "sum"
            },
            {
              "field": "tokenUsage.tokenUsage.completionTokens",
              "aggregation": "sum"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "022cd2fc-3d1c-43fd-910e-02af46eeb4fa",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        740,
        -240
      ],
      "parameters": {
        "color": 7,
        "width": 660,
        "height": 580,
        "content": "## 限制"
      },
      "typeVersion": 1
    },
    {
      "id": "5a687ff7-2f31-4541-8d8c-245cb345de2b",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -840,
        -520
      ],
      "parameters": {
        "color": 7,
        "height": 120,
        "content": "这是一个AI代理示例。"
      },
      "typeVersion": 1
    },
    {
      "id": "6b96057d-150e-4a97-aff7-317c369af156",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1560,
        -620
      ],
      "parameters": {
        "color": 4,
        "width": 660,
        "height": 260,
        "content": "## 在哪里查找LLM定价?"
      },
      "typeVersion": 1
    },
    {
      "id": "18797895-4302-422c-b1a4-44a1e5e72e5c",
      "name": "记录令牌使用情况",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        540,
        180
      ],
      "parameters": {
        "columns": {
          "value": {
            "llm_model": "={{ $json.tokenUsage_model }}",
            "timestamp": "={{ $now.format('yyyy-MM-dd HH:mm:ss')}}",
            "workflow_id": "={{ $json.id }}",
            "execution_id": "={{ $json.execution_id }}",
            "input tokens": "={{ $json.sum_tokenUsage_tokenUsage_promptTokens }}",
            "workflow_name": "={{ $json.name }}",
            "completion tokens": "={{ $json.sum_tokenUsage_tokenUsage_completionTokens }}"
          },
          "schema": [
            {
              "id": "execution_id",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "execution_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "timestamp",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow_id",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "workflow_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow_name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "workflow_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "llm_model",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "llm_model",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "input tokens",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "input tokens",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "completion tokens",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "completion tokens",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "input price",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "input price",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "output price",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "output price",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "input cost",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "input cost",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "output cost",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "output cost",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "total cost",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "total cost",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1c9CeePI6ebNnIKogyJKHUpDWT6UEowpH9OwVtViadyE/edit#gid=0",
          "cachedResultName": "Executions"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1c9CeePI6ebNnIKogyJKHUpDWT6UEowpH9OwVtViadyE",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1c9CeePI6ebNnIKogyJKHUpDWT6UEowpH9OwVtViadyE/edit?usp=drivesdk",
          "cachedResultName": "[TEMPLATE] Calculate LLM Token Usage"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YR4pbjuZM5Xs4CTD",
          "name": "Google Sheets"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "b958bc0c-9142-4d1c-a72f-e05fb0f007c6",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1560,
        -880
      ],
      "parameters": {
        "color": 4,
        "width": 660,
        "height": 240,
        "content": "## 复制此表格文件"
      },
      "typeVersion": 1
    },
    {
      "id": "1f7fb4ae-ca87-484a-9b45-38d4a7f14c6d",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        740,
        -620
      ],
      "parameters": {
        "color": 7,
        "width": 400,
        "height": 200,
        "content": "# 需要帮助?"
      },
      "typeVersion": 1
    },
    {
      "id": "a4370528-00d7-4b04-8eb2-8aab6d5d77dc",
      "name": "便签10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1560,
        -340
      ],
      "parameters": {
        "color": 7,
        "width": 660,
        "height": 680,
        "content": "# 作者"
      },
      "typeVersion": 1
    },
    {
      "id": "17cf9314-b6a2-4abc-9223-846c08d53237",
      "name": "便签16",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1540,
        240
      ],
      "parameters": {
        "color": 4,
        "width": 620,
        "height": 80,
        "content": "### 💡 **想学习高级n8n技能并通过构建工作流赚钱吗?**"
      },
      "typeVersion": 1
    },
    {
      "id": "9cd12907-4b88-4bed-a997-53b7a0d07d12",
      "name": "便签7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2280,
        -880
      ],
      "parameters": {
        "width": 700,
        "height": 640,
        "content": "## 描述"
      },
      "typeVersion": 1
    },
    {
      "id": "51c0beee-ca42-4d3b-a725-1922a778cfcc",
      "name": "Gemini",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -400,
        -280
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.5-flash"
      },
      "credentials": {
        "googlePalmApi": {
          "id": "9iKgzeC7G4p9Z4Yu",
          "name": "Gemini API"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2e02c00e-375e-4fdb-adc5-0b49f6217cd1",
      "name": "OpenAI",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -320,
        -280
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "5LVOlVwHUgB8MAj2",
          "name": "OpenAI - n8n project"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "7dcc1116-ba80-4fad-89f9-ddae07a906e9",
      "name": "Anthropic",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "position": [
        -240,
        -280
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "claude-3-haiku-20240307",
          "cachedResultName": "Claude Haiku 3"
        },
        "options": {}
      },
      "credentials": {
        "anthropicApi": {
          "id": "Wibl5zrpbja5N3kH",
          "name": "Bau da Copy"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "e222998d-669c-47f6-9ddc-037a2b2c3166",
      "name": "便签8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -420,
        -160
      ],
      "parameters": {
        "color": 7,
        "width": 280,
        "height": 200,
        "content": "与上述所有提供商兼容良好。"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {
    "When Executed by Another Workflow": [
      {
        "execution_id": "594878"
      }
    ]
  },
  "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
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 工程

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量24
分类1
节点类型14
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Solomon

Solomon

@solomon

Teaching at the Scrapes Academy (see link below). For business inquiries: automations.solomon@gmail.com | Telegram: t.me/salomaoguilherme | LinkedIn: linkedin.com/in/guisalomao/

外部链接
在 n8n.io 查看

分享此工作流