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:
- Financial visibility – know exactly how much each workflow costs.
- Performance insight – high token counts often indicate prompt bloat or unnecessary context.
- Security awareness – sudden spikes can signal abuse or a prompt‑injection attack.
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.
- Expose a
/usagewebhook (Node.js, Cloudflare Workers, or n8n HTTP Request node). - In the wrapper above,
requests.post("https://example.com/usage", json=record). - In the webhook, aggregate daily totals and compare against a budget (e.g., $50/month).
- 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:
- Prompt hygiene: Trim system messages, reuse static context, and avoid sending full document bodies when a summary will do.
- Model selection: Use the cheapest model that meets quality needs (e.g.,
gpt-4o-minifor routine triage,gpt-4ofor complex reasoning). - Rate limiting: Enforce a max number of calls per minute in your orchestrator to prevent runaway loops.
- Budget caps: Set a hard limit in your OpenAI account settings; the platform will reject calls once the cap is hit.
Putting It All Together: A Sample Monitoring Checklist
| Frequency | Task | Owner |
|---|---|---|
| Daily | Run aggregation job that sums total_tokens and calculates cost. | DevOps |
| Daily | Check alert inbox for token‑spike notifications. | Founder |
| Weekly | Review top‑5 cost‑heavy workflows and prune prompts. | Product Lead |
| Monthly | Adjust 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.