Repository navigation
Description
Enforce per-run spend caps (cancel the run when its cost exceeds the cap)
Context
ProPR estimates the cost of every LLM call (packages/core/src/services/pricingService.ts, packages/core/src/utils/llmMetrics.ts) and stores it per call in llm_logs / llm_executions, but it never enforces a limit. The only threshold today is LLM_COST_THRESHOLD_USD (env only, default 10.00) in checkCostThreshold() in packages/core/src/utils/llmMetrics.ts, which pushes a "high-cost alert" onto the Redis list llm:metrics:alerts:highcost and logs a warning. Nothing is stopped, and the alert is only visible through GET /api/llm-metrics. The README now promises operators visibility and control over what every run costs; a runaway run can still spend without limit until the 24-hour agent timeout.
packages/shared/src/usageTypes.ts contains an exported but unused AgentTankConfig type with maxBudgetUsd, billingPeriod, warningThreshold, hardLimitThreshold, modelBudgets, maxTokens, maxRequests. It is scaffolding that nothing references; either reuse the relevant field names or delete the dead type as part of this work (do not leave two competing shapes).
Requirements
- A per-run cost cap in USD, resolved in this precedence order (highest wins):
- Explicit per-task override: a
maxCostUsdfield on REST task submissions (packages/api/routes/taskSubmissionRoutes.ts), on the MCPcreate_tasktool, and on the CLI command that submits a task (propr task .../ implement command). - Repository workflow file:
limits.max_cost_usdin.propr/workflow.yml(extendpackages/core/src/workflow/repositoryWorkflow.tsand the JSON schemadocs/static/schemas/repository-workflow.schema.json; follow howlimits.max_parallel_tasksis parsed, validated and clamped). - Instance default: a new setting
default_max_cost_usdmanaged inpackages/core/src/config/configManager.tsalongside the ultrafix settings, exposed in the Settings UI andpropr settings. Empty/0 means "no cap".
A malformed or negative value must be treated as "no cap" with a warning logged — a typo must never cancel every run at $0.
- Explicit per-task override: a
- Enforcement while the run is executing, not only at the end: track cumulative estimated cost for the task (sum of
llm_executionsrows for the task, including the implementation agent calls and any summarization/analysis calls attributed to the task). When the cumulative cost crosses the cap, stop the running container through the same path the UI "stop" action uses (packages/api/routes/dockerRoutes.ts/ the existing stop logic), with a newterminalReasonvaluecost_cap_exceededadded topackages/core/src/utils/workerStateManager.types.ts. Partial work must still be published the way a timed-out run's partial work is published today (seepackages/core/src/claude/docker/dockerExecutor.tstimeout handling andsrc/jobs/prCommentPostExecution.ts). - Scope: implementations, PR follow-ups,
/fix, ultrafix cycles and reviews. Goals keep their own controls; do not change goal behaviour in this task beyond making the cap value available if trivial. - Events and notifications: write a task timeline event (
budget.exceeded-style) that records the cap, the observed cost and the resolution source (override / workflow.yml / instance default). Send an Inbox notification throughpackages/core/src/services/notificationService.tsusing the existing notification categories conventions. Post a short GitHub comment on the linked issue/PR saying the run was stopped at the cap (reuse the existing failure-comment helpers; keep it to a few lines). - Retries share the budget: if ProPR re-queues the task (provider usage-limit re-queue in
src/jobs/errorHandlers.ts, BullMQ attempts), the cap for the retry is the original cap minus what earlier attempts already spent on that task. - Surface the cap in the task detail API/UI next to the existing cost figure (cap, spent, percentage), and in
propr task get --json. - Keep
LLM_COST_THRESHOLD_USDworking as the alert-only threshold; document that it alerts and the new cap enforces.
Documentation
docs/docs/features/repository-workflow.md: documentlimits.max_cost_usd.docs/docs/operations/metrics.md("Cost Tracking") anddocs/docs/operations/settings-locations.md: document the new setting, the precedence order, the retry behaviour and the new terminal reason.docs/docs/features/execution-safety.md: add a short "Spend caps" section.- CHANGELOG.md Unreleased entry.
Tests
- Unit tests for cap resolution precedence (override > workflow.yml > instance default), malformed values → no cap, and retry budget arithmetic.
- A test that drives the cumulative-cost check past the cap and asserts the stop path is invoked once, the terminal reason is
cost_cap_exceeded, and the timeline event is written. - Workflow schema validation test for
limits.max_cost_usd(follow existing tests formax_parallel_tasks). - Run
npm run lint,npm run typecheckand the affected suites. Keep existing tests passing and keep public CLI/MCP signatures backward compatible (the new field is optional everywhere).
Out of scope
Per-period (daily/weekly) repository budgets and per-model budgets — a follow-up issue will cover those once the per-run cap exists.
Submitted by @integry through ProPR.
Activity
propr-dev commentedon Oct 5, 2026
🤖 AI processing has started for this issue using claude agent with claude-opus-5-5 model.
I'll analyze the problem and work on a solution. This may take a few minutes.
Processing Details:
claudeclaude-opus-5-52734/claude-opus-5-5-enforce-per-run-spend-cap-20261005-2330-yv3mainissue-2734-20261005-2330-claude-opus-5-5-yv3🔍 Track Task Execution
I've implemented per-run spend caps for #2734. A run is now stopped o…