ExtantApe

payload plugin ai budget

by ExtantApe
Logging & monitoring
v3
^3.80.0
MIT

Overview

Summary generated from the README

Wraps each AI call to record model, tokens and cost in a ledger collection, and refuses calls when a run, job, day or month cap is reached. It pauses all calls when prepaid credits run out, probes hourly to resume, and sends one notice per run-out. Requires Payload 3.80 or newer and MongoDB; ships Anthropic prices and a provider adapter.

Features

  • Wrapper function that checks caps before and records usage after each AI call
  • Caps per run, per job per day, per day and per month
  • Dated price table with flagging of unknown models and unpriced answers
  • Credits-out pause with hourly probe and a single notice per run-out
  • Spend summary with month projection, top jobs and cap usage
  • Provider adapters for reading usage and detecting no-credit errors

Use cases

  • Cap daily and monthly LLM spend for background jobs such as translation
  • Halt AI jobs cleanly and save progress when a budget or credits are exhausted
  • Show admins a spend card with projected month-end cost

From the author

AI Budget for Payload: a ledger of every AI API call's tokens and cost, budgets per job, day and month that stop a job cleanly, and a clear pause when prepaid credits run out.

Installation

npm i @extantape/payload-plugin-ai-budget

0

Stars

0

Forks

1

Open Issues

3 days ago

Last Updated

356

Weekly Downloads

v0.1.1

npm Version

45.4 KB

Unpacked Size

0

Dependencies

GitHub stars

00 since Nov 8

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20260
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260
Nov 28, 20260
Nov 27, 20260
Nov 26, 20260
Nov 25, 20260
Nov 24, 20260
Nov 23, 20260
Nov 22, 20260
Nov 21, 20260
Nov 20, 20260
Nov 19, 20260
Nov 18, 20260
Nov 17, 20260
Nov 16, 20260
Nov 15, 20260
Nov 14, 20260
Nov 13, 20260
Nov 12, 20260
Nov 11, 20260
Nov 10, 20260
Nov 9, 20260
Nov 8, 20260
npm weekly downloads

341+341 since Nov 14

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026341
Dec 5, 2026327
Dec 4, 2026307
Dec 3, 2026280
Dec 2, 2026247
Dec 1, 202623
Nov 30, 202627
Nov 29, 202628
Nov 28, 202627
Nov 27, 202628
Nov 26, 202625
Nov 25, 202627
Nov 24, 202633
Nov 23, 202641
Nov 22, 2026256
Nov 21, 2026253
Nov 20, 2026250
Nov 19, 2026250
Nov 18, 2026246
Nov 17, 2026233
Nov 16, 2026221
Nov 15, 20260
Nov 14, 20260

Each point is npm's rolling 7-day download count.

Health Score

