Skip to main content

Track AI agent token costs and store receipts with the n8n API and Data Tables

Workflow preview

Workflow preview
100%
Track AI agent token costs and store receipts with the n8n API and Data Tables preview
Open on n8n.io

1. Workflow Overview

Ever wondered how much your AI Agent is costing you per run? I had no idea. You build an agent, it works great, but behind the scenes tokens are burning. And you just see the API key cost as an agg...

Best for

  • Engineering automation workflows
  • AI Summarization automation workflows
  • intermediate n8n builders looking for reusable templates

Tools used

n8n-nodes-base.stickynote, n8n-nodes-base.scheduletrigger, n8n-nodes-base.n8n, n8n-nodes-base.splitinbatches, n8n-nodes-base.code, n8n-nodes-base.if, n8n-nodes-base.datatable

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Ben van Steenbergen.

Original n8n.io source

1.1 Workflow description

Title
Track AI agent token costs and store receipts with the n8n API and Data Tables
Workflow name
Track AI agent token costs and store receipts with the n8n API and Data Tables

Ever wondered how much your AI Agent is costing you per run?

I had no idea. You build an agent, it works great, but behind the scenes tokens are burning. And you just see the API-key cost as an aggregated value on Stripe, right?

One workflow costs you $0.002. Another one quietly racks up $2.00 every time it fires. Without tracking, you're flying blind.

This workflow fixes that.

Who is this for

Anyone running AI agents in n8n who wants to know what they actually cost before the bill surprises you.

What it does

Every hour, it scans all your tagged (triggers on worklow TAGS) AI agent workflows and extracts the exact token usage per model, calculates the estimated cost in USD, and stores a receipt in an DataTable.

Setup

  • Create a DataTable called execution_receipts with columns: workflowid (text), executionid (text), receipt (text), created_at (text), units (number) — or import the CSV template
  • Set your n8n API credential to your own instance on "Get AI Agent Workflows" and "Get Workflow Executions"
  • Select your Data Table in "Save Receipt to Data Table"
  • Tag your workflows you want to track with agent
  • Try-out the workflow

How it works

  1. Hourly Trigger kicks off the workflow on a schedule
  2. Get AI Agent Workflows fetches all workflows tagged with agent via the n8n API
  3. Loop Workflows iterates through each workflow one by one
  4. Get Workflow Executions pulls all execution data for the current workflow
  5. Extract Token Usage & Cost parses each execution, detects which AI model was used, extracts token counts (input/output/total), and calculates the estimated USD cost using a built-in pricing table
  6. Save Receipt to DataTable stores the receipt with workflow ID, execution ID, full token breakdown, cost, and timestamp

Supported providers

  • Anthropic Claude: claude-3 through claude-opus-4-7
  • OpenAI: gpt-3.5 through gpt-4.1, o1/o3/o4 reasoning models
  • Google Gemini: 1.5 through 2.5
  • Perplexity: sonar family

New models are auto-detected. To add pricing for a newly released model, add one line to the MODEL_RATES table inside the code node.

What each receipt contains

  • Workflow ID and Execution ID
  • Model(s) used
  • Token breakdown (prompt, completion, total)
  • Estimated cost in USD
  • Timestamp

Self-hosted users may need to enable Data Tables with N8N_ENABLED_MODULES=data-table in their environment.


Like this workflow? Let me know! Follow me on LinkedIn for more n8n automation and AI agent builds.

Cheers, Ben

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 2 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 3 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 4 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 5 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 6 - Hourly Trigger

Type / Role
n8n-nodes-base.scheduleTrigger - scheduleTrigger
Config choices
Version 1.2

Block 7 - 1.1 Get AI Agent Workflows

Type / Role
n8n-nodes-base.n8n - n8n
Config choices
Version 1

Block 8 - Loop Workflows

Type / Role
n8n-nodes-base.splitInBatches - splitInBatches
Config choices
Version 3

Block 9 - 1.2 Get Workflow Executions

Type / Role
n8n-nodes-base.n8n - n8n
Config choices
Version 1

Block 10 - Extract Token Usage & Cost

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 11 - Loop Receipts

Type / Role
n8n-nodes-base.splitInBatches - splitInBatches
Config choices
Version 3

Block 12 - Has Usage Data?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 13 - 1.3 Save Receipt to Data Table

Type / Role
n8n-nodes-base.dataTable - dataTable
Config choices
Version 1

3. Summary Table

Workflow Track AI agent token costs and store receipts with the n8n API and Data Tables
Complexity intermediate
Nodes 13
Categories Engineering, AI Summarization
Author Ben van Steenbergen
Published 20 Apr 2026

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/15177/15177.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Track AI agent token costs and store receipts with the n8n API and Data Tables do?

Ever wondered how much your AI Agent is costing you per run? I had no idea. You build an agent, it works great, but behind the scenes tokens are burning. And you just see the API key cost as an agg...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, AI Summarization use case.