AI Model Token Usage and Cost Tracking Sub-Workflow - n8n Workflow

Use this modular n8n workflow template to accurately track and calculate the cost of AI model token consumption (like JinaAI or OpenAI). Logs execution details directly into Google Sheets for financial monitoring.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Automation Developers seeking reliable cost monitoring for large language model (LLM) usage.
Teams needing to implement granular financial tracking for generative AI services.
Users who leverage n8n for complex AI pipelines and require a dedicated cost tracking module.
Anyone looking for reusable n8n templates for accounting and operations.

Overview

Managing the costs associated with frequent AI model interactions is crucial. This specialized n8n workflow template is designed to operate as a modular sub-workflow, automatically retrieving execution data from a parent n8n workflow, extracting token usage, fetching current pricing, calculating the total expense, and persistently logging the data. This solution solves the problem of opaque AI usage costs by providing a transparent audit trail in Google Sheets. Every run of your primary AI n8n workflow can call this specific n8n workflow, ensuring accurate and continuous financial oversight. The core logic relies on using the n8n node dedicated to fetching execution data, providing a robust mechanism for data exchange between workflows.

How it Works

This n8n workflow starts in one of two ways: either manually via the 'When clicking ‘Test workflow’' n8n trigger for setup, or automatically via the 'When Executed by Another Workflow' n8n trigger, indicating its use as a sub-workflow component.


  1. Retrieve Execution Data: The 'n8n Get Execution Data' n8n node captures the runtime data passed from the initiating n8n workflow, which contains the critical usage metrics.

  2. Extract Usage Metrics: A custom 'Code' n8n node, 'Get AI Usage Data', processes this execution data, specifically extracting the input and output token counts and identifying the model used.

  3. Data Preparation: The 'Set AiRunData' n8n node standardizes the extracted metrics into a usable JSON format for subsequent calculations.

  4. Fetch Pricing: An 'HTTP Request' n8n node, 'Get AI Pricing', calls an external endpoint to retrieve the latest pricing information for the identified AI model.

  5. Calculate Cost: A second 'Code' n8n node, 'Get Models Price and Add Summary', performs the core cost calculation by multiplying the token counts by the fetched pricing data, generating the final cost summary.

  6. Log Results: Finally, the 'Google Sheets' n8n node appends the comprehensive usage summary (model, tokens, cost) to a designated spreadsheet, completing the logging cycle of this sophisticated n8n template.

Installation Guide


  1. Import: Download the provided JSON and import it into your n8n instance via the 'Workflows' section by clicking 'New' and then 'Import from JSON'. This n8n workflow is ready to be utilized.

  2. Google Sheets Credential Setup: Click the 'Google Sheets' n8n node and set up or select your Google Sheets credential. Configure the node to point to the specific Spreadsheet ID and Sheet Name where you wish to log the token usage data.

  3. HTTP Request Configuration (Pricing): Update the 'Get AI Pricing' HTTP Request n8n node with the correct URL endpoint to your pricing data source (this JSON assumes an external data source).

  4. Sub-Workflow Integration: To use this as a cost tracker, ensure your main n8n workflow executes this template using the 'Execute Workflow' n8n node, passing the AI usage results as input data. The 'When Executed by Another Workflow' n8n trigger will handle the incoming payload.

Node Details

When Executed by Another Workflow (n8n trigger): The primary n8n trigger for modular use. It activates the n8n workflow when called by another execution, passing relevant usage data.
n8n Get Execution Data (n8n node): A specialized n8n node essential for sub-workflows. It retrieves the data payload and context from the parent n8n workflow execution.
Get AI Usage Data (Code n8n node): Contains custom JavaScript logic to dynamically parse the execution data, isolating metrics like prompttokens and completiontokens required for cost tracking.
Get AI Pricing (HTTP Request n8n node): Responsible for fetching real-time model pricing data necessary for the cost calculation step in this n8n template.
Get Models Price and Add Summary (Code n8n node): Executes the final financial logic, calculating the total dollar cost of the run based on the usage data and external pricing, preparing the output for the Google Sheets n8n node.
Google Sheets (n8n node): The final action. This n8n node appends the calculated usage summary (including model name, tokens, and cost) to a specified Google Sheet row, providing an auditable log of every run of the primary n8n workflow.

Related n8n Workflows

Paid

Nodes: 8 Nodes
Updated: December 26 2025
View all

Featured*