Skip to content

## Enforce per-run spend caps (cancel the run when its cost exceeds the cap) #2734

Closed

Description

@propr-dev

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

  1. A per-run cost cap in USD, resolved in this precedence order (highest wins):
    1. Explicit per-task override: a maxCostUsd field on REST task submissions (packages/api/routes/taskSubmissionRoutes.ts), on the MCP create_task tool, and on the CLI command that submits a task (propr task ... / implement command).
    2. Repository workflow file: limits.max_cost_usd in .propr/workflow.yml (extend packages/core/src/workflow/repositoryWorkflow.ts and the JSON schema docs/static/schemas/repository-workflow.schema.json; follow how limits.max_parallel_tasks is parsed, validated and clamped).
    3. Instance default: a new setting default_max_cost_usd managed in packages/core/src/config/configManager.ts alongside the ultrafix settings, exposed in the Settings UI and propr 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.
  2. Enforcement while the run is executing, not only at the end: track cumulative estimated cost for the task (sum of llm_executions rows 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 new terminalReason value cost_cap_exceeded added to packages/core/src/utils/workerStateManager.types.ts. Partial work must still be published the way a timed-out run's partial work is published today (see packages/core/src/claude/docker/dockerExecutor.ts timeout handling and src/jobs/prCommentPostExecution.ts).
  3. 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.
  4. 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 through packages/core/src/services/notificationService.ts using 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).
  5. 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.
  6. Surface the cap in the task detail API/UI next to the existing cost figure (cap, spent, percentage), and in propr task get --json.
  7. Keep LLM_COST_THRESHOLD_USD working as the alert-only threshold; document that it alerts and the new cap enforces.

Documentation

  • docs/docs/features/repository-workflow.md: document limits.max_cost_usd.
  • docs/docs/operations/metrics.md ("Cost Tracking") and docs/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 for max_parallel_tasks).
  • Run npm run lint, npm run typecheck and 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

  1. propr-dev commented on Oct 5, 2026

    @propr-dev
    ContributorAuthor

    🤖 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:

    • Agent: claude
    • Model: claude-opus-5-5
    • Branch: 2734/claude-opus-5-5-enforce-per-run-spend-cap-20261005-2330-yv3
    • Base Branch: main
    • Worktree: issue-2734-20261005-2330-claude-opus-5-5-yv3

    🔍 Track Task Execution

  2. added a commit that references this issue on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions