Track AI Cost Per Client with n8n Langchain Code Node

This n8n workflow enables you to track and log AI token usage cost per client by extracting resume data from uploaded PDFs using Langchain’s custom LLM node, then storing costs in Google Sheets for accurate billing.
formTrigger
code
googleSheetsTool
+7
Workflow Identifier: 2102
NODES in Use: formTrigger, extractFromFile, set, informationExtractor, code, googleSheetsTool, googleSheets, filter, summarize, gmail
Track AI cost with n8n and Langchain

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What this workflow does

This workflow helps manage AI usage and billing for clients who submit resumes in PDF form. It extracts text from uploaded PDFs, converts the raw data into structured resume information, tracks AI token use per request, and logs all costs for billing. At month end, it totals clients’ usage and sends automatic invoices by email.

The workflow solves the problem of manual tracking and billing for AI service costs. It ensures accurate cost calculation per client and removes the risk of undercharging or losing money on AI usage.


Who should use this workflow

This workflow is built for small or growing services that use AI models to process resumes or documents. Users who want to automate billing by capturing AI token usage specifically per client will benefit most.

It fits well with businesses that receive resumes as PDF files and want a transparent way to extract data while tracking costs precisely.


Tools and services used

  • n8n automation platform: Runs the workflow and integrates other tools.
  • OpenAI API: Provides AI model for text extraction and processing.
  • Langchain integration in n8n: Manages AI calls and token usage tracking.
  • Google Sheets: Stores detailed logs of AI usage and cost per client.
  • Gmail service node: Sends monthly invoice emails to clients.


How this workflow works

Inputs

Clients upload a resume as a PDF file using the Form Trigger node, which collects the file and a billing confirmation.

Processing steps

  • The PDF is converted to text using the Extract from File node.
  • The Set node adds fixed workflow data and client ID to log this request.
  • The extracted text is passed to Langchain’s Information Extractor node, which outputs structured JSON resume data.
  • A custom Langchain Code node reads token usage metadata from the OpenAI response, calculates cost per token, and prepares a log row.
  • The usage and cost data are appended as a new row in Google Sheets using the connected Google Sheets Tool.
  • The extracted resume JSON is shown back to the client through a Form node configured as a completion screen.
  • On the last day each month, a Schedule Trigger node starts monthly aggregation of client logs.
  • Google Sheets are queried for logs matching the client and current month.
  • Token usage and costs are summed with a Summarize node.
  • An invoice email is sent via the Gmail node with the billing summary.

Output

Clients get structured resume data as JSON on submission. The service owner receives detailed usage logs per request. At month end, clients get invoices showing AI resource consumption and cost.


Beginner step-by-step: Using this workflow in n8n production

Step 1: Import the workflow

  1. Download the workflow file using the Download button on this page.
  2. Open the n8n editor where you want to use this workflow.
  3. Choose “Import from File” and upload the downloaded workflow file.

Step 2: Add credentials and API keys

  1. In n8n, open each node that needs credentials (OpenAI, Google Sheets, Gmail).
  2. Paste your OpenAI API key into the Langchain nodes configurations.
  3. Ensure Google Sheets nodes have proper OAuth2 credentials linked.
  4. Set Gmail credentials with OAuth2 access to send emails.

Step 3: Configure IDs and emails

  1. Update the client ID in the Set node if you want dynamic clients.
  2. Confirm the Google Sheet ID and sheet name are correct in the Google Sheets nodes.
  3. Set the recipient email address in the Gmail node.

Step 4: Test the workflow

  1. Submit a sample PDF resume through the form URL generated by the Form Trigger node.
  2. Check extracted data is shown and logged correctly in the Google Sheet.
  3. Review token usage and cost calculations in the sheet.

Step 5: Activate for production

  1. Turn on the workflow switch in n8n for automatic runs.
  2. Enable the monthly Schedule Trigger node to automate billing emails.

If hosting or running n8n on your server, see self-host n8n for help.


Inputs → Process → Output summary

Inputs: PDF resume files uploaded by clients plus billing consent.

Process: Convert PDF to text, extract resume details as JSON, track AI token usage and costs via Langchain Code node, and log details in Google Sheets. Monthly batch sums costs and triggers invoice emails.

Outputs: Structured resume JSON visible to clients on form completion and a detailed Google Sheets log of usage with cost per client. Monthly emailed invoices show precise AI service charges.


Potential issues and fixes

  • Google Sheets appends fail silently: Check sheet name, spreadsheet ID, and OAuth2 tokens for correctness.
  • Invoice emails not sent: Verify Gmail OAuth2 credential setup and client email addresses.
  • Information Extractor gives empty or error output: Ensure input text from PDF is non-empty and JSON schema format is exact.


Suggestions for customization

  • Make client IDs dynamic by linking them from the form input to support many clients.
  • Adjust per-token cost values in the Langchain Code node code to match current AI pricing.
  • Improve invoice email templates in the Gmail node with logos and payment links.
  • Add extra client info like emails or project numbers into the Google Sheets log.


Results and benefits

✓ Clients upload resumes easily and get structured feedback.

✓ AI usage is tracked precisely per client with token-level details.

✓ Billing is automated monthly with accurate cost calculation.

✓ Saves time by removing manual tracking work.

→ Reduces billing errors and lost revenue risks.

→ Supports scaling with many clients without extra effort.


Track AI cost with n8n and Langchain

Visit through Desktop to Interact with the Workflow.

Frequently Asked Questions

The workflow uses a custom Langchain Code node that reads token usage metadata from OpenAI responses and calculates costs. It then logs this data with client IDs into Google Sheets.
Yes. By changing the client ID in the Set node to a dynamic value from form input, the workflow can track usage separately for many clients.
After import, add OpenAI API key, Google Sheets OAuth2 credentials, Gmail OAuth2 credentials, correct client ID if needed, Google Sheets document and sheet names, and client email addresses for invoices.
Verify Gmail OAuth2 credentials in the Gmail node settings and confirm recipient email addresses are correct and valid.
Author
Written By
Ritu Sanjali

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.