OpenAI to Google Sheets, track token spend per run
Your AI bill shows up and you have that moment of, “Wait… what happened this week?” Token usage gets scattered across runs, models, and experiments, and the only “report” is usually a vague dashboard or a pile of logs.
Marketing leads running lots of copy and creative tests feel this fast. So do agency owners shipping client work, and product teams trying to keep AI features profitable. This token spend tracking automation puts hard numbers in a spreadsheet per run, so you can see spikes before they turn into surprises.
You’ll learn what the workflow does, what you need to run it, how the pieces fit together in n8n, and where people usually get stuck.
How This Automation Works
Here’s the complete workflow you’ll be setting up:
n8n Workflow Template: OpenAI to Google Sheets, track token spend per run
flowchart LR
subgraph sg0["When chat message received Flow"]
direction LR
n3@{ icon: "mdi:cog", form: "rounded", label: "Execute Workflow", pos: "b", h: 48 }
n4@{ icon: "mdi:play-circle", form: "rounded", label: "When chat message received", pos: "b", h: 48 }
n5@{ icon: "mdi:robot", form: "rounded", label: "AI Agent", pos: "b", h: 48 }
n6@{ icon: "mdi:brain", form: "rounded", label: "gemini", pos: "b", h: 48 }
n7@{ icon: "mdi:brain", form: "rounded", label: "openai", pos: "b", h: 48 }
n8@{ icon: "mdi:memory", form: "rounded", label: "Simple Memory", pos: "b", h: 48 }
n6 -.-> n5
n7 -.-> n5
n5 --> n3
n8 -.-> n5
n4 --> n5
end
subgraph sg1["Start Flow"]
direction LR
n0["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/n8n.svg' width='40' height='40' /></div><br/>Get an execution"]
n1@{ icon: "mdi:play-circle", form: "rounded", label: "Start", pos: "b", h: 48 }
n2["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/code.svg' width='40' height='40' /></div><br/>map json"]
n1 --> n0
n0 --> n2
end
%% Styling
classDef trigger fill:#e8f5e9,stroke:#388e3c,stroke-width:2px
classDef ai fill:#e3f2fd,stroke:#1976d2,stroke-width:2px
classDef aiModel fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px
classDef decision fill:#fff8e1,stroke:#f9a825,stroke-width:2px
classDef database fill:#fce4ec,stroke:#c2185b,stroke-width:2px
classDef api fill:#fff3e0,stroke:#e65100,stroke-width:2px
classDef code fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
classDef disabled stroke-dasharray: 5 5,opacity: 0.5
class n4,n1 trigger
class n5 ai
class n6,n7 aiModel
class n8 ai
class n2 code
classDef customIcon fill:none,stroke:none
class n0,n2 customIcon
Why This Matters: Unpredictable AI spend
Token costs are sneaky because they don’t fail loudly. A prompt gets longer, a model changes, memory gets added, retries happen, and suddenly the same “feature” costs double. If you’re using OpenAI and Gemini in the same system, it’s even harder to tell what’s driving spend because each model reports usage a little differently and your usage is buried inside n8n execution details. The result is a finance-style problem landing on a marketing, ops, or product desk. And it usually lands late.
The friction compounds. Here’s where it breaks down in real life.
- You end up sampling a few executions manually, which misses the one runaway prompt that matters most.
- Model changes get rolled out quietly, and you only notice when the invoice looks “off.”
- Without a per-run log, it’s hard to tie cost to campaigns, clients, or features you actually care about.
- Even when you do track spend, totals without a model breakdown don’t tell you what to fix.
What You’ll Build: Per-execution token reporting into Google Sheets
This workflow acts like an “after action report” for any n8n run that used AI. It starts with an execution ID (the specific run you want to audit) plus a simple list of AI node names you care about, like OpenAI and Gemini. From there, it fetches the full execution details from n8n, searches through the execution data to find token usage fields, and then sums prompt tokens, completion tokens, and totals across the run. It also compiles the models that were actually used so you don’t have to guess which model drove the cost. Finally, you can push those totals and the per-model breakdown into Google Sheets for clean reporting and ongoing monitoring.
The workflow begins when it’s triggered at the end of another workflow (or triggered via a chat message, if you prefer). It pulls execution data, transforms it into a tidy JSON payload, and runs a sub-workflow that logs the results. Google Sheets becomes your living ledger, which means audits and cost reviews stop being detective work.
What You’re Building
| What Gets Automated | What You’ll Achieve |
|---|---|
|
|
Expected Results
Say you run a content pipeline that hits two AI models (OpenAI and Gemini) about 30 times a day. If you manually inspect executions, it’s maybe 10 minutes per run between opening logs, finding the right node, and copying totals, which can eat about 5 hours daily. With this workflow, you trigger it at the end of each run, and it logs to Google Sheets automatically. Your “work” becomes checking a sheet for spikes, which is closer to 5 minutes a day.
Before You Start
- n8n instance (try n8n Cloud free)
- Self-hosting option if you prefer (Hostinger works well)
- Google Sheets for storing per-run token logs.
- OpenAI node to generate AI outputs and token usage.
- Google Gemini node if you want cross-model tracking.
- Google credentials (create in n8n Credentials for Sheets)
Skill level: Intermediate. You’ll connect credentials, identify your AI node names, and wire this to run at the end of an existing workflow.
Want someone to build this for you? Talk to an automation expert (free 15-minute consultation).
Step by Step
Execution-triggered start. The workflow is designed to run after another workflow finishes, using an execution trigger so you can capture the exact run you want to measure.
Pull the execution record from n8n. It uses the execution_id to fetch execution details, which includes the data each node produced during that run (including token usage fields in AI nodes).
Transform and aggregate token usage. A code step reshapes the raw execution JSON into a consistent payload, then totals prompt, completion, and overall tokens while also listing the models found across OpenAI and Gemini nodes.
Log results through a sub-workflow. The workflow passes the cleaned metrics to an “Execute Sub-workflow” step, which is where you typically append a row to Google Sheets and keep your reporting standardized across projects.
You can easily modify which AI nodes are scanned to match your setup, or change the Google Sheets columns to include client name, campaign ID, or environment. See the full implementation guide below for customization options.
Step-by-Step Implementation Guide
Step 1: Configure the Triggers
Set up the workflow entry points so the automation can be launched by another workflow and by chat messages.
- Select Execution Start Trigger and confirm the workflow inputs include excecution_id and model_names (array) as defined.
- Select Chat Message Trigger and keep the default settings unless you need to change the webhook behavior for your chat channel.
- Confirm the execution flow matches: Execution Start Trigger → Fetch Execution Details and Chat Message Trigger → AI Orchestrator.
Step 2: Connect n8n Execution Data
Configure the node that retrieves execution details from your n8n instance.
- Open Fetch Execution Details and set Resource to
executionand Operation toget. - Set Execution ID to
={{ $('Execution Start Trigger').item.json.excecution_id }}. - Credential Required: Connect your n8nApi credentials.
Step 3: Set Up the JSON Processing Logic
Transform execution data into totals and model usage details for downstream automation.
- Open Transform JSON Payload and keep the provided JavaScript code as-is to aggregate token totals and model usage.
- Ensure the code references the trigger input array
{{ $('Execution Start Trigger').item.json.model_names }}to target the model nodes you want to analyze. - Verify the node is connected from Fetch Execution Details to ensure the input is the full execution payload.
excecution_id (misspelled). Keep this exact key in any calling workflow to avoid missing execution data.Step 4: Configure AI Orchestration and Sub-Workflow Handoff
Connect the agent to your chat models, memory, and sub-workflow execution.
- In AI Orchestrator, keep Needs Fallback enabled (
true) to allow multi-model fallback. - Attach Gemini Chat Model and OpenAI Chat Model as language models for AI Orchestrator.
- Credential Required: Connect your googlePalmApi credentials in Gemini Chat Model.
- Credential Required: Connect your openAiApi credentials in OpenAI Chat Model and confirm the model is set to
gpt-4.1-mini. - Connect Session Memory Buffer to AI Orchestrator for chat context. This is an AI memory sub-node—any credentials should be added to AI Orchestrator if required in your environment.
- Open Run Sub-Workflow (Configure Required) and set Workflow ID to the workflow you want to run.
- In Run Sub-Workflow (Configure Required), confirm Workflow Inputs are mapped as
model_namesto={{ ["Gemini Chat Model", "OpenAI Chat Model"] }}andexcecution_idto={{ $execution.id }}.
Step 5: Test and Activate Your Workflow
Validate that the workflow runs end-to-end and produces usage summaries.
- Use Chat Message Trigger to send a test message and confirm AI Orchestrator responds without errors.
- Run the workflow manually by executing Execution Start Trigger with sample inputs for
excecution_idandmodel_names. - Check the output of Transform JSON Payload for
totals,models, anddetailedUsagesto verify aggregation. - Once successful, toggle the workflow to Active so it can run in production.
Troubleshooting Tips
- Google Sheets credentials can expire or need specific permissions. If things break, check the credential in n8n’s Credentials section and confirm the connected Google account still has access to the target sheet.
- If you’re using Wait nodes or external rendering, processing times vary. Bump up the wait duration if downstream nodes fail on empty responses.
- Default prompts in AI nodes are generic. Add your brand voice early or you’ll be editing outputs forever.
Quick Answers
About 30 minutes if your Sheets access is ready.
No. You’ll mainly connect accounts and plug in your execution_id and AI node names.
Yes. n8n has a free self-hosted option and a free trial on n8n Cloud. Cloud plans start at $20/month for higher volume. You’ll also need to factor in OpenAI and Gemini usage costs, since token pricing depends on the models you call.
Two options: n8n Cloud (managed, easiest setup) or self-hosting on a VPS. For self-hosting, Hostinger VPS is affordable and handles n8n well. Self-hosting gives you unlimited executions but requires basic server management.
Yes, and you probably should. The key is the “model_names” array you pass in and the sub-workflow that writes to Google Sheets. If you rename your OpenAI or Gemini nodes, update that list so the scan finds the right usage fields. Common tweaks include adding a “client” column, logging environment (prod vs staging), and splitting rows by workflow name so reporting stays clean.
Usually it’s permissions. The Google account tied to your n8n credential must be able to edit the target spreadsheet, and the Sheet/Tab name has to match exactly what the node expects. If it worked before and suddenly doesn’t, re-check the credential in n8n and confirm Google hasn’t revoked access. Also make sure you’re not writing to a protected range.
Quite a lot, because it’s just reading execution data and logging rows. On n8n Cloud Starter, you can run a healthy number of monthly executions for small teams, and higher tiers handle more. If you self-host, you’re mainly limited by your server and how many executions you try to log at once. Practically, most teams log every run and only notice performance issues when Sheets writing becomes the bottleneck.
Often, yes. This workflow relies on pulling n8n execution details and then doing a bit of structured processing, which n8n handles comfortably with code steps, branching, and sub-workflows. Zapier and Make can do parts of it, but it’s clunkier to introspect n8n executions there, and costs rise quickly when you add steps and high volume. If your setup is already in n8n, keeping spend tracking in the same place is frankly simpler to maintain. Talk to an automation expert if you want a quick sanity check before you build.
Once token spend tracking is automatic, cost control becomes routine instead of reactive. You get clean numbers per run, and a lot fewer surprises.
Need Help Setting This Up?
Our automation experts can build and customize this workflow for your specific needs. Free 15-minute consultation—no commitment required.