LLM Token Usage and AI Agent Observability Tracker via Google Sheets - n8n Workflow

Use this powerful n8n workflow to track OpenAI token consumption, calculate costs, and monitor AI agent observability metrics, logging all data automatically to Google Sheets. Ideal for cost management.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • AI Developers: Who need precise cost monitoring for their production-level LangChain agents.

  • Operations Teams: Responsible for tracking LLM expenditure and generating usage reports.

  • n8n Automation Specialists: Looking for advanced examples of integrating custom LangChain callbacks within an n8n workflow.

  • Startups and Businesses: Running customer-facing AI applications that require detailed observability dashboards.

Overview

Managing the operational costs of large language models (LLMs) is crucial. This advanced n8n workflow provides a robust solution for tracking every interaction of your AI agent. By leveraging custom LangChain callbacks within an n8n node, the automation captures real-time data on input tokens, output tokens, and computes the exact monetary cost using your specified pricing rates. This detailed usage data is then instantly appended to a Google Sheet dashboard. Furthermore, the n8n workflow ensures high observability by logging when the AI agent utilizes internal tools, giving you a complete view of agent behavior. This n8n template is essential for anyone seeking transparent cost management and performance insights into their AI operations.

How it Works

The entire process begins with the n8n trigger, the When chat message received n8n node, which activates upon a user interaction in a chat interface.


  1. Metadata Capture: The flow immediately hits the Set • Workflow + Client Metadata n8n node, which dynamically captures the workflowid, executionid, and a custom client_id for traceability.

  2. LLM Configuration and Callback: The LangChain Chat Model + Token Callback n8n code node takes center stage. It uses the input from the subsequent Google Sheets Tool n8n node and configures the LangChain ChatOpenAI model. Crucially, it sets up a custom handleLLMEnd callback. This callback executes the cost calculation (based on predefined token prices) immediately after the LLM generates a response.

  3. Token Logging (Tool): The callback function then calls the Token Usage Log n8n node, configured as an AI Tool. This ensures synchronous logging of the date, model, token counts, and computed costs to a dedicated 'Token cost tracker' sheet in Google Sheets.

  4. Agent Execution: The AI Agent n8n node processes the original chat input using the configured, instrumented LLM.

  5. Observability Logging: The Branch • Tool Used? n8n node checks if the AI agent utilized any tools during its processing (checking for non-empty intermediateSteps).

  6. Tool Use Logging: If tools were used, the n8n workflow proceeds to the Log • Token Metrics to Sheets (Tool) n8n node, which records the input, final output, and the specific tool used to a separate 'Observability' sheet, completing the detailed audit trail provided by this powerful n8n workflow.

Installation Guide

To deploy this comprehensive n8n template, follow these steps:


  1. Import the n8n workflow: Copy the provided JSON data and paste it into your n8n instance via the 'New' menu -> 'Import from JSON'.

  2. Environment Variables: Ensure your LLM provider API key (e.g., OPENAIAPIKEY) is set as an environment variable in your n8n instance.

  3. Google Sheets Credentials: Create or select an existing Google Sheets OAuth2 credential and apply it to both Google Sheets n8n node instances.

  4. Configuration Check:

In the Set • Workflow + Client Metadata n8n node, customize the static clientid if necessary.
In the LangChain Chat Model + Token Callback n8n node (Code), update the model, input
tokencost, and outputtoken_cost variables to accurately reflect your LLM provider's current pricing.
* In both Google Sheets n8n node instances (Token Usage Log and Log • Token Metrics to Sheets (Tool)), select your Google Spreadsheet ID and ensure the Sheet names correspond to your desired metrics and observability tabs.

  1. Activate: Set the n8n workflow to 'Active' to start monitoring token usage when the trigger fires.

Node Details


  • When chat message received (Chat Trigger n8n node):

- Function: The starting n8n trigger for the entire automation, initiating the sequence whenever a chat input is received.
- Key Configuration: Public webhook enabled; configured with an initial welcome message.

  • Set • Workflow + Client Metadata (Set n8n node):

- Function: Captures essential contextual data, dynamically setting workflowid and executionid, and assigning a hardcoded client_id (customizable).

  • LangChain Chat Model + Token Callback (Code n8n node):

- Function: Defines the specific LLM (ChatOpenAI) and implements a custom LangChain callback (handleLLMEnd) that executes token counting and cost calculation before proceeding. This is a critical component of the n8n workflow structure.
- Key Configuration: Imports ChatOpenAI; hardcoded variables for model and per-million token costs; uses the input Google Sheets n8n node as a logging tool within the callback.

  • Token Usage Log (Google Sheets Tool n8n node):

- Function: Acts as the dedicated tool for logging calculated token metrics (date, costs, token counts) to a Google Sheet (Token cost tracker). It's called asynchronously by the custom callback.
- Key Configuration: Operation set to append; maps all computed token and cost data fields.

  • AI Agent (Agent n8n node):

- Function: Executes the core reasoning loop based on the chat input and the LLM provided by the LangChain Chat Model n8n node.
- Key Configuration: Uses dynamic text input; includes systemMessage for context; returnIntermediateSteps is enabled for observability.

  • Branch • Tool Used? (IF n8n node):

- Function: Provides conditional logic to track agent behavior, specifically checking if the intermediateSteps array is empty. If it is NOT empty (meaning a tool was used), it continues to the observability logging n8n node.

  • Log • Token Metrics to Sheets (Tool) (Google Sheets n8n node):

- Function: Logs detailed observability data (input message, final output, tool name used) when the agent invokes an external tool.
- Key Configuration: Operation set to append; logs metadata (workflow ID, client ID) alongside the interaction details.

Related n8n Workflows

Free

Nodes: 8 Nodes
Updated: December 26 2025
View all
Created by

Hun Yao is a founder-engineer obsessed with building scalable automation that actually moves the needle - with a focus in the E-commerce space. A self-taught product builder with roots in marketing and AI, he uses n8n as his playground to ship MVPs fast - turning modular workflows into elegant, revenue-generating systems.

Featured*