Automatic OpenAI Usage and Cost Logging to Google Sheets - n8n Workflow

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.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Finance and Operations Teams: Who need accurate, granular tracking of AI resource consumption for chargebacks and budgeting.
Cloud Administrators: Responsible for monitoring large-scale deployments of OpenAI services.
Developers using n8n: Looking for a complex example of merging parallel HTTP requests and custom data transformation within an n8n workflow.
Anyone needing transparent AI billing: Seeking a fully automated, customizable solution that utilizes n8n templates for observability.

Overview

Managing API consumption across multiple projects and API keys can be complex, especially for large organizations utilizing OpenAI services. This n8n workflow solves the problem of manual tracking by creating a fully automated pipeline for billing and usage transparency. Using the built-in schedule n8n trigger, the system regularly queries the OpenAI Admin API for comprehensive usage and cost data. The core value of this n8n template lies in its ability to correlate token usage reports with corresponding cost metrics, and then enrich that data with human-readable API key names and project details, ensuring that every record appended to Google Sheets is complete and ready for analysis. This robust n8n workflow ensures billing compliance and provides immediate visibility into your AI spending.

How it Works

This comprehensive n8n workflow executes based on a defined schedule n8n trigger, typically running once daily or hourly.


  1. Trigger & Parallel Requests: The process starts with the Schedule Trigger n8n node. Immediately following the trigger, the workflow simultaneously executes two HTTP Request n8n node calls to the OpenAI Admin API: one to fetch token usage data and one to fetch cost data.

  2. API Key Enrichment: The usage stream first extracts API key IDs and sends them to a dedicated HTTP Request n8n node to fetch the associated key details and names. A series of Set, Split Out, and Merge n8n node operations correlate these details back into the usage data, adding the human-readable API key name.

  3. Project Details Retrieval: Concurrently, the workflow fetches OpenAI Project details to obtain project names.

  4. Data Consolidation: The token usage data (now enriched with API key names) and the cost data are merged together (Merge token and usage). This combined stream is then merged with the retrieved project data (Merge token, usage, projects).

  5. Final Transformation: A Code n8n node (Add Project name in cost and usage objects) performs final transformations, integrating the project names into the cost and usage items.

  6. Data Splitting and Structuring: The consolidated dataset is split into two distinct streams: Usage and Cost. Each stream passes through dedicated Set and Split Out n8n node sequences to structure the data precisely into the columns required for Google Sheets.

  7. Final Action: Two Google Sheets n8n node instances execute the final step, appending the structured Usage data to one sheet and the Cost data to another, completing the automated logging process of this powerful n8n workflow.

Installation Guide

To install and utilize this high-value n8n template, follow these steps:


  1. Import the n8n workflow: Copy the provided JSON data and paste it directly into your n8n instance using the 'Import from JSON' function.

  2. Set up Credentials: You will need to configure the following credentials:

OpenAI Admin API: The HTTP Request n8n node requires appropriate headers or authentication (usually a Bearer token) configured to access the specialized OpenAI Admin endpoints (which differ from the standard API). Note that the specific structure for Admin API authorization must be correctly set in the HTTP Request n8n node parameters.
Google Sheets: You must provide Google Sheets credentials and configure the Spreadsheet ID and sheet names in the final two Google Sheets n8n node instances (Append Usage to GSheets and Append Cost to GSheets).

  1. Configure Schedule: Adjust the timing of the Schedule Trigger n8n node to meet your desired reporting frequency (e.g., daily at midnight).

  2. Review Code Nodes: Examine the Code n8n node configurations (like Add api_key name to token usage) to ensure the input field references match your data structure, especially if the OpenAI API responses change slightly.

  3. Activate: Save and activate the n8n workflow.

Node Details

Schedule Trigger (n8n trigger): Initiates the entire n8n workflow on a recurring schedule (e.g., every hour or day). This is the starting n8n node.
OpenAI Admin - get token usage (HTTP Request n8n node): Calls the OpenAI Admin API to retrieve raw token usage statistics.
OpenAI Admin - Get cost (HTTP Request n8n node): Calls the OpenAI Admin API to retrieve cost data associated with usage.
OpenAI Admin - Get API Key details (HTTP Request n8n node): Fetches metadata about the API keys, enabling the correlation of cryptic key IDs with meaningful names.
Set apikey and project ids / Set apikey id and name (Set n8n node): Used extensively for structuring, renaming, and selecting specific data fields necessary for subsequent merging and Google Sheets output.
Add api_key name to token usage (Code n8n node): Custom scripting used to map and merge API key names found in the API key details response into the corresponding token usage records.
Merge token and usage / Merge token, usage, projects (Merge n8n node): Critical flow control n8n node used to systematically combine data streams from the parallel API calls and project lookups.
Split Out Usage / Split Out Cost (Split Out n8n node): Separates the combined dataset back into discrete Cost and Usage streams for final, customized formatting before writing to Google Sheets.


  • Append Usage to GSheets / Append Cost to GSheets (Google Sheets n8n node): The final action n8n node, responsible for appending the fully structured and enriched data records to their respective tabs in the target spreadsheet.

Related n8n Workflows

Paid

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

We are a product studio that helps organizations leverage no-code and generative AI to automate internal processes and launch new digital products. LinkedIn: https://www.linkedin.com/in/baptistej/

Featured*