68/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1456393913-root.svg)](https://payloaddirectory.dev/plugins/1456393913-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1456393913-root"><img src="https://payloaddirectory.dev/badge/1456393913-root.svg" alt="Payload Directory"></a>
Updated Dec 2, 2026Created Nov 13, 2026Published Nov 29, 2026

README

payload-plugin-ai-budget

AI Budget for Payload: a ledger of every AI API call's tokens and cost, budgets per job, day and month that stop a job cleanly, and a clear pause when prepaid credits run out.

Status: 0.1.1 is the latest release (see CHANGELOG.md). 44 tests pass against Payload 3.92 with an in-memory MongoDB replica set, using fake answers shaped like Anthropic's (no real API is called) and a stand-in for the Notifications plugin. They cover the prices, the ledger and what it leaves out, every cap, fail-closed reads, the credits-out pause and probe, which errors count as a run-out, the notice and its fallbacks, the auto-reload suggestion, the summary and access, signed-out visitors included. No site uses it yet.

What it does

  • withAiBudget(payload, { job, run }, call) wraps each AI call.
    • Before the call, it refuses when:
      • the job isn't in your caps (UnknownJob), so nothing spends outside the budget by accident;
      • any cap is reached (BudgetSpent, with which cap): this run, this job today, everything today, everything this month. The tightest wins;
      • the prepaid credits are out (CreditsOut).
    • After the call, it records the call: the job, run, model and tokens (input, output, cache write, cache read), and the cost in micro-dollars from your dated price table.
    • It never records the prompt, the answer or who asked.
    • A failed call is recorded with zero tokens, and the error passes on.
    • An answer with no token counts (a batch create, a stream object) is recorded as unpriced: flagged, logged and counted in the summary, never as a silent $0. Record the real usage when it arrives with recordUsage(payload, { job, run, batch }, results). Batch result entries ({ result: { message } }) are read as they come. For a stream, record its final message.
    • Wrap only the API call. An error thrown later, while you read the answer, isn't the call's.
    • If the ledger can't be read, it refuses without calling.
  • Your job catches BudgetSpent, saves what it has done, and stops. Don't retry.
  • Prices come from a dated table you pass. anthropicPrices (2026-11-10) ships as data.
    • A model the table doesn't know is priced at its most expensive row and flagged, so a new model can't hide spend.
    • Batch calls are half price, and Haiku prompts over 100,000 tokens use the long-prompt price.
    • Costs round up.
    • costOf is exported, so another tool can recompute and compare.
  • Providers are adapters: usageOf(answer) and isNoCredit(error). anthropicProvider reads answers by shape, with no SDK. Another provider is a few lines.
  • When the credits run out:
    • the provider's billing error pauses every call (CreditsOut);
    • one call an hour from your probeJob (any job, if you don't name one) is let through to see if they're back, and only one worker probes at a time;
    • a successful probe resumes everything;
    • one notice goes out per run-out, never one per refused call. It goes through the Notifications plugin when it's there (to the accounts in notify), else your onCreditsOut.
    • The notice lists what's paused and last month's spend.
    • After a run-out in each of three months in a row, it also suggests a capped monthly auto-reload: the highest of those months, rounded up to the next $5, and never above maxSuggestionUSD. The plugin never sets anything up.
  • spendSummary(payload), for a spend card:
    • month to date, a projection to month end, and today;
    • the top jobs;
    • each cap's use;
    • the 50%, 75% and 90% marks;
    • whether the credits are out.
  • If nobody can be told: with no Notifications recipients and no onCreditsOut, the run-out is logged as an error and the plugin warns at start-up. Give one of them.
  • Made-up test data marked seeded: true shows in the summary and never counts toward a cap, so a seeded month can't refuse a real run on a test site.
  • Access: managers read the ledger (access.manage, required; only an answer of exactly true counts). Nobody writes it through the API.

On its own, and with others

On its own: wrap each AI call in withAiBudget, and give it caps, a price table, a provider and access.manage. Without Notifications, the credits-out notice goes to your onCreditsOut. It doesn't depend on any other plugin.

Needs: Payload ^3.80.0 as a peer dependency. No other EXTANT APE plugin is required.

Works with:

  • Notifications: when it's installed, AI Budget registers its ai-budget:credits-out kind through config.custom.notificationKinds and sends the notice to the accounts in notify through config.custom.notifications.notify.

Color Theory: none needed. It has no screens of its own, and its collections use Payload's standard admin, which takes Color Theory's admin colors when its applyToAdmin option is on.

Family: none yet. The plugin families are a working draft, and AI Budget isn't in it.

Install

npm install @extantape/payload-plugin-ai-budget
import { aiBudgetPlugin, anthropicPrices, anthropicProvider, withAiBudget, BudgetSpent } from '@extantape/payload-plugin-ai-budget'

aiBudgetPlugin({
  caps: { month: 15, day: 1.5, jobs: { translate: { run: 1, label: 'Translation' }, watch: { day: 0.25 } } }, // USD
  prices: anthropicPrices,
  provider: anthropicProvider,
  access: { manage: ({ req }) => req.user?.role === 'admin' }, // required
  probeJob: 'watch',
  notify: [ownerAccountId],
})

try {
  const answer = await withAiBudget(payload, { job: 'translate', run: runId }, () => anthropic.messages.create({ … }))
} catch (e) {
  if (e instanceof BudgetSpent) { await saveProgress(); return }
  throw e
}

Caps, the price table and the suggestion ceiling are checked at start-up: no negatives, missing prices or job caps above the month. Job names are matched exactly, so constructor isn't a job.

Limits

  • Overshoot: two calls at the same moment can both pass the check, so a cap can be overshot by one call per worker running at once. On small models that's cents.
  • Batches overshoot by the whole batch. A batch is checked at its create, when its cost is unknown, and priced when its results arrive. A batch can go past a cap by everything in it, so size each batch under its job's run cap (translation: well under $1). recordUsage settles the create's unpriced record and skips results that errored, expired or were canceled.
  • Summing: each check reads the month's records. That's fine at thousands a month; at millions, it would need a running total.
  • The credits-out error is recognised by its HTTP status and error type. For anthropicProvider that's a 402, a 4xx whose type is billing_error, or a 400 invalid_request_error whose message says the credit balance is too low. A 5xx, a network error, or an error thrown without a status and the response body fails as an ordinary error and is recorded as one, whatever its message says. If a provider changes its status, type or wording, a real run-out is recorded the same way, so check the ledger's error records after a provider change.
  • MongoDB only for now (conditional updates).

Commands

npm test          # integration tests, in-memory MongoDB
npm run typecheck
npm run build

License

MIT. See LICENSE.

Similar plugins