8
n8n 中文网amn8n.com

使用Google Sheets自动追踪OpenAI管理API使用量和成本

高级

这是一个Document Extraction领域的自动化工作流,包含 40 个节点。主要使用 Set, Code, Merge, SplitOut, HttpRequest 等节点。 使用Google Sheets自动追踪OpenAI管理API使用量和成本

前置要求
  • 可能需要目标 API 的认证凭证
  • Google Sheets API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "f4f5d195bb2162a0972f737368404b18be694648d365d6c6771d7b4909d28167",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "fd7bb357-fc9e-4429-9450-d7abf83cab75",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -800,
        100
      ],
      "parameters": {},
      "typeVersion": 1.2
    },
    {
      "id": "de898eb7-5add-445d-a7c0-71c98b16e7bb",
      "name": "OpenAI 管理员 - 获取令牌使用量",
      "type": "n8n-nodes-base.httpRequest",
      "maxTries": 3,
      "position": [
        -320,
        -100
      ],
      "parameters": {},
      "retryOnFail": true,
      "typeVersion": 4.2,
      "waitBetweenTries": 5000
    },
    {
      "id": "e503041f-aa60-4e9d-bb23-6c7de54b67ba",
      "name": "OpenAI 管理员 - 获取成本",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -320,
        300
      ],
      "parameters": {},
      "retryOnFail": true,
      "typeVersion": 4.2,
      "waitBetweenTries": 5000
    },
    {
      "id": "0613b105-d505-4fd5-83f9-8f649d10429a",
      "name": "移除重复项",
      "type": "n8n-nodes-base.removeDuplicates",
      "position": [
        900,
        80
      ],
      "parameters": {},
      "typeVersion": 2
    },
    {
      "id": "4561e48e-d066-48eb-89b7-8ae6cc4e0b5d",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3160,
        -60
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "3a4dcd95-4291-4331-830f-53eb321ff5da",
      "name": "设置 api_key 和项目 ID",
      "type": "n8n-nodes-base.set",
      "position": [
        20,
        80
      ],
      "parameters": {},
      "executeOnce": true,
      "typeVersion": 3.4
    },
    {
      "id": "f145c7d5-1748-4b7a-8d38-742ee28dc961",
      "name": "设置 api_key ID 和名称",
      "type": "n8n-nodes-base.set",
      "position": [
        680,
        80
      ],
      "parameters": {},
      "executeOnce": false,
      "typeVersion": 3.4
    },
    {
      "id": "cfffda6e-609c-41d1-be95-affd050897fe",
      "name": "将 api_key 名称添加到令牌使用量",
      "type": "n8n-nodes-base.code",
      "position": [
        1440,
        20
      ],
      "parameters": {},
      "typeVersion": 2
    },
    {
      "id": "1f636c35-9ed1-49b7-8dcb-87b49b9e63c7",
      "name": "获取项目 ID",
      "type": "n8n-nodes-base.code",
      "position": [
        1880,
        500
      ],
      "parameters": {},
      "typeVersion": 2
    },
    {
      "id": "3dcd709c-4a17-4d86-97c4-30614e5bf702",
      "name": "合并令牌、使用量、项目",
      "type": "n8n-nodes-base.merge",
      "position": [
        2320,
        300
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "10c89cfa-3640-4d44-87b4-34dbb824f00e",
      "name": "合并令牌和使用量",
      "type": "n8n-nodes-base.merge",
      "position": [
        1640,
        280
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "dabe73c0-83c4-4c45-a30f-40f6ae26a227",
      "name": "构建使用量、成本、项目结构",
      "type": "n8n-nodes-base.set",
      "position": [
        2540,
        300
      ],
      "parameters": {},
      "executeOnce": true,
      "typeVersion": 3.4
    },
    {
      "id": "f23ad3dc-9953-4cae-b7d4-4e69e6b41a85",
      "name": "分离 api_key 和项目",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        240,
        80
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "97b5bf89-d83b-4f46-8757-e92694c80bb6",
      "name": "分离使用量",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        3200,
        120
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "bbc7e115-08e5-4f70-a939-c5439cfcb437",
      "name": "分离成本",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        3200,
        540
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "28f76584-beea-4490-b874-5386098d6488",
      "name": "分离使用量结果",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        3620,
        120
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "3409a348-0bb5-45f2-a445-63ffbabcdbc5",
      "name": "分离成本结果",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        3620,
        540
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "57502eaa-f55a-43db-be3d-f23e8d26a03e",
      "name": "构建使用量数据结构",
      "type": "n8n-nodes-base.set",
      "position": [
        3420,
        120
      ],
      "parameters": {},
      "typeVersion": 3.4
    },
    {
      "id": "eb42aece-486e-434a-a8a6-8961b16196ac",
      "name": "构建成本数据结构",
      "type": "n8n-nodes-base.set",
      "position": [
        3420,
        540
      ],
      "parameters": {},
      "typeVersion": 3.4
    },
    {
      "id": "73f256ea-42a0-4bf4-b8db-82d58b84f07d",
      "name": "将使用量追加到 Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        4120,
        120
      ],
      "parameters": {},
      "typeVersion": 4.6
    },
    {
      "id": "b937e858-f2bf-4a6a-b0f3-be440175f0b5",
      "name": "将成本追加到 Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        4120,
        540
      ],
      "parameters": {},
      "typeVersion": 4.6
    },
    {
      "id": "f71dccd7-072a-4edd-bb68-960f67cc8c16",
      "name": "设置 Google Sheets 使用量数据",
      "type": "n8n-nodes-base.set",
      "position": [
        3840,
        120
      ],
      "parameters": {},
      "typeVersion": 3.4
    },
    {
      "id": "8e6c86be-fd65-4daf-8268-e8802910d24b",
      "name": "设置 Google Sheets 成本数据",
      "type": "n8n-nodes-base.set",
      "position": [
        3840,
        540
      ],
      "parameters": {},
      "typeVersion": 3.4
    },
    {
      "id": "98445cae-0371-4148-85c7-6937b5afc738",
      "name": "便签12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1960,
        -560
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "0b08c6b6-37ed-4961-9827-e4175453ccad",
      "name": "便签13",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1960,
        -240
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "0c1361b9-ef18-4509-ad08-75b62456eaa7",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -900,
        -60
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "ca64b8cc-22d0-49c2-a03b-5076504b1b78",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -400,
        -280
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "e4e52422-df6c-4d9a-b535-6ad65fb17bbb",
      "name": "合并使用量数据",
      "type": "n8n-nodes-base.merge",
      "position": [
        1180,
        -80
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "03d0816f-3add-4863-abe7-1a1e5390caf2",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -40,
        -20
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "09896795-a670-482d-ae56-2e51b3bff815",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1360,
        -80
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "b60b1a83-9f57-4a3c-be0f-20b244c5cb10",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1840,
        400
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "8b5395dc-186e-4427-9cdc-9fe82061ae62",
      "name": "便签7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2500,
        220
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "79a58f71-b28d-4749-a249-f81c730bcf3d",
      "name": "在成本和用量对象中添加项目名称",
      "type": "n8n-nodes-base.code",
      "position": [
        2760,
        300
      ],
      "parameters": {},
      "typeVersion": 2
    },
    {
      "id": "aa4fa236-2d0c-4039-809f-b9c4c083bbbf",
      "name": "便签8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        4040,
        -60
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "b87c70be-4673-4fa8-bccc-97f7d1287ee9",
      "name": "便签9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3160,
        360
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "0d47dc46-8d55-41ea-a69e-311520cd0e59",
      "name": "便签10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        4040,
        360
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "7dcd9dc7-c1b7-4de9-82d1-4e0231f4a13c",
      "name": "便签11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        4040,
        -180
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "c239893f-414d-4afa-8b8f-5f2325e30aa6",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1960,
        880
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "218ca015-bdab-42ea-85e4-bcacfa83ea54",
      "name": "OpenAI 管理员 - 获取项目详情",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2060,
        500
      ],
      "parameters": {},
      "typeVersion": 4.2
    },
    {
      "id": "ad1c1fea-69a9-424d-b92a-3e9d9945618f",
      "name": "OpenAI 管理员 - 获取 API 密钥详情",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        460,
        80
      ],
      "parameters": {},
      "retryOnFail": true,
      "typeVersion": 4.2,
      "waitBetweenTries": 5000
    }
  ],
  "pinData": {},
  "connections": {
    "Split Out Cost": {
      "main": [
        [
          {
            "node": "Structure Cost data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get project_ids": {
      "main": [
        [
          {
            "node": "OpenAI Admin - Get Project details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out Usage": {
      "main": [
        [
          {
            "node": "Structure Usage data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Usage data": {
      "main": [
        [
          {
            "node": "Add api_key name to token usage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "OpenAI Admin - Get cost",
            "type": "main",
            "index": 0
          },
          {
            "node": "OpenAI Admin - get token usage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Remove Duplicates": {
      "main": [
        [
          {
            "node": "Merge Usage data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Structure Cost data": {
      "main": [
        [
          {
            "node": "Split Out Cost results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structure Usage data": {
      "main": [
        [
          {
            "node": "Split Out Usage Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge token and usage": {
      "main": [
        [
          {
            "node": "Get project_ids",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge token, usage, projects",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out Cost results": {
      "main": [
        [
          {
            "node": "Set Cost data for Gsheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Admin - Get cost": {
      "main": [
        [
          {
            "node": "Merge token and usage",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Set api_key id and name": {
      "main": [
        [
          {
            "node": "Remove Duplicates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out Usage Results": {
      "main": [
        [
          {
            "node": "Set Usage data for Gsheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Cost data for Gsheets": {
      "main": [
        [
          {
            "node": "Append Cost to GSheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Usage data for Gsheets": {
      "main": [
        [
          {
            "node": "Append Usage to GSheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set api_key and project ids": {
      "main": [
        [
          {
            "node": "Split Out api_key and project",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge token, usage, projects": {
      "main": [
        [
          {
            "node": "Structure usage, cost, projects",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out api_key and project": {
      "main": [
        [
          {
            "node": "OpenAI Admin - Get API Key details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Admin - get token usage": {
      "main": [
        [
          {
            "node": "Set api_key and project ids",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge Usage data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add api_key name to token usage": {
      "main": [
        [
          {
            "node": "Merge token and usage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structure usage, cost, projects": {
      "main": [
        [
          {
            "node": "Add Project name in cost and usage objects",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Admin - Get API Key details": {
      "main": [
        [
          {
            "node": "Set api_key id and name",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Admin - Get Project details": {
      "main": [
        [
          {
            "node": "Merge token, usage, projects",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Add Project name in cost and usage objects": {
      "main": [
        [
          {
            "node": "Split Out Usage",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split Out Cost",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 文档提取

需要付费吗?

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

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

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

作者
Agent Studio

Agent Studio

@agentstudio

We are a product studio that helps organizations leverage no-code and generative AI to automate internal processes and launch new digital products. LinkedIn: https://www.linkedin.com/in/baptistej/

外部链接
在 n8n.io 查看

分享此工作流