Skip to content

fix(billing): settle hosted fal generations to their exact cost - #9750

Merged
waleedlatif1 merged 3 commits into
stagingfrom
fix/falai-exact-cost-settlement
Nov 6, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
fix/falai-exact-cost-settlement

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Hosted fal generations are billed at completion from fal's per-request receipt when it is ready, else from fal's per-call historical estimate or a fixed floor when the estimate is rate-limited. fal records the receipt minutes after the request completes, so most generations were billed on the estimate, and token-priced models (Seedance) often cost several times the per-call average
  • New falai-cost-settlement Trigger task: a generation billed on an estimate schedules a settlement (2 min delay, retried with backoff for about an hour) that reads fal's billing event for the request and records the shortfall as one ledger row. The row's event key is the fal request id, so retries never bill twice; an estimate above the exact charge stays billed, since the ledger records charges only
  • Wired for the workflow/API hosted tools (video_falai, image_generate, through a declarative hosting.exactCostSettlement: 'falai' read by executeTool, at the tool's markup) and Chat's generate_video/generate_audio (at cost, matching how Mothership bills fal media). The hosted key travels as its env var name, never its value, so the task reads the key that created the request
  • The video and image fal poll loops now cancel the queued request when they stop waiting (timeout, abort, status error) before fal reports a terminal status, so an abandoned request is not run and charged; this reuses the existing best-effort cancelFalRequest
  • Settlement runs only on hosted, billing-enabled deployments with Trigger.dev configured

Type of Change

  • Bug fix

Testing

  • lib/tools/falai-settlement.integration.ts (real Postgres, fal billing-events stubbed): the shortfall is billed exactly once across repeated settlements (fails with a per-attempt ledger key), nothing is billed while fal has no receipt, and a missing hosted key refuses without calling fal
  • lib/internal/image/operations.test.ts: polling that stops before completion cancels the fal request (fails without the cancel)
  • lib/media/falai.test.ts, lib/mothership/tools/server/media/*, lib/internal/video, tools/index.test.ts: existing suites updated for the cost recorder's hosted-key argument
  • biome, check:unused-exports, check:test-patterns, check:source-conventions, check:comment-hygiene, check:utils, check:explicit-any, check:file-names, check:boundaries, check:client-boundary, check:tool-registry-boundary; type-check and full audits in 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 Nov 6, 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 Nov 6, 2026 8:17pm UTC

Request Review

@greptile-apps

greptile-apps Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] The reviewed changes appear safe to merge.

Summary

Adds delayed settlement for hosted fal generations initially billed from estimates.

  • Hosted Fal.ai generations settle to their exact cost.
  • Fal.ai image and video requests are cancelled when polling stops early.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Hosted fal generation completes] --> B{Exact receipt ready?}
  B -->|Yes| C[Bill receipt cost]
  B -->|No| D[Bill estimate, including zero]
  D --> E[Schedule settlement after two minutes]
  E --> F{Receipt ready?}
  F -->|No| G[Retry with backoff]
  G --> F
  F -->|Yes| H{Exact cost exceeds estimate?}
  H -->|No| I[Leave original charge unchanged]
  H -->|Yes| J[Refresh billing period if it ended]
  J --> K[Record shortfall with stable event key]
Loading

Reviews (4) · Last reviewed commit: "fix(billing): settle zero fal estimates ..." · Reviewed by Greptile

Comment thread apps/sim/lib/tools/falai-settlement.ts Resolved

@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 19 files

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/internal/video/client.ts Resolved
@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 Nov 6, 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.

Comment thread apps/sim/tools/index.ts Resolved

@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 19 files

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

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/tools/index.ts Resolved
@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 Nov 6, 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 20 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 2441e8d into staging Nov 6, 2026
61 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/falai-exact-cost-settlement branch November 6, 2026 20:20

This branch was previously deployed

1 inactive deployment
Preview — 9f36ce13 Deployed Nov 6, 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