Tracking LLM Token Costs per Customer Using the Langchain Code Node
 *Note: This template only works for self-hosted n8n.* ### This n8n template demonstrates how to use the Langchain code node to track token usage and cost for every LLM call. This is useful if your templates handle multiple clients or customers and you need a cheap and easy way to capture how much of your AI credits they are using. ### How it works - In our mock AI service, we're offering a data conversion API to convert Resume PDFs into JSON documents. - A form trigger is used to allow for PDF upload and the file is parsed using the Extract from File node. - An Edit Fields node is used to capture additional variables to send to our log. - Next, we use the Information Extractor node to organize the Resume data into the given JSON schema. - The LLM subnode attached to the Information Extractor is a custom one we've built using the Langchain Code node. - With our custom LLM subnode, we're able to capture the usage metadata using lifecycle hooks. - We've also attached a Google Sheet tool to our LLM subnode, allowing us to send our usage metadata to a Google Sheet. - Finally, we demonstrate how you can aggregate from the Google Sheet to understand how much AI tokens/costs your clients are liable for. **Check out the example Client Usage Log** - [https://docs.google.com/spreadsheets/d/1AR5mrxz2S6PjAKVM0edNG-YVEc6zKL7aUxHxVcffnlw/edit?usp=sharing](https://docs.google.com/spreadsheets/d/1AR5mrxz2S6PjAKVM0edNG-YVEc6zKL7aUxHxVcffnlw/edit?usp=sharing) ### How to use - **SELF-HOSTED N8N ONLY** - The Langchain Code node is only available in the self-hosted version of n8n. It is not available in n8n cloud. - The LLM subnode can only be attached to non-AI agent nodes; Basic LLM node, Information Extractor, Question & Answer Chain, Sentiment Analysis, Summarization Chain, and Text Classifier. ### Requirements - Self-hosted version of n8n - OpenAI for LLM - Google Sheets to store usage metadata ### Customizing this template - Bring the custom LLM subnode into your own templates! In many cases, it can be a drop-in replacement for the regular OpenAI subnode. - Not using Google Sheets? Try other databases or an API call to pipe into your CRM.
New buyers can create an account from the cart to unlock a controlled $10 first-purchase credit on eligible orders of $25+.
Related bundle
Content Repurposing Engine
8 hand-picked workflows for $29.00.
That is $3.63 each, vs $14.99 for this one alone.
View bundleSecure checkout powered by Stripe
Support
How to import this workflow into n8n
- 1Purchase or download the workflow to get the n8n workflow JSON file.
- 2In your n8n instance, open Workflows and choose "Import from File" (or paste the JSON with Ctrl+V on the canvas).
- 3Open each node marked with a credential warning and connect your own accounts and API keys.
- 4Run the workflow once manually to verify the data flow, then toggle it to Active.
Related AI workflows
- Launch Your First AI-Powered Chatbot with Actionable Tools$9.99
- Automate AI Video Creation and YouTube Upload with Google Sheets$14.99
- Build a WhatsApp Assistant with Memory, Google Suite, Multi-AI, Research, and Imaging$24.99
- Automate Blog Post Creation and Publishing with GPT, Leonardo AI, and WordPress$14.99
- Automate SEO Keyword Generation with ChatGPT from Google Sheets$3.99
- Email Agent$500.99
More from Jimleuk
Need this deployed? We'll set it up for you.
Our automation experts deploy this workflow in your stack, connect your accounts, and verify it works — or build a custom solution from scratch.