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.
Download this n8n workflow template and start using it instantly.
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.
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.
workflowid, executionid, and a custom client_id for traceability.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.intermediateSteps).To deploy this comprehensive n8n template, follow these steps:
OPENAIAPIKEY) is set as an environment variable in your n8n instance.clientid if necessary.model, inputtokencost, and outputtoken_cost variables to accurately reflect your LLM provider's current pricing.workflowid and executionid, and assigning a hardcoded client_id (customizable).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.ChatOpenAI; hardcoded variables for model and per-million token costs; uses the input Google Sheets n8n node as a logging tool within the callback.append; maps all computed token and cost data fields.systemMessage for context; returnIntermediateSteps is enabled for observability.intermediateSteps array is empty. If it is NOT empty (meaning a tool was used), it continues to the observability logging n8n node.append; logs metadata (workflow ID, client ID) alongside the interaction details.Use this advanced n8n workflow to automatically track your OpenAI Admin API token usage, project costs, and API key metrics, logging all relevant financial data instantly to Google Sheets for transparent billing and analysis.

Identify new search queries (keywords) in Google Search Console that previously had zero impressions. This powerful n8n workflow uses advanced filtering logic to categorize zero-click opportunities and immediate traffic drivers.

Use this powerful n8n workflow to track P&L across all your investment brokers automatically. Securely receive scheduled or on-demand portfolio summaries via Telegram. Explore more n8n templates today.

Capture Partnerstack affiliate events using an n8n trigger, automatically log details to Google Sheets, and send instant alerts via Telegram. A powerful n8n workflow for partnership management.

Automate personal finance tracking by implementing a powerful n8n workflow featuring an AI Agent and Google Sheets. Log expenses via Slack messages, track debts, and receive daily financial summaries.

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.







































