LLM Token Usage and Cost Tracking for Client Billing - n8n Workflow

Use this advanced n8n workflow to accurately track OpenAI token usage and cost per client using the Langchain Code n8n node, enabling automated billing via Google Sheets and Gmail.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

AI Service Providers: Companies offering AI-powered APIs or services to multiple customers. Developers using n8n: Users looking for advanced examples of how to hook into the LLM lifecycle for custom metrics. Finance Teams: Organizations needing granular cost attribution for generative AI usage. Self-Hosted n8n Users: This particular n8n templates uses the Langchain Code n8n node, which is optimized for self-hosted n8n environments.

Overview

Managing the costs associated with Large Language Models (LLMs) is crucial, especially when servicing multiple clients. This comprehensive n8n workflow addresses the challenge of accurately tracking and attributing token usage and financial cost to individual customers.

The core of this n8n automation is the use of a custom LLM subnode built with the Langchain Code n8n node. This allows the workflow to tap into the LLM response metadata before the data is passed to the next step, ensuring every token used is captured. The captured metrics—including input tokens, output tokens, and calculated cost—are immediately logged to Google Sheets using a specialized 'tool' connection. This log forms the basis for accurate, automated monthly billing, which is handled in a secondary, scheduled part of the n8n workflow that retrieves logs, aggregates totals, and uses the Gmail n8n node to send a finalized invoice.

How it Works

This powerful n8n workflow is divided into two main logical streams: the usage tracking service and the scheduled billing process.

Usage Tracking Stream


  1. Form Trigger: The n8n workflow starts with the 'On form submission' n8n trigger when a client uploads a PDF file (e.g., a CV) seeking data extraction. This is the entry point for the AI service.

  2. Data Preparation: The 'Parse PDF Upload' n8n node extracts the text content from the binary file. The 'Logging Attributes' n8n node then sets crucial client context variables (like clientid, workflowid, and executionid).

  3. Custom LLM Execution: The 'Extract Resume Data' node, an Information Extractor, is configured to use a custom LLM rather than a standard OpenAI connection. This custom LLM is defined by the 'Custom LLM Subnode'.

  4. Token Logging (The Core Logic): The 'Custom LLM Subnode' is a Langchain Code n8n node. This custom n8n node intercepts the LLM completion response using a handleLLMEnd callback. Inside this custom code, the usagemetadata (containing token counts) is accessed, costs are calculated using predefined rates, and the entire transaction record is appended to the Google Sheets 'Client Usage Log' using the Google Sheets Tool connected to the subnode.

  5. Result Delivery: The final structured JSON output from the data extractor is displayed back to the client using the 'Display JSON Document' form n8n node.

Scheduled Billing Stream


  1. Schedule Trigger: The 'Every End of Month' n8n trigger initiates the billing process on the last day of the month.

  2. Data Retrieval: The 'Get Client Logs' Google Sheets n8n node retrieves all log entries for the specified client ID.

  3. Filtering: The 'Filter Last Month' n8n node narrows down the records to only those created in the current billing cycle.

  4. Aggregation: The 'Calculate Totals' n8n node summarizes the data, providing a total token count (sumtotaltokens) and total cost (sumtotalcost).

  5. Invoicing: Finally, the 'Send Invoice' Gmail n8n node uses the aggregated cost data to dynamically generate and send a detailed monthly invoice.

Installation Guide

To deploy this token cost tracking n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON data and paste it directly into your n8n instance using the 'New' button and selecting 'Import from JSON'.

  2. Self-Hosted Requirement: Note that the critical 'Langchain Code' n8n node is currently available only in self-hosted versions of n8n.

  3. Google Sheets Setup:

Create a Google Sheet titled 'Client Usage Log' (or update the name in the credentials).
Ensure the sheet contains the exact column headers required by the 'Client Usage Log' n8n node (e.g., date, clientid, totaltokens, totalcost, etc.).
Set up a Google Sheets OAuth2 credential and connect it to both the 'Client Usage Log' and 'Get Client Logs' n8n node.

  1. OpenAI Configuration: Edit the 'Custom LLM Subnode' Langchain Code n8n node. You must update:

openAIApiKey: Replace the placeholder with your actual OpenAI API key.
model: Adjust the model name if necessary (it currently uses gpt-4o-mini).
input
tokencost and outputtoken_cost: Update these variables to reflect the current market cost per million tokens for your chosen model.

  1. Gmail Setup: Configure a Gmail credential and connect it to the 'Send Invoice' n8n node. Update the 'sendTo' parameter with the correct client email address.

  2. Activate: Save and activate the n8n workflow.

Node Details

On form submission (n8n trigger):
Function: Serves as the client-facing front end, allowing file upload to trigger the service.
Key Configuration: Configured to accept a required PDF file upload and an acknowledgment checkbox.
Custom LLM Subnode (Langchain Code n8n node):
Function: Acts as the custom LLM provider for the data extractor. It contains Javascript logic to listen for the LLM response completion event (handleLLMEnd) and extract token usage metadata.
Key Configuration: Calculates costs based on hardcoded per-million-token rates and calls the connected Google Sheets tool to log the transaction data.
Client Usage Log (Google Sheets Tool n8n node):
Function: Used as a 'tool' connection to the custom LLM subnode, allowing the custom code to append usage data to a specific spreadsheet instantaneously.
Key Configuration: Operation set to 'append'; targets the spreadsheet '89. Langchain Code Node - Client Usage Log'.
Extract Resume Data (Information Extractor n8n node):
Function: Uses the custom LLM to convert raw text (from the PDF) into a structured JSON schema (for CV/Resume data).
Key Configuration: Uses a detailed JSON schema defining fields like name, email, work history, and skills.
Every End of Month (Schedule n8n trigger):
Function: Initiates the financial aggregation and invoicing process monthly.
Key Configuration: Scheduled to run on the 31st of the month at 18:00.
Calculate Totals (Summarize n8n node):
Function: Aggregates the costs and tokens from the retrieved client logs.
Key Configuration: Summarizes the sum of totalcost and totaltokens.
Send Invoice (Gmail n8n node):
Function: Composes and sends the final billing email to the client.
* Key Configuration: Email body dynamically constructed using the aggregated totals from the previous n8n node, including tax calculations.

Related n8n Workflows

Free

Nodes: 13 Nodes
Updated: December 26 2025
View all
Created by
Jimleuk
Jimleuk

Freelance consultant based in the UK specialising in AI-powered automations. I work with select clients tackling their most challenging projects. For business enquiries, send me an email at [email protected] LinkedIn: https://www.linkedin.com/in/jimleuk/ X/Twitter: https://x.com/jimle_uk

Featured*