On a schedule
Get warned when OpenAI spend runs high
An email on mornings when the previous day's API cost went over your limit, broken down by project.
- Runs
- On a schedule
- Apps
- OpenAI
- AI
- Not needed
- Access
- Admin key (project keys get "missing scopes: api.usage.read")
Send this to your assistant
Create a ClickRuntime automation: every morning at 8am, check yesterday's OpenAI API costs per project and email me if the total is over $20.
First time? Adding ClickRuntime to your assistant takes about two minutes.
Why use it
Why teams use this automation
A developer ships a small app that calls OpenAI, and one weekend a loop bug, a leaked key or a surprisingly popular feature burns through a few hundred dollars before anyone looks at the usage page. The monthly spending limit exists, but by the time it kicks in the damage is done, and the first signal is an invoice. Developers and startup founders often check the dashboard only when something already feels wrong.
Once a day, or on whatever schedule you set, the OpenAI Costs endpoint is queried for the last 24 hours and amounts are summed by project. The total is compared against your threshold, for example 20 dollars, and an email or a Slack message goes out only if spending went over, showing which project caused it.
It is useful for teams that share an organization account or run several projects under one billing setup.
What you get out of it
Runaway spend is caught within a day
A loop that calls the API thousands of times shows up as a spike in the next check. Finding it after one day costs tens of dollars, while finding it after a month can cost thousands.
The culprit project is named
Because costs are broken out by project, the alert tells you which app, environment or teammate's experiment is responsible. You can pause that key directly and leave everything else running.
Surprise invoices become less likely
Seeing daily totals against a threshold gives you a feel for normal usage. When the bill arrives at month end, there are no unexplained numbers in it. Over time, daily totals teach you what ordinary usage looks like for each project, which makes strange days stand out.
Leaked keys get noticed
If someone finds a key in a public repository and uses it, the usage appears as an unusual spike. An alert is often the first sign that a key needs to be revoked.
Silence on normal days
You hear only when something went over your limit, so there is no extra email to ignore on quiet days. That keeps the alert meaningful when it does arrive. That quiet is what makes the occasional alert credible, which is why people act on it quickly.
How it works
What happens, step by step
Every morning, ClickRuntime looks up how much you spent on the OpenAI API the day before.
It adds up the cost for each project and compares the total with the limit you set.
If yesterday went over, you get an email with the total in the subject and a breakdown by project. Otherwise you hear nothing.
Before it runs live
- Connect OpenAI
What you get
Build your own
Need it to work differently?
Your own rules
Conditions, thresholds, filters and exceptions, written the way you'd explain them to a colleague.
Any number of apps
One automation can read from one app and update several others. Over 248 apps are ready, and anything with an API works too.
AI where it helps
Summaries, classification, extraction and drafts use ClickRuntime's built-in AI, with no extra key.
On a schedule or on each event
Runs as often as every 5 minutes, or the moment an app sends an event, and keeps track of what it already handled.
Changes you can ask for
- Run it at a different time, more or less often, or only on weekdays
- Add an AI step, for example to summarize, sort or draft a reply
- Add another app, for example also send it to Slack
- Combine it with other OpenAI steps into one automation
Under the hood
The code that runs this automation
from collections import defaultdict
from datetime import datetime, timedelta, timezone
from clickruntime import on_trigger, credentials, http, notify
LIMIT = 20.0
@on_trigger
def run(event):
today = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
start = today - timedelta(days=1)
resp = http.get("https://api.openai.com/v1/organization/costs",
params={"start_time": int(start.timestamp()), "end_time": int(today.timestamp()),
"group_by": "project_id"},
headers={"Authorization": f"Bearer {credentials.secret('OPENAI_ADMIN_KEY')}"})
resp.raise_for_status()
per_project = defaultdict(float)
for bucket in resp.json()["data"]:
for r in bucket["results"]:
per_project[r.get("project_id") or "no project"] += r["amount"]["value"]
total = sum(per_project.values())
if total > LIMIT:
lines = [f"{p}: ${v:,.2f}" for p, v in sorted(per_project.items(), key=lambda kv: -kv[1])]
notify.email_me(f"OpenAI spend yesterday: ${total:,.2f}", "\n".join(lines))
return {"total": round(total, 2)}FAQ
Common questions
What permission does the OpenAI key need?
Are costs final at the time of the check?
Can the threshold differ for each project?
What permissions does it need?
Does it need AI?
How many runs does it use?
Can I change how it works?
Related
You might also like
Generate weekly social images with OpenAI
Each week a set of images for your planned topics, made in your style, arrives in your inbox as links.
See how it worksGet a weekly OpenAI cost breakdown
A Monday table of last week's tokens and cost per model and project, compared with the week before.
See how it worksGet a morning digest summarised on your own key
Every day new items from the feeds you follow are summarised with your OpenAI model and emailed, with no repeats.
See how it worksKeep exploring
Set up “Get warned when OpenAI spend runs high” from your assistant
Copy the prompt above, or connect your assistant first. Free plan, no credit card.