Costs and the AI budget
Every model and jev call is recorded with what OpenRouter charged for it, and each user has a hard monthly budget (SI-534). Code: packages/core/src/ai/. In the app: Settings → Costs (/settings/costs).
Amounts are US dollars, as OpenRouter bills. The target from SI-514 is under 10 € a month, and the expected spend is $1–5.50.
Making a paid call
There is one way to call a model or jev: the OpenRouter client (openrouter.ts). In a pipeline stage it is context.ai, inside once():
const result = await context.once(unit, 'summary', () => context.ai.chat(unit, { models: ['openai/gpt-6-luna', 'deepseek/deepseek-v4.1-flash'], reasoning: { enabled: false }, messages }));context.ai.chat() sends a chat completion and context.ai.decide() a jev decision. The request goes to OpenRouter as it is. Each call:
- Checks the budget of the unit’s users. Only users within their budget pay. When none is, nothing is sent and the call throws
BudgetExceededError. - Checks who may use AI (who may use AI). Users who aren’t admins and have no key of their own neither pay nor count as a reason to call. When none is left, nothing is sent and the call throws
NoModelKeyError. - Calls OpenRouter with the user’s own key when they set one, else, for admins, with
OPENROUTER_API_KEY(your own key). A stage that is stopping (close, abort) starts no new call, but a call that started isn’t cut off: OpenRouter bills it either way, so it runs to its end (2 minutes at most) and is recorded. The stage stops after it. - Records the call in
ai_calls: who pays, the stage, the item,modelorjev, the model that answered (a fallback if the first one failed), tokens in and out, and OpenRouter’susage.cost. A call for several users (anitemstage) is split equally between them.
It returns plain JSON (id, model, content or answers, usage), so once() can keep it. Because of once(), a stage that reruns after a crash doesn’t call or record again. A call that was cut off by the crash runs again and is recorded again: OpenRouter charged for both.
Rows are keyed by OpenRouter’s generation ID, the ID its usage dashboard shows too. Recording an ID twice keeps one row.
A failed call (an HTTP error, an error in the body, or a body that isn’t JSON) throws OpenRouterError and records nothing. When the key’s own limit is reached, OpenRouter answers 402.
Besides the stages, a reader’s Check claims (SI-551, research.md) calls through the same client, recorded under stage research, and so do web page recipes, topics and check previews.
The budget
Each user has a monthly budget, $10 unless they set one (Settings → Costs, or PUT /api/v1/costs/budget). 0 turns paid stages off. An account can’t set more than its limit, $10 unless an admin changes it, and a budget set before the limit was lowered counts as the limit (budgetLimitUsd in the overview; admins have no limit, accounts.md). A suspended account makes no paid calls at all. The month is the calendar month in the user’s time zone (user_settings.time_zone, UTC when unset), and it starts again on the 1st.
When this month’s spend reaches the budget, the pipeline degrades instead of stopping:
- Before a paid stage, its units are left out for users over budget, with a
skippedstep and the reasonbudget(allowPaid, pipeline.md). - During a stage, every call checks again, so a batch that crosses the cap stops calling at once. A refused call skips its unit with the reason
budget, througheachUnit(). When a stage lets the error endrun(), only units whose users are all over budget are skipped for it; the others fail, so a re-run gives them their turn. A stage may also catch it and return{ status: 'skipped', reason: 'budget' }itself. - Free stages and reading go on as usual. Items whose paid stages were skipped can be run again next month.
Calls that are already running when the cap is reached finish and are recorded, so the month can end slightly over the budget: at most one call per unit a stage runs at once (eachUnit() runs 4).
The OpenRouter key’s own limit is the backstop for the whole instance: a separate key for Hotaru with a monthly limit of about $10.
Deleting a user deletes their ledger rows with the rest of their data. pnpm costs check then shows less than OpenRouter for the rest of that month.
Who may use AI
Only admins may use the instance’s OPENROUTER_API_KEY (SI-578). Everyone else needs an OpenRouter key of their own: without one, every AI feature is off for them, and the instance pays nothing on their behalf. aiAccess() in model-keys.ts decides it for each user: own (their own key, admins included), instance (an admin without a key of their own) or none. An unknown user is no admin.
The server enforces it on every paid call, whatever the app shows:
- The OpenRouter client (
OpenRouterinopenrouter.ts), which every pipeline stage, check claims, web page recipes, topics and check previews call through, leaves such users out of a call’s payers. A call left without payers throwsNoModelKeyErrorbefore anything is sent. - The pipeline skips every paid stage for such a user before it runs, with a
skippedstep and the reasonno_key(Executions shows “no OpenRouter key”). They never join a call another user’s item stage makes: a summary or a same-story check for an item that an admin and a reader without a key both follow is made for the admin alone, and paid by the admin alone. - The agent chat refuses their message (
no_key), and never points their conversation at the instance’s key. - The API answers such requests with
403and{ "error": "no_key" }:POST /api/v1/chat,/api/v1/items/:id/research,/api/v1/sources/recipe,/api/v1/topics/groupand/api/v1/checks/preview.POST /api/v1/topics/groupwithifDueanswersgrouped: falseinstead, so opening Topics stays quiet.
The app says why a feature is off and links to Settings → Costs: the agent panel, Check claims, Topics, web page recipes, check previews, and the Gate, Summaries, Keywords, Checks and Executions settings. The app layout’s aiAccess tells it. Setting a key that OpenRouter accepts turns AI on at once: the next call goes out with it. Items that arrived while AI was off aren’t run again.
Results that are shared per item, a summary in one language and the story an item joined, are paid for by whoever may use AI. A reader without a key sees the ones that are there, as a reader over budget does: they cost nothing more, and nothing is called for them.
Your own OpenRouter key
Under Settings → Costs → Your own OpenRouter key, anyone can set an OpenRouter key of their own (SI-564, “bring your own key”). From then on, every paid call made for them goes out with it, and OpenRouter bills them:
- the pipeline’s stages. In a stage that makes one call for everyone who follows an item, someone with their own key gets a call of their own instead of sharing the instance’s: a summary costs one call more. Same-story checks are the exception: an item joins one story for everyone, so once readers on the instance’s key have checked it, it’s in a story and the own-key reader’s check needs no call.
- check claims, web page recipes, topics, check previews
- the agent chat: their conversation uses an OpenRouter provider of its own (
openrouter-own-<user id>), which only ever resolves their key. Setting or removing the key counts from the next message.
Without a key, an admin’s calls use the instance’s OPENROUTER_API_KEY within the budget. Anyone else’s AI features are off (who may use AI).
The budget still applies. Your own budget keeps stopping paid stages when it’s reached, as a safety net. The account’s highest budget, which an admin sets (accounts.md), doesn’t: you pay.
How the key is kept:
- Before it’s saved, it’s checked with OpenRouter (
GET /api/v1/key). A key OpenRouter refuses isn’t saved. - It’s stored encrypted, like a Raindrop token: AES-256-GCM with a key derived from
BETTER_AUTH_SECRET(crypto/secrets.ts). Changing that secret makes stored keys unreadable: calls for those users then fail (they never fall back to the instance’s key), and Settings → Costs asks them to set the key again. - It never leaves the server: the page and the API show only whether one is set and its last four characters. It’s not in the account export, and it’s never logged.
- Only a logged-in browser can set, see or remove it: no API token can (scope
session). - When OpenRouter refuses it (SI-576), because it was revoked or isn’t valid (
401) or has run out of credit (402), on a pipeline or other paid call or a chat turn, Settings → Costs says so at the top and under the key, with what to do: paste a key that works, or remove it (an admin then uses the instance’s key again; anyone else has AI off). The chat’s turn ends with the same words and a link to Settings → Costs (keyRefusedinGET /api/v1/chat). The note goes away with the next call the key pays for, or when the key is replaced or removed. A refusal from before the key was set doesn’t count. Other errors, such as a model that’s down, say nothing about the key.
Calls paid with a user’s own key are marked in the ledger (ai_calls.own_key) and count towards their spend as usual. pnpm costs check leaves them out, since OpenRouter counts them on that user’s key.
The code: packages/core/src/services/model-keys.ts.
The overview
Settings → Costs and GET /api/v1/costs show:
- Today and this month, against the budget, and what is left.
- The forecast: this month so far, plus the average of the last seven days for each day that is left. A ledger younger than a week is averaged over the days it has.
- By stage: items, calls, tokens and cost this month, and what a stage cost per item over the last 30 days.
- By model: calls, tokens and cost this month, per model that answered.
- Your checks (SI-567): what each of the user’s own checks adds to the Gate’s call, per month. An estimate, since a check rides in the Gate’s call and the ledger records the call as a whole (checks.md). Their previews are a stage of their own,
check_preview(Check previews). - Web page recipes (SI-556): making a web page’s recipe with AI,
web_recipe, one or two calls, an estimated $0.003 a recipe (not yet measured), only when you ask, at most 30 calls in 24 hours. Fetching the page later costs nothing (source-types.md). - Topics (SI-552): grouping your keywords into topics,
topics, an estimated $0.002 a grouping with 500 keywords (not yet measured), at most one a day unless you ask for more (topics.md).
Estimates
estimateCost() multiplies what each paid stage cost per item over the last 30 days by a number of items. Stages with no calls in that time are listed in unknownStages and not counted.
- Executions shows what running an item again costs, for the paid stages the user has on.
GET /api/v1/costs/estimate?items=30answers for any number of items, withfitsBudget: whether it fits in what is left this month. A future bulk action (“re-score the last 30 days ≈ $0.40”) asks it first.
Checking the ledger against OpenRouter
pnpm costs checkcompares this month’s and today’s ledger (UTC, as OpenRouter counts) with the key’s usage_monthly and usage_daily from GET /api/v1/key. It exits 1 when they differ by more than 5 % and more than a cent. The comparison only holds when the key is used by Hotaru alone, and on the server’s database (DATABASE_PATH).
A call OpenRouter returns without usage.cost is recorded at $0, with a warning in the log. The check shows it as a gap.
API
GET /api/v1/costs |
Budget, spend this month and today, forecast, by stage, by model, per item |
PUT /api/v1/costs/budget |
{ "budgetUsd": 10 }, 0–1000. Answers like GET /api/v1/costs. Needs a session or a write token |
GET /api/v1/costs/estimate?items= |
What the paid stages that are on would cost for that many items, and whether it fits the budget |
GET /api/v1/me/model-key |
Whether your own OpenRouter key is set, its last four characters, and whether OpenRouter refused it. Never the key. Browser session only |
PUT /api/v1/me/model-key |
{ "key": "sk-or-…" }: checks it with OpenRouter and sets it. 400 when OpenRouter refuses it, 502 when OpenRouter can’t be reached. Browser session only |
DELETE /api/v1/me/model-key |
Removes your key. Browser session only |
The SDK has costs(), setAiBudget(), estimateCost(), modelKey(), setModelKey() and deleteModelKey().