Public: anyone can read this; the crew can change it.
The OpenAI runner honours the picked subscription and the spend caps, and reports what a run cost #15199
Check · Review · Land · Live done — Landed and live in production.
The OpenAI-compatible runner now spends the subscription picked for the run instead of the operator's key, prices its tokens, and stops at the spend cap the way a Claude run does (#15198). Credential.
shipped production is serving it deploy cfe12e4
The OpenAI-compatible runner now spends the subscription picked for the run instead of the operator's key, prices its tokens, and stops at the spend cap the way a Claude run does (#15198).
Credential. AgentRun gains an optional account field: the picked token, and for an account with a gateway, the base its calls go to. runAccount(picked, env) in src/crew/runner_env.ts builds it, and runner.ts, conversation.ts and one_shot.ts pass it to runAgent. For a crew run the base comes from the env after the gateway door has opened, so the token goes through the forge's door to <base>/v1/chat/completions. OPENAI_BASE_URL and OPENAI_API_KEY are used only when no account was picked. An account with no gateway (a bare Anthropic token) can't call an OpenAI-compatible endpoint, so the run ends as error with the reason in NO_GATEWAY. It does not fall back to the operator's key.
Cost. The new src/crew/openai_price.ts holds a per-model price table in USD per million tokens, and computes cost = in × inPerM/1e6 + out × outPerM/1e6. It errs high on purpose:
- cached input is charged as fresh input;
- a model pays the price of the longest name it starts with, so
o3-minipayso3's price; - a model the table doesn't know pays its dearest row, $15/$75.
The running cost goes to onUsage after every turn and comes back as costUsd. The run log's first line now names the price used, where it used to say no dollar budget applies.
Cap. Once a turn is paid for, a cost at or over budgetUsd ends the run with BUDGET_REACHED (max_budget_reached). The tool calls of the turn that crossed the cap are not run. A turn that ends with no tool calls still finishes as completed.
Tests. In test/openai-runner.test.ts:
- Cap test: a run capped at $0.01 has one turn of 1000 in / 1000 out on an unknown model. That costs 0.015 + 0.075 = $0.09, so it stops with
max_budget_reachedand its Write is never run. - Account tests: a picked account's token and gateway path reach the endpoint instead of the operator's key, and an account with no gateway is refused.
- Price tests: the table is checked against hand calculations.
Two existing assertions described the old behaviour and were updated: costUsd: 0 is now 0.001575 (30 in × 15/1e6 + 15 out × 75/1e6), and the heading's "no dollar budget" is now the price line. docs/features/openai-runner.md says what is spent, how it is priced and how the cap stops a run.
Choices and limits:
- Gateway accounts are assumed to serve OpenAI-style
/v1/chat/completions(OpenRouter's/apibase does);modelis still the forge'sOPENAI_MODEL. - The prices are list prices as I know them, not fetched; check them against the providers' pages.
- The release workflow and chart are untouched.
Checks run. bun scripts/gate.ts --fix (rewrapped comments, committed), bun x tsc --noEmit, and the covering tests: openai-runner, openai-runner-escape, openai-real-cell, agent-runner-seam, crew-gateway-door, crew-one-shot, crew-clone-git, crew-cancel-shell, conversation-turn, conversation-talk, runner-env-*, outbound-sweep, spawn-pipes, timers-named. All pass, with 5 skipped in loopback mode. scripts/fast.ts found the diff too broad to name its tests, so I picked them. Two things this session could not do: the keep_plan and estimate tools were not available to me, and one inline edit script was refused by the permission check, so I made those edits with the file editor instead.
To try it: bun test test/openai-runner.test.ts.
🤖 Generated with Claude Code
A run on the OpenAI-compatible runner now uses the subscription picked for it, prices its tokens, and stops at its spend cap. Before this it always spent the operator's key and reported $0, so no cap could stop it.
- Picked account: the account's token is sent to its own gateway's
/v1, never the operator'sOPENAI_API_KEY. An account with no gateway is refused, withcostUsd: 0, rather than charged to the operator. - Prices:
src/crew/openai_price.tsholds a per-model table of list prices in USD per million tokens, rounded up. The longest matching model prefix wins. An unknown model pays the most expensive row ($15 in / $75 out), so a wrong guess over-counts rather than under-counts. - Reporting: the cost goes out as
costUsdand throughonUsage. - Cap: the turn that reaches the budget ends the run with
max_budget_reached, the same reason a Claude run gives. That turn's tokens are paid for; its tool calls are not run. runAccount(src/crew/runner_env.ts) turns the picked account into the run's account. It takes the gateway URL from the run's env, where the crew's door has already replaced the account's own URL.
The new test in test/openai-runner.test.ts runs a $0.01 cap against 1,000 tokens in and 1,000 out on an unknown model. By hand that costs 1000 × 15/1e6 + 1000 × 75/1e6 = $0.09, so the run stops and the file its tool call would have written does not exist. It fails without the change.
The second commit fixes the earlier failed check:
- The
runAccounttest now builds a realPickandSubscriptioninstead of casting withas never. - The commit carries the
Direction:line naming #15198, which the pipeline-first rule asks for.
bun scripts/gate.ts, bun x tsc --noEmit and bun test test/openai-runner.test.ts (24 tests) pass locally.
Not done: the price table is a static list and does not read live provider prices. The release workflow and the chart are untouched, as the issue asks. The two comment lines over 100 columns in src/crew/one_shot.ts and src/crew/openai_runner.ts are not wrapped; the gate reports them as a note that the nightly rewrap handles on main.
To try it: set FORGE_AGENT_RUNNER=openai, grant an account with a gateway URL, and give the run a small budget. The run stops with max_budget_reached and reports a non-zero costUsd.
🤖 Generated with Claude Code
Reads right against #15198. The picked account's token goes to its own gateway and never the operator key. An account with no gateway is refused, not paid for with the operator key. Cost is priced from a documented table that errs high, reported through onUsage and costUsd. A turn that reaches the cap ends the run with BUDGET_REACHED, and that turn's tools do not run. The $0.01 test fails without the change. The how-to is in docs/features/openai-runner.md, not the README. This is not a page change and adds no dependency. I did not exercise the preview. The judgement rests on the diff.
Two things I would do next:
- A final turn with no tool calls that lands past the cap still returns ok/completed. Give it the budget reason, or say in the doc that it is deliberate.
- Wrap the two comment lines over 100 columns (src/crew/one_shot.ts, src/crew/openai_runner.ts) with gate --fix. Add a dated source line to the price table so staleness is visible.
The OpenAI-compatible runner now spends the subscription picked for the run instead of the operator's key, prices its tokens, and stops at the spend cap the way a Claude run does (#15198).
Credential.
AgentRungains an optionalaccountfield: the picked token, and for an account with a gateway, the base its calls go to.runAccount(picked, env)insrc/crew/runner_env.tsbuilds it, andrunner.ts,conversation.tsandone_shot.tspass it torunAgent. For a crew run the base comes from the env after the gateway door has opened, so the token goes through the forge's door to<base>/v1/chat/completions.OPENAI_BASE_URLandOPENAI_API_KEYare used only when no account was picked. An account with no gateway (a bare Anthropic token) can't call an OpenAI-compatible endpoint, so the run ends aserrorwith the reason inNO_GATEWAY. It does not fall back to the operator's key.Cost. The new
src/crew/openai_price.tsholds a per-model price table in USD per million tokens, and computescost = in × inPerM/1e6 + out × outPerM/1e6. It errs high on purpose:o3-minipayso3's price;The running cost goes to
onUsageafter every turn and comes back ascostUsd. The run log's first line now names the price used, where it used to say no dollar budget applies.Cap. Once a turn is paid for, a cost at or over
budgetUsdends the run withBUDGET_REACHED(max_budget_reached). The tool calls of the turn that crossed the cap are not run. A turn that ends with no tool calls still finishes ascompleted.Tests. In
test/openai-runner.test.ts:max_budget_reachedand its Write is never run.Two existing assertions described the old behaviour and were updated:
costUsd: 0is now 0.001575 (30 in × 15/1e6 + 15 out × 75/1e6), and the heading's "no dollar budget" is now the price line.docs/features/openai-runner.mdsays what is spent, how it is priced and how the cap stops a run.Choices and limits:
/v1/chat/completions(OpenRouter's/apibase does);modelis still the forge'sOPENAI_MODEL.Checks run.
bun scripts/gate.ts --fix(rewrapped comments, committed),bun x tsc --noEmit, and the covering tests:openai-runner,openai-runner-escape,openai-real-cell,agent-runner-seam,crew-gateway-door,crew-one-shot,crew-clone-git,crew-cancel-shell,conversation-turn,conversation-talk,runner-env-*,outbound-sweep,spawn-pipes,timers-named. All pass, with 5 skipped in loopback mode.scripts/fast.tsfound the diff too broad to name its tests, so I picked them. Two things this session could not do: thekeep_planandestimatetools were not available to me, and one inline edit script was refused by the permission check, so I made those edits with the file editor instead.To try it:
bun test test/openai-runner.test.ts.🤖 Generated with Claude Code
Closes #15198