Back to Blog
·7 min read·guide

How to Track OpenAI Costs Across Multiple AI Agents

Step-by-step guide to instrumenting your OpenAI API calls with AgentBurn for per-agent cost visibility, budget alerts, and spend optimization.

OpenAI's billing dashboard shows you a single number: total spend. If you're running multiple agents, each making dozens of API calls per task, that number is useless. You need per-agent, per-model, per-task cost breakdowns.

The Problem with OpenAI's Built-in Billing

OpenAI gives you monthly totals and daily usage charts. That's it. No way to attribute costs to specific agents, workflows, or customers. If your support bot and your research agent share the same API key, you're flying blind.

Setting Up Per-Agent Tracking

AgentBurn tracks costs at the event level. Each API call becomes a cost event tagged with the agent that made it.

Step 1: Register Your Agents

# Register each agent with a unique ID
curl -X POST https://your-agentburn.dev/api/agents \
  -H "Content-Type: application/json" \
  -d '{"name": "support-bot", "projectId": "customer-service"}'

curl -X POST https://your-agentburn.dev/api/agents \
  -H "Content-Type: application/json" \
  -d '{"name": "research-agent", "projectId": "data-team"}'

Step 2: Instrument Your OpenAI Calls

After every OpenAI API call, send the token usage and cost to AgentBurn:

import openai
import requests

response = openai.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

# Calculate cost (gpt-4o: $2.50/1M input, $10/1M output)
input_cost = response.usage.prompt_tokens * 2.50 / 1_000_000
output_cost = response.usage.completion_tokens * 10.00 / 1_000_000

requests.post("https://your-agentburn.dev/api/ingest", json={
    "agentId": "support-bot",
    "provider": "openai",
    "model": "gpt-4o",
    "operation": "llm_call",
    "inputTokens": response.usage.prompt_tokens,
    "outputTokens": response.usage.completion_tokens,
    "costUsd": input_cost + output_cost
}, headers={"x-api-key": "YOUR_KEY"})

Step 3: Set Budget Alerts

Configure alerts so you know when any agent exceeds its daily or monthly budget:

curl -X POST https://your-agentburn.dev/api/alerts \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Support bot daily limit",
    "agentId": "AGENT_ID",
    "budgetUsd": 50,
    "periodType": "daily"
  }'

What You'll See

Once instrumented, your AgentBurn dashboard shows: cost per agent over time, token usage breakdown (input vs output), cost per model (GPT-4o vs GPT-4o-mini), and spend velocity so you can predict monthly totals.

Optimization Tips

  • Route simple tasks to cheaper models — Use GPT-4o-mini for classification, GPT-4o for reasoning
  • Cache common prompts — If multiple users ask similar questions, cache the response
  • Shorten system prompts — Every token in your system prompt is charged on every call
  • Set hard daily limits — AgentBurn alerts catch runaway loops before they drain your budget
openaicost-trackingtutorialapi

Start tracking your AI agent costs

Open-source. Self-hosted. Free forever for the core engine.

Related Articles