Track AI agent token usage and estimate costs in Google Sheets
Needs changesWhat this workflow does
This AI workflow has 13 nodes and runs manually, when you click “Execute workflow” (plus 1 other trigger). Along the way it connects Google Sheets, Google Gemini Chat Model, OpenAI Chat Model and Anthropic Chat Model.
How it runs
Trigger: Manually, when you click “Execute workflow” (plus 1 other trigger).
- AI Agent: runs an AI agent, with model: OpenAI; 1 tool (Think)
- Get execution data (n8n): get (execution)
- Call sub-workflow (Execute Sub-workflow): calls another n8n workflow
- Extract token usage data (Edit Fields (Set)): sets 2 fields: execution_id, tokenUsage
- Split Out: splits a list into separate items
- Sum Token Totals - aggregate by model (Summarize): summarizes / groups the items
- Record token usage (Google Sheets): append
- Gemini (Google Gemini Chat Model): uses Google Gemini Chat Model
- Anthropic (Anthropic Chat Model): uses Anthropic Chat Model
Services it uses
Before you upgrade to n8n 3.0
1 thing in this workflow is removed in n8n 3.0. Fix it before you upgrade:
- AI Agent v1 (old agent modes) in AI Agent (v1.8): replace with Latest AI Agent node (Tools Agent). For SQL Agent, use a Postgres or MySQL tool with a recent AI Agent.
Rather not rewrite it? Many of these flows can be rebuilt in Make (free plan).
| Removed in 3.0 | Replace with |
|---|---|
| AI Agent v1 (old agent modes) AI Agent (v1.8) | Latest AI Agent node (Tools Agent). For SQL Agent, use a Postgres or MySQL tool with a recent AI Agent. |
Nodes used (13)
- Manual Trigger
n8n-nodes-base.manualTrigger - AI Agent
@n8n/n8n-nodes-langchain.agent - Execute Sub-workflow
n8n-nodes-base.executeWorkflow - Think Tool
@n8n/n8n-nodes-langchain.toolThink - Edit Fields (Set)
n8n-nodes-base.set - n8n
n8n-nodes-base.n8n - Execute Workflow Trigger
n8n-nodes-base.executeWorkflowTrigger - Split Out
n8n-nodes-base.splitOut - Summarize
n8n-nodes-base.summarize - Google Sheets
n8n-nodes-base.googleSheets - Google Gemini Chat Model
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - OpenAI Chat Model
@n8n/n8n-nodes-langchain.lmChatOpenAi - Anthropic Chat Model
@n8n/n8n-nodes-langchain.lmChatAnthropic
From the author
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. Obtaining the token usage from AI Agents is tricky, because it doesn't provide all the data…