Skip to content

fix(billing): stop free-plan workflow runs at their usage limit mid-run - #9207

Merged
waleedlatif1 merged 7 commits into
stagingfrom
fix/workflow-mid-run-usage-cap
Oct 19, 2026
Merged

waleedlatif1 merged 7 commits into
stagingfrom
fix/workflow-mid-run-usage-cap

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Workflow runs were only checked against the usage limit at admission, and a run's cost reaches the ledger only when its log is written, so one free-plan Agent tool loop (or a Loop of Agent blocks) could keep calling hosted models long past the limit, unbilled
  • Every settled model step's billed cost (BYOK steps count $0) now adds to a run-wide unrecorded total, shared with child workflows and workflow tools billed on the same log; custom-block children keep their own
  • Before every model call (prepareConversationGeneration, which every provider loop already calls) and again after a compaction summary, a free-plan run re-judges its payer through the existing readMidRunUsageVerdict, now counting the run's unrecorded spend; it reads nothing until the run has spent and uses the cached execution gate
  • On the limit the run stops with RunUsageLimitError (non-retryable, attributed to the user so it logs at info), carrying what success would have billed: the request's settled steps, successful tool results and failed Function runs, compaction summaries, and journaled prior attempts (a continued pending call is billed once, through the journal). Providers already pass it through unwrapped via isConversationContextError, so fallbacks and block retries don't fire
  • An unreadable ledger or blocked account lets the run continue and isn't re-read for the near-limit TTL; paid plans (overage is billed), self-hosted, cancelled runs and calls with no workflow context are never read
  • Copilot callers of readMidRunUsageVerdict pass no spend and are unchanged
  • providerFailureLogLevel and the streaming tool loops now share one severity rule: a run abort first (timeout warn, Stop info), then classifyFailure attribution

Type of Change

  • Bug fix

Testing

  • providers/run-usage-limit.test.ts (21 failure-mode tests through the real hooks, executeProviderRequest and verdict code), plus compaction, workflow-tool and workflow-handler child-sharing tests; each guard reverted to confirm its test goes red
  • Provider, executor, agent/workflow handler and memory suites (2,537 tests)
  • bun run lint, check:comment-hygiene, check:test-patterns; type-check and check:audits left to CI

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Oct 19, 2026 3:10am UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 12 files

This PR changes payment, billing, or quota logic. Ultrareviews find 2.4x more serious bugs than standard reviews. Comment @cubic-dev-ai ultrareview to run one.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/providers/run-usage-limit.ts
Comment thread apps/sim/providers/conversation-history.ts
Comment thread apps/sim/providers/conversation-generation.ts
@greptile-apps

greptile-apps Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] The PR appears safe to merge; no new blocking issue remains.

Summary

Stops free-plan Agent runs before another model call once recorded usage plus the run’s unsettled model spend reaches the limit.

  • Free-plan runs stop before a model call that reaches their usage limit.
  • A run's unsettled spend includes model work and costs from children billed on its log.
  • Streaming tool loops and block streams log user stops at the right severity.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Settled model step] --> B[Add cost to request and shared run total]
  B --> C[Before next model call]
  C --> D{Free-plan run has spent?}
  D -- No --> G[Continue]
  D -- Yes --> E[Read recorded usage plus run total]
  E --> F{Limit reached?}
  F -- No --> G
  F -- Yes --> H[Stop without retry]
  H --> I[Bill completed request work]
Loading

Reviews (7) · Last reviewed commit: "fix(billing): charge a continued Functio..." · Reviewed by Greptile

Comment thread apps/sim/providers/run-usage-limit.ts Outdated
Comment thread apps/sim/providers/conversation-generation.ts
Comment thread apps/sim/providers/conversation-history.ts
@waleedlatif1
waleedlatif1 force-pushed the fix/workflow-mid-run-usage-cap branch from 56d9599 to 2d47822 Compare October 18, 2026 23:09
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 13 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/providers/run-usage-limit.ts Outdated
Comment thread apps/sim/providers/conversation-generation.ts
Comment thread apps/sim/executor/handlers/workflow/handler.ts
@waleedlatif1
waleedlatif1 force-pushed the fix/workflow-mid-run-usage-cap branch from 2d47822 to e10de80 Compare October 18, 2026 23:22
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 17 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/providers/run-usage-limit.ts Outdated
@waleedlatif1
waleedlatif1 force-pushed the fix/workflow-mid-run-usage-cap branch from e10de80 to ebabfe5 Compare October 18, 2026 23:35
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 17 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

…ntinued calls once

- RunUsageLimitError joins the errors every provider already rethrows unwrapped, replacing the
  per-request side channel that could mask an unrelated later failure
- The stop is attributed to the user, so provider, stream-loop and stream-read logs record it at
  info; a run abort is classified before attribution so a run timeout still logs at warn
- A pending call continued from an earlier attempt is billed through the journal only
- The stop bills tool results the way success does: successful results and failed Function runs
@waleedlatif1
waleedlatif1 force-pushed the fix/workflow-mid-run-usage-cap branch from ebabfe5 to 9d7aa68 Compare October 19, 2026 02:47
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 25 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/providers/run-usage-limit.ts
Comment thread apps/sim/providers/index.ts
Comment thread apps/sim/providers/run-usage-limit.ts
… limit once

- A turn an earlier process journaled adds its model spend to the run's unrecorded total the first
  time this run opens it; reopening the turn (fallback model, block retry) never counts it again
- Type the continued-call test's tool as a ProviderToolConfig
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 26 files

Confidence score: 3/5

  • In apps/sim/providers/index.ts, a failed function_execute replay that proceeds to a model call is charged twice. Clear the failed-function accumulator before adding priorConversationUsage.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/sim/providers/index.ts">

<violation number="1" location="apps/sim/providers/index.ts:353">
P1: A failed `function_execute` replay is charged twice when this request continues to a model call: once through `failedFunctionToolCost` and again through `priorConversationUsage`. Clear the failed-function accumulator after replaying pending calls, before the current provider response is assembled.

(Based on your team's feedback about journaled agent spend.)</violation>
</file>

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/providers/index.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 26 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit c0cf608 into staging Oct 19, 2026
53 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/workflow-mid-run-usage-cap branch October 19, 2026 03:21

This branch was previously deployed

1 inactive deployment
Preview — ec98c116 Deployed Oct 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant