Skip to main content

Track LLM token costs per customer using the Langchain code node

Workflow preview

Workflow preview
100%
Track LLM token costs per customer using the Langchain code node preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

Note: This template only works for self hosted n8n. This n8n te...

Best for

  • Engineering automation workflows
  • AI Summarization automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.googlesheetstool, n8n-nodes-base.set, n8n-nodes-base.formtrigger, @n8n/n8n-nodes-langchain.code, n8n-nodes-base.stickynote, n8n-nodes-base.form, n8n-nodes-base.filter, n8n-nodes-base.googlesheets

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Jimleuk.

Original n8n.io source

1.1 Workflow description

Title
Track LLM token costs per customer using the Langchain code node
Workflow name
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 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

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.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Client Usage Log

Type / Role
n8n-nodes-base.googleSheetsTool - googleSheetsTool
Config choices
Version 4.5

Block 2 - Logging Attributes

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 3 - On form submission

Type / Role
n8n-nodes-base.formTrigger - formTrigger
Config choices
Version 2.2

Block 4 - Custom LLM Subnode

Type / Role
@n8n/n8n-nodes-langchain.code - code
Config choices
Version 1

Block 5 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 6 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 7 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 8 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 9 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 10 - Display JSON Document

Type / Role
n8n-nodes-base.form - form
Config choices
Version 1

Block 11 - Filter Last Month

Type / Role
n8n-nodes-base.filter - filter
Config choices
Version 2.2

Block 12 - Get Client Logs

Type / Role
n8n-nodes-base.googleSheets - googleSheets
Config choices
Version 4.5

Block 13 - Calculate Totals

Type / Role
n8n-nodes-base.summarize - summarize
Config choices
Version 1.1

Block 14 - Every End of Month

Type / Role
n8n-nodes-base.scheduleTrigger - scheduleTrigger
Config choices
Version 1.2

Block 15 - Send Invoice

Type / Role
n8n-nodes-base.gmail - gmail
Config choices
Version 2.1

Block 16 - Sticky Note5

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 17 - Sticky Note6

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 18 - Sticky Note7

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 19 - Parse PDF Upload

Type / Role
n8n-nodes-base.extractFromFile - extractFromFile
Config choices
Version 1

Block 20 - Extract Resume Data

Type / Role
@n8n/n8n-nodes-langchain.informationExtractor - informationExtractor
Config choices
Version 1

3. Summary Table

Workflow Track LLM token costs per customer using the Langchain code node
Complexity advanced
Nodes 20
Categories Engineering, AI Summarization
Author Jimleuk
Published 05 Apr 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/3440/3440.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Track LLM token costs per customer using the Langchain code node do?

Note: This template only works for self hosted n8n. This n8n te...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, AI Summarization use case.