Skip to content

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.

Paste it into your assistant. It tells you what it will send or change before it runs live.

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

  1. Every morning, ClickRuntime looks up how much you spent on the OpenAI API the day before.

  2. It adds up the cost for each project and compares the total with the limit you set.

  3. 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

RunsOn a schedule
Get warned when OpenAI spend runs hightoday, 9:00Succeeded
Email · OpenAI spend yesterday: $27.40proj_support-bot: $19.12 proj_search: $8.28

Build your own

Need it to work differently?

This template is a starting point. Copy the prompt and change it, or describe a different automation from scratch, with your own conditions, more apps or an AI step. Your assistant writes it, tests it and runs it on ClickRuntime.
  • 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

This is the Python script that runs each time the automation starts. Your AI assistant writes it for you when you send the prompt, and you allow each step in your assistant. To change how the automation works, describe the change to your assistant and it updates the script for you. For every function it can use, see the script reference.
daily_openai_spend_alert.pyexample
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?
The Costs endpoint is part of OpenAI's organization admin API, so it requires an admin API key and not an ordinary project key. The key is stored as a secret, and only an organization owner can create it.
Are costs final at the time of the check?
Costs reported by OpenAI can lag by a short time, so the most recent hours may be slightly understated. The alert reflects what OpenAI has reported when the run happens, which is accurate enough for catching spikes.
Can the threshold differ for each project?
Yes. A separate limit per project is possible, for example 5 dollars for a test project and 50 for production. Each project's total is compared against its own number, and only those that exceed it are listed.
What permissions does it need?
Admin key (project keys get "missing scopes: api.usage.read"). Nothing else in your accounts is used.
Does it need AI?
No. It runs without AI, so it doesn't use your plan's AI allowance.
How many runs does it use?
One run each time it runs (on a schedule). Checks that find nothing to do and stop right away aren't counted.
Can I change how it works?
Yes. Describe the change to your AI assistant, for example a different schedule, channel or filter. It updates the automation, tests it and tells you what changed.

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.