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

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

Learn how to Build this Workflow with AI:

Visit through Desktop for Best experience

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.


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.

Promoted by BULDRR AI

Related Workflows

Automate Viral UGC Video Creation Using n8n + Degaus (Beginner-Friendly Guide)

Learn how to automate viral UGC video creation using n8n, AI prompts, and Degaus. This beginner-friendly guide shows how to import, configure, and run the workflow without technical complexity.
Form Trigger
Google Sheets
Gmail
+37
Free

AI SEO Blog Writer Automation Workflows in n8n

A complete beginner guide to building an AI SEO blog writer automation using n8n.
AI Agent
Google Sheets
httpRequest
+5
Free

Automate CrowdStrike Alerts with VirusTotal, Jira & Slack

This workflow automates processing of CrowdStrike detections by enriching threat data via VirusTotal, creating Jira tickets for incident tracking, and notifying teams on Slack for quick response. Save hours daily by transforming complex threat data into actionable alerts effortlessly.
scheduleTrigger
httpRequest
jira
+5
Free

Automate Telegram Invoices to Notion with AI Summaries & Reports

Save hours on financial tracking by automating invoice extraction from Telegram photos to Notion using Google Gemini AI. This workflow extracts data, records transactions, and generates detailed spending reports with charts sent on schedule via Telegram.
lmChatGoogleGemini
telegramTrigger
notion
+9
Free

Automate Email Replies with n8n and AI-Powered Summarization

Save hours managing your inbox with this n8n workflow that uses IMAP email triggers, AI summarization, and vector search to draft concise replies requiring minimal review. Automate business email processing efficiently with AI guidance and Gmail integration.
emailReadImap
vectorStoreQdrant
emailSend
+12
Free

Automate Email Campaigns Using n8n with Gmail & Google Sheets

This n8n workflow automates personalized email outreach campaigns by integrating Gmail and Google Sheets, saving hours of manual follow-up work and reducing errors in email sequences. It ensures timely follow-ups based on previous email interactions, optimizing communication efficiency.
googleSheets
gmail
code
+5
Free