AI Usage and Token Tracking for Messaging Apps - n8n Workflow

Automate AI token tracking for agent usage from WhatsApp and Telegram, logging all interactions, audio transcriptions, and costs directly to Google Sheets using this powerful n8n workflow.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • AI service providers needing to monitor operational costs and usage metrics.

  • Developers seeking robust logging for their conversational AI agents.

  • Businesses implementing AI agents via messaging platforms like WhatsApp or Telegram.

  • Automation engineers looking for advanced n8n templates integrating LangChain Agents.

Overview

Managing AI operational costs is crucial, especially when conversational agents are deployed across multiple platforms. This specialized n8n workflow addresses this challenge by providing a centralized tracking system. Whether the input originates from a WhatsApp message (via Evolution API webhook) or a Telegram bot, this automation ensures that every interaction, including the input text, model response, and associated token counts, is meticulously logged to Google Sheets. This n8n solution handles complexity, such as automatically transcribing audio messages before processing them with the AI Agent. By leveraging this sophisticated n8n node structure, users gain real-time visibility into cost centers and usage patterns, enabling better resource management and detailed reporting.

How it Works

This automation begins with one of two starting points: the Evolution API webhook for WhatsApp or the Telegram n8n trigger. Once activated, the n8n workflow standardizes the data using Set nodes (Map/Map1).


  1. Input Handling: The flow immediately hits the Audio or Text Separation If n8n node. If the input is audio (often encountered in voice messages on WhatsApp/Telegram), the workflow executes an HTTP Request (GET Media) to fetch the audio file, converts it, and runs it through the Audio Transcription n8n node (OpenAI/LangChain) to convert speech to text. If the input is standard text, it bypasses transcription.

  2. Core Intelligence: The transcribed or original text is routed to the AI Agent (LangChain Agent n8n node). This agent, configured with a Chat Model (likely via OpenRouter), can utilize various tools like Gmail (Send Email), Airtable (Get Contacts), and Google Calendar (Create Event) based on the user's request. The n8n Agent handles the complex interaction and provides token usage statistics in its output.

  3. Token Logging: The agent's output is split into success and error paths. A dedicated Code n8n node (Clean Up/Clean_Up) processes the result, extracting essential data including tokens used. This data is then written to a Google Sheet via the Log or Errors Google Sheets n8n node, effectively acting as your AI token tracker.

  4. Response Delivery: Finally, the Input Source Switch n8n node determines the originating platform (WhatsApp or Telegram) and uses the appropriate HTTP Request or Telegram n8n node to send the AI agent’s response back to the user, completing the full conversational loop within the n8n workflow.

Installation Guide


  1. Import the n8n Workflow: Copy the provided JSON data and import it into your n8n instance via the 'Workflows' menu, selecting 'New' and 'Import from JSON'.

  2. Set up Trigger Credentials: Configure the Evolution API Webhook n8n trigger URL in your WhatsApp provider settings, and ensure the Telegram n8n trigger is correctly configured with your Bot Token.

  3. Messaging Service Credentials: Update the WhatsApp and WhatsApp Erro HTTP Request n8n nodes with your specific Evolution API (or similar platform) authentication details and endpoint URLs for sending replies.

  4. AI Credentials: Configure the Chat Model and Audio Transcription n8n nodes with your relevant OpenAI/LangChain/OpenRouter API keys.

  5. Logging Credentials: Establish a Google Sheets credential and update the Log and Errors Google Sheets n8n nodes to point to your desired spreadsheet ID and sheet names where the usage data will be stored.

  6. Activate: Ensure all necessary credentials are set for the AI Agent's tools (Gmail, Airtable, Google Calendar, though these are disabled in the provided template, they indicate potential expansion) and activate the n8n workflow.

Node Details


  • Evolution API (Webhook n8n trigger): Serves as the primary n8n trigger for incoming WhatsApp messages.

  • Telegram (Telegram n8n trigger): Acts as the secondary n8n trigger for interactions coming from a Telegram bot.

  • Audio or Text Separation (If): A core logic n8n node that directs the flow based on message type, initiating transcription if an audio file is detected.

  • Audio Transcription (OpenAI n8n node): Uses OpenAI services to convert voice messages into text input suitable for the AI agent.

  • AI Agent (LangChain Agent n8n node): The central processing unit. It processes the text, utilizes its configured tools (even if disabled here, such as Gmail and Airtable), calculates token usage, and generates the final response.

  • Chat Model (LangChain ChatOpenRouter n8n node): Provides the language model backend for the AI Agent, handling conversational reasoning.

  • Clean Up / Clean_Up (Code n8n node): Essential for transforming the raw AI agent output into structured data, focusing on extracting token usage and cost metrics.

  • Log / Errors (Google Sheets n8n node): The destination n8n nodes for tracking. They append interaction and token usage data (successes and failures) to specified rows in Google Sheets.

  • Input Source / Input Source1 (Switch): Directs the final AI response (or error message) back to the correct originating messaging platform (WhatsApp via HTTP or Telegram via the dedicated Telegram n8n node).

Related n8n Workflows

Paid

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

Hi loves! I've been making workflows for n8n for two years, and I hope you like my flows <3

Featured*