AI Automation

Monitoring Token Usage and Cost with the OpenAI Agents SDK for Small Teams

TL;DR: Use the OpenAI Agents SDK’s built‑in usage callbacks, pipe the data into a lightweight observability tool (e.g., n8n, Cloudflare Workers, or a simple spreadsheet), set budget alerts, and review a weekly checklist to keep token spend predictable for a small team.

Why Track Token Usage in the First Place?

Tokens are the currency of large language models. Even modest‑scale automations can add up quickly when they run dozens of calls per day. For a solo founder or a five‑person startup, an unexpected spike can eat a month’s budget. Monitoring token usage gives you:

Setting Up Basic Metrics in the OpenAI Agents SDK

The SDK emits a usage object on every ChatCompletion response. Capture it with a simple wrapper:

import openai
from datetime import datetime

usage_log = []

def call_agent(messages):
    response = openai.ChatCompletion.create(
        model="gpt-4o-mini",
        messages=messages,
        stream=False,
    )
    usage = response["usage"]
    usage_log.append({
        "timestamp": datetime.utcnow().isoformat(),
        "prompt_tokens": usage["prompt_tokens"],
        "completion_tokens": usage["completion_tokens"],
        "total_tokens": usage["total_tokens"],
        "cost_usd": usage["total_tokens"] * 0.000002  # example rate
    })
    return response

Store usage_log in a persistent medium – a JSON file, a tiny SQLite DB, or a cloud KV store – so the data survives restarts.

Integrating with a No‑Code Orchestrator for Alerts

Most small teams already use a workflow engine like n8n or Cloudflare Workers. Push the usage record to an HTTP endpoint that triggers a notification when thresholds are crossed.

  1. Expose a /usage webhook (Node.js, Cloudflare Workers, or n8n HTTP Request node).
  2. In the wrapper above, requests.post("https://example.com/usage", json=record).
  3. In the webhook, aggregate daily totals and compare against a budget (e.g., $50/month).
  4. If the daily spend exceeds budget/30, send a Slack or email alert.

This pattern keeps the monitoring logic outside the agent code, preserving the SDK’s simplicity while giving you a real‑time safety net.

Cost‑Control Strategies for Small Teams

Beyond raw monitoring, adopt these practical controls:

Putting It All Together: A Sample Monitoring Checklist

FrequencyTaskOwner
DailyRun aggregation job that sums total_tokens and calculates cost.DevOps
DailyCheck alert inbox for token‑spike notifications.Founder
WeeklyReview top‑5 cost‑heavy workflows and prune prompts.Product Lead
MonthlyAdjust budget caps and model choices based on usage trends.Finance

Following this checklist keeps token spend transparent without adding heavy infrastructure.

When to Involve the NIST AI Risk Management Framework

If your automation handles regulated data (e.g., health or finance), map token‑usage monitoring to the NIST AI RMF’s Monitoring function. Document the metrics, define risk thresholds, and include them in your AI system’s governance artifacts. This alignment satisfies auditors and reduces surprise expenses.

With these steps, a small company can enjoy the power of the OpenAI Agents SDK while keeping the bill under control.

Want this kind of automation built for your workflow?

AISecAll designs, builds, deploys, and maintains focused AI automations for small companies and independent entrepreneurs.

Book a call Discuss a project