How to Track AI Agent Costs in n8n
The Problem With n8n AI Workflows
n8n makes it easy to connect AI models to your workflows. OpenAI node here, Anthropic node there, and suddenly you have a powerful automation. What you don't have is any visibility into what each run costs.
The provider dashboard shows your total API spend. It doesn't show which workflow spent what, which run spiked, or which automation is running 10x more than expected.
What You Need
Before you start, you need:
Step 1: Get Your AgentShield API Key
Sign up at agentshield.one → Settings → API Keys → Create key.
Your key starts with `ags_live_`.
Step 2: Find Where Your AI Node Outputs Token Usage
Most LLM nodes in n8n return token usage in the response. For OpenAI:
$json.usage.prompt_tokens
$json.usage.completion_tokensFor Anthropic:
$json.usage.input_tokens
$json.usage.output_tokensStep 3: Add an HTTP Request Node
After your AI node, add a new node: HTTP Request.
Configure it exactly like this:
Method: POST
URL: https://api.agentshield.one/v1/track
Headers:
Authorization: Bearer ags_live_xxxxx
Content-Type: application/json
Body (JSON):
{
"agent": "my-n8n-workflow",
"model": "gpt-4o",
"input_tokens": {{ $json.usage.prompt_tokens }},
"output_tokens": {{ $json.usage.completion_tokens }}
}Replace `my-n8n-workflow` with a name that identifies this workflow in your dashboard.
Step 4: Run Your Workflow
Execute the workflow. In AgentShield's dashboard, you'll see:
Setting Budget Caps
Once you're tracking costs, you can set a budget cap per workflow. If your research workflow hits $5 in a day, AgentShield can send you a Slack alert or freeze the workflow automatically.
This is the per-agent, per-session visibility that your provider dashboard doesn't give you.
Conclusion
Five minutes of setup. An HTTP Request node. That's all it takes to go from zero visibility to complete cost attribution for your n8n AI workflows.
Track costs, catch spikes, optimize what matters.
Ready to monitor your AI agents?
Set up AgentShield in 5 minutes. Free plan available.
Start for Free →