Use this essential n8n workflow to accurately track AI Agent token usage (prompt and completion tokens) across various models and log cost estimates in Google Sheets. This n8n templates solution is crucial for cost management.
Download this n8n workflow template and start using it instantly.
Managing the cost of AI Agents and large language models (LLMs) is crucial. Unlike simple API calls, complex n8n workflow designs involving AI Agents and tools can obscure the actual token consumption. This specialized n8n workflow provides a solution by tapping directly into the n8n execution metadata.
This powerful n8n templates solution utilizes a self-calling sub-workflow structure and the n8n API node to reliably extract token usage (prompt and completion tokens) from the execution data after the primary task is complete. The aggregated usage data is then recorded in a Google Sheets document, which is pre-configured with formulas to calculate estimated costs based on specific LLM pricing.
By deploying this n8n workflow, users gain unprecedented transparency into their AI operational expenses, helping them optimize their prompts and model choices.
This n8n workflow operates in two distinct phases: the main execution and the asynchronous tracking process.
$execution.id). This ensures the main execution fully logs its usage data before the tracking process begins.tokenUsage metrics.gpt-4o-mini).To set up this essential n8n workflow, follow these steps:
[TEMPLATE] Calculate LLM Token Usage).Record token usage) with your Google Sheets OAuth2 API credentials and select the copied spreadsheet and the 'Executions' sheet.Call sub-workflow n8n node. If you did not change the workflow ID after import, the expression ={{ $workflow.id }} should automatically reference the current n8n workflow. If you moved the tracking logic to a separate n8n template, update the ID accordingly. When clicking ‘Test workflow’ (n8n trigger): Serves as the initiation point for testing the n8n workflow.
AI Agent (LangChain Agent n8n node): Executes the main AI logic, simulating a task that involves interacting with language models.
Call sub-workflow (Execute Workflow n8n node): Calls the current n8n workflow asynchronously (waitForSubWorkflow: false), passing the execution ID. This is key for cost tracking in this n8n templates solution.
When Executed by Another Workflow (n8n trigger): The dedicated n8n trigger for the tracking branch, activated by the sub-workflow call, receiving the execution ID input.
Get execution data (n8n node - resource: execution): Retrieves the comprehensive execution log data from the n8n API, which contains the token usage metrics.
Extract token usage data (Set n8n node): Uses a complex JMESPath query to meticulously extract token usage (promptTokens and completionTokens) and the corresponding model_name from the deep structure of the n8n execution data.
Sum Token Totals - aggregate by model (Summarize n8n node): Aggregates the token counts, calculating the total prompt and completion tokens for each distinct LLM model used within the execution.
Record token usage (Google Sheets n8n node): Appends the final aggregated token usage data to the 'Executions' sheet in Google Sheets, enabling automatic cost calculation.
Use this advanced n8n workflow to track expenses instantly via Telegram (voice or text). It leverages AI agents and Google Sheets for smart financial management and analysis.

Use this n8n workflow to scrape sales data, analyze rep performance using an AI agent (OpenAI, Bright Data), and automatically save detailed metrics to Google Sheets. Find more powerful n8n templates here.

Use this robust n8n workflow to automatically track public holidays for distributed teams using the Nager.Date API. This powerful n8n template identifies shared holidays and sends real-time alerts to Slack while maintaining a master schedule in Notion.

Use this advanced n8n workflow to capture execution metrics, token usage, and performance data from your AI applications and send them automatically to LangFuse for deep observability. A powerful addition to your n8n templates collection.

Use this powerful n8n workflow to automatically track competitor pricing on Amazon. This n8n template leverages Apify scraping services to fetch prices and update Google Sheets daily, providing competitive intelligence.

Teaching at the Scrapes Academy (see link below). For business inquiries: [email protected] | Telegram: t.me/salomaoguilherme | LinkedIn: linkedin.com/in/guisalomao/







































