Track 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...
🔌 Nodes & Integrations Used (8)
📖 Implementation & Setup Guide
*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 organise 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
Customising 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 a HTTP call to pipe into your CRM.
⚡ How to import this workflow into n8n:
- Click the Direct Download JSON button above to save
Track_LLM_token_costs_per_customer_using_the_Langchain_code_node.json. - Log in to your n8n Cloud or self-hosted n8n instance.
- In your workflow canvas, click the menu in top-right and select Import from File (or paste the JSON directly).
- Configure your credentials (e.g. API keys, database URLs) in the corresponding node settings.
- Click Execute Workflow to test and switch to Active!
Related Email & Outreach Workflows
View All in Email & Outreach →Abandoned cart recovery for Shopify via Gmail Google Sheets T
Who is this for? This template is ideal for solo store owners, eCommerce marketers, automation beginners, or anyone using Shopify and Gmail who wants to reco...
Add new leads in Lemlist from Airtable
This workflow allows you to add new leads in Lemlist from Airtable. !workflow-screenshot Airtable node: This node lists all the emails that are stored in you...
Add Subscriber and Send Email Campaign
Production-ready n8n automation template for Add Subscriber and Send Email Campaign built by AhiXLight. Seamlessly connects Sendy with 3 configured nodes.
Add new contacts from HubSpot to the email list in Mailchimp
This workflow adds new HubSpot contacts to the Mailchimp email list. Prerequisites - HubSpot account and HubSpot credentials - Mailchimp account and Mailchim...