Building a Video Generation SaaS with AI Coding on Long-Horizon Tasks

    This article is a plan breakdown rather than a tool review: it walks through how to build a production video-generation SaaS with AI coding under long-horizon tasks, where the challenge is not producing code but sustaining engineering consistency across hours, many turns, and dozens of files. It covers an async-first backend with idempotent credit billing and an append-only ledger, PostgreSQL row-level security, an async task queue with a clear state machine and dead-letter handling, auth and captcha protections, and spec-driven development (SDD). It also catalogs common pitfalls such as context drift, RLS misconfiguration, missing idempotency, concurrent over-charging, and SSRF risk, and closes with acceptance criteria and an FAQ. No reproducible benchmark data is disclosed, so the article deliberately avoids tool comparisons, timing, success rates, or intervention counts.

    1. Why long-horizon tasks are the real dividing line for AI coding

    Over the past two years, most AI coding tools have been good at short-horizon tasks: generating a single page, writing scripts, completing functions, and building quick prototypes. These tasks are narrow in scope, short in context, and easy to verify immediately. Real commercial software does not work that way. A video-generation SaaS aimed at content or cross-border e-commerce touches many modules at once: authentication, asset uploads, async generation, billing and deductions, task queries, failure retries, and monitoring/alerting. A single change to one table schema can ripple into auth, billing, and front-end state.

    A note on scope: this article is a plan breakdown, not a tool review. Since no reproducible test records are disclosed, it does not provide tool comparisons, latency figures, success rates, or counts of manual interventions. Instead, it focuses on how a complex system should be designed under long-horizon tasks. The core of a long-horizon task is not “can it be written,” but whether engineering consistency can be maintained across hours, multiple rounds of interaction, and dozens of evolving files.

    These tasks usually present three kinds of challenges:

    1. Context window and memory decay: As files, logs, and decisions accumulate, architectural conventions agreed early on can be forgotten, and later code tends to drift from the original conventions.
    2. Task decomposition and dependency management: System-level requirements must first be split into subtasks with an explicit dependency order, for example: create tables first, then the data-access layer, then business APIs, and finally wire up the queue and front end.
    3. A self-correction loop: Video generation takes a relatively long time and does not have a low failure rate, so the system must be able to detect failures, roll back, and retry, rather than carrying an error all the way to the end.

    So whether AI coding can take on a complex SaaS is decided not by how many lines of code it generates, but by whether it can keep advancing against the same set of architectural constraints.

    2. Why choose video-generation SaaS as the complex task?

    Video-generation SaaS is chosen not because it “looks flashy,” but because it is naturally complex enough to stress-test long-horizon development ability. For content and cross-border e-commerce merchants, who often do not want to learn complex tools, only three things matter: unit cost, throughput, and ease of use.

    Merchant concern Corresponding system capability
    Unit cost Credit billing, pre-charge, refund, idempotency, ledger reconciliation
    Throughput Async queue, consumer scaling, failure backoff, priority
    Ease of use Auth, task creation, status queries, failure messaging

    Unit cost fluctuates with the model and region, so always refer to the vendor’s latest pricing. At the system level, no single video price should be hardcoded. Instead, cost should be calculated from parameters, model, and region, and every deduction, refund, and top-up should be recorded in the credit ledger.

    The value of this object is that it simultaneously involves full-stack APIs, a relational database, async tasks, object storage, auth, billing, monitoring, and error handling, which reflects the difficulty of “AI coding a system” fairly realistically.

    3. Why the backend should be async-first

    Video generation usually takes seconds to minutes, sometimes longer. If an API request synchronously waits for the generation result, it will quickly exhaust connections, time out, and block the front end, and failure handling becomes difficult. A more sensible approach is async-first: the API layer only receives the task, validates parameters, authenticates, enqueues, and returns a task ID, while the actual generation work is handed to a background consumer.

    A reference main flow looks like this:

    1. async def create_video_task(user_id: int, client_request_id: str, image_url: str, params: dict):
    2. # 1. Authenticate and validate params; validate the image URL scheme, domain/IP to prevent SSRF
    3. await assert_user_active(user_id)
    4. validate_asset_url(image_url)
    5. # 2. Idempotency key: the same user + client_request_id creates only one task
    6. existing = await task_repo.get_by_client_request_id(user_id, client_request_id)
    7. if existing:
    8. return {"task_id": existing.id, "status": existing.status}
    9. cost = calculate_cost(params)
    10. async with db.transaction():
    11. # 3. Conditional update for atomic debit; 0 affected rows means insufficient balance
    12. updated = await db.execute("""
    13. UPDATE users
    14. SET balance = balance - :cost
    15. WHERE id = :uid
    16. AND balance >= :cost
    17. """, {"cost": cost, "uid": user_id})
    18. if updated.rowcount == 0:
    19. raise InsufficientCredits
    20. # 4. Write the pre-charge entry into the credit ledger; the ledger is append-only
    21. await credit_ledger.append(user_id, -cost, client_request_id)
    22. # 5. Create the task; a unique index is recommended as a fallback against concurrency
    23. # CREATE UNIQUE INDEX ON video_tasks (user_id, client_request_id);
    24. task = await task_repo.create(
    25. user_id=user_id,
    26. client_request_id=client_request_id,
    27. status="pending",
    28. payload={"image": image_url, **params},
    29. )
    30. # If the unique index conflicts, return the existing task and roll back
    31. # this transaction to avoid a duplicate pre-charge
    32. # 6. If enqueueing fails, roll back the pre-charge or route it to a compensation queue
    33. try:
    34. await task_queue.enqueue("render_video", task.id)
    35. except EnqueueError:
    36. await compensation_refund(user_id, task.id, cost, client_request_id)
    37. raise
    38. return {"task_id": task.id, "status": task.status}

    Several points are key in this design:

    • The idempotency key must be defined up front: Repeated client clicks, network retries, and front-end refreshes can all resubmit. Using client_request_id together with a database unique index prevents duplicate tasks and duplicate charges.
    • Pre-charge and the ledger stay in the same transaction: First check the balance, then do a conditional debit, then write the ledger, then create the task. Any step that fails must roll the whole thing back.
    • Enqueue failure must be compensated: If enqueueing fails after a pre-charge, you cannot leave a state where “money was deducted but the task never ran.” Roll back the pre-charge or move it to a compensation queue.
    • Only successful tasks are ultimately charged: A pre-charge is a credit reservation; billing happens after the task is confirmed successful. Failed tasks should be refunded, with the reason and status preserved in the ledger.

    4. Database and row-level security: how to configure RLS so it actually prevents unauthorized access

    Commercial SaaS usually relies on a relational database to hold core data such as users, tasks, assets, credit ledgers, and open-API keys. The core tables can include:

    • users: users and balance
    • video_tasks: video-generation tasks
    • assets: asset and output metadata
    • credit_ledger: credit ledger
    • api_keys: open-API keys

    One important point in database design is Row-Level Security (RLS). It pushes “data isolation” down to the database layer and reduces the burden of repeatedly hand-writing user filters in application code. But note that RLS is not a set-and-forget switch; it depends on correct roles, policies, and connection handling.

    In PostgreSQL, the following conditions should be met simultaneously:

    1. The application connects to the database with a non-owner role;
    2. Row-level security is enabled and forced on the target table;
    3. The application role is granted the necessary schema and table privileges;
    4. The current user is set at the start of each request;
    5. Connection pooling does not reuse a connection across users.

    A reference configuration:

    1. ALTER TABLE video_tasks ENABLE ROW LEVEL SECURITY;
    2. ALTER TABLE video_tasks FORCE ROW LEVEL SECURITY;
    3. CREATE POLICY user_owns_own_tasks ON video_tasks
    4. TO app_user
    5. USING (user_id = current_setting('app.user_id', true)::int)
    6. WITH CHECK (user_id = current_setting('app.user_id', true)::int);

    Privileges also need to be granted, for example:

    1. GRANT USAGE ON SCHEMA public TO app_user;
    2. GRANT SELECT, INSERT, UPDATE ON TABLE video_tasks TO app_user;

    At the start of an application request, run something like:

    1. SET LOCAL app.user_id = 'current user ID';

    And each connection should serve only one user; the connection pool must not reuse a connection across users.

    What can RLS do? It can block missing filters in SELECT, UPDATE, and DELETE statements, and through WITH CHECK it can prevent inserting or updating data that does not belong to the current user. But it cannot replace all business validation. If a policy is misconfigured, role privileges are wrong, or multiple policies stack up, privilege escalation can still occur. So RLS should be used together with least-privilege queries in the business layer, API authorization, and parameter validation.

    In addition, the credit ledger is best kept append-only. Each deduction, refund, and top-up records an immutable entry. The balance can come from summing the ledger, or from a materialized balance with periodic reconciliation. That way, even if something goes wrong, the ledger can be traced.

    5. Async task queues: how to avoid infinite retries and deadlocks

    Once generation tasks enter the queue, a group of consumers pulls and executes them. The task state machine should stay clear and clearly distinguish success, failure, retryable, and dead-letter.

    Recommended state machine:

    1. pending → processing → succeeded
    2. processing → failed
    3. failed → pending (retryable and backoff elapsed)
    4. failed → dead_letter (exceeds max retries)

    If a task exceeds the maximum number of retries, it should move to a dead-letter store and support manual intervention. The dead-letter store should include at least the task ID, user ID, failure reason, retry count, last error, creation time, and processing status.

    How does the client learn the result? There are two common approaches:

    • Polling: The client queries task status on an interval. Simple to implement and suitable for the starting phase, but the query frequency must be controlled to avoid overwhelming the API.
    • Callback or push: The system actively notifies the client when a task completes. Better real-time behavior, but you must verify the callback source, handle callback failures, and prevent duplicate notifications.

    Whichever approach you choose, two principles apply:

    1. Processing must be idempotent: If the same task is consumed repeatedly, it must not regenerate, re-charge, or rewrite the result.
    2. Failures retry a limited number of times with backoff: Use exponential backoff with jitter, and set a visibility timeout so that a task is not held for a long time if a consumer stalls.

    6. Auth, captcha, and billing: how to meet the commercial baseline

    Commercial platforms cannot avoid login, captcha, tokens, and billing. These security details cannot be skipped.

    1. Login and captcha security

    Phone-number-plus-SMS-code login is common, but it must be reinforced with:

    • Add image or behavior verification before login to reduce the risk of SMS bombing.
    • Per-phone-number send-rate limits, for example, no repeated sends within a short window for the same number.
    • A per-IP daily send quota to prevent scripted bulk calls.
    • Short-lived, single-use verification codes that are invalidated immediately after successful verification.
    • A limit on incorrect verification attempts, with lockout or an escalated challenge after a threshold.
    • Encrypt phone numbers at rest, and always mask them in logs so full phone numbers never enter logs.

    2. Token and key management

    After successful login, you can issue a short-lived access token and a refresh token, for example a JWT-based signed-token scheme. Note that:

    • The access token should have a short expiration.
    • The refresh token should be stored separately and support revocation.
    • The signing key must not be hardcoded or committed to the repository.
    • Keys should support rotation with a smooth cutover mechanism.

    3. Concurrent deductions

    Deductions are error-prone under high concurrency. Two requests deducting at the same time can cause over-charging; two requests retrying at the same time can also double-deduct. In engineering terms, put the balance deduction, ledger write, and task status update in the same transaction.

    A conditional update can be used:

    1. UPDATE users
    2. SET balance = balance - :cost
    3. WHERE id = :uid
    4. AND balance >= :cost;

    If the number of affected rows is 0, the balance is insufficient, so reject the request directly. This merges “check the balance” and “deduct the balance” into an atomic operation and avoids the race condition of read-then-write.

    At the same time, watch out for hot rows. If all requests deduct from the same platform account, that creates a hot row and hurts throughput. Common approaches include splitting accounts with async aggregation, or accounting separately by user, channel, or model. But whichever approach is used, the balance deduction, ledger write, and task creation must remain consistent.

    7. Spec-driven development: how to keep AI from drifting on long-horizon tasks

    Faced with a complex system, asking AI to write code directly from the start is easy to lose control of. A more reliable approach is spec-driven development (SDD). Its core is not “let the AI finish writing first and figure it out later,” but first forming a reviewable spec, then having the implementation advance against that spec.

    Three phases can be used:

    1. Research: Sort out who the users are, what the core flows are, how data moves, and which boundaries must be defended, forming a requirements and terminology list.
    2. Architecture: Define system layering, module division, database table structure, API contracts, task queues, auth, billing, and error codes, forming architecture and interface documents.
    3. Development: Code strictly against the documents from the first two steps, implement and self-test module by module; after finishing each module, re-read the architecture document to confirm nothing has drifted.

    The value of SDD is that the spec is the anchor in a long-horizon task. When a session runs for hours and code and logs keep growing, early conventions are easily forgotten. At that point, the architecture document serves as the criterion: whenever you are unsure, go back to the spec and align.

    8. Engineering practice: modules, key technologies, and acceptance criteria

    For a production video-generation SaaS, modules, key technologies, and acceptance criteria can be laid out in a table for item-by-item review.

    Module Key technologies Acceptance criteria
    API layer Async-first, auth, param validation, idempotency key, SSRF protection Duplicate requests don’t create duplicate tasks or double-charge; illegal URLs are rejected
    Data layer Relational DB, RLS, credit ledger, unique index Cross-user access is denied; balance and ledger reconcile
    Task layer Message queue, state machine, dead letter, exponential backoff Failed tasks retry a limited number of times; duplicate consumption has no side effects
    Storage layer Object storage, metadata, signed URLs, upload limits Signed URLs expire; file type and size are controlled
    Observability trace_id, logs, metrics, alerts Traces per task; queue backlog, failure rate, and latency have alert thresholds

    Assets and outputs are best kept in object storage, with the database holding only metadata and access paths. The upload API should validate file type, size, and extension; the download API should preferably use time-limited signed URLs to avoid long-term public access.

    Observability cannot only look at “whether the service is alive”; it must also look at whether business tasks are healthy. Generate a task-level trace_id for each task, covering the API, queue, consumer, and storage calls. Monitoring metrics should include at least:

    • Task creation count
    • Task success rate
    • Average task latency
    • Queue backlog length
    • Failure rate
    • Dead-letter count
    • Charge failure count
    • Refund failure count

    If a background task fails but the front end only shows “processing,” the user experience is poor. So every task must have a clear status, error information, retry count, and a queryable progress state.

    9. Common pitfalls and how to avoid them

    1. Context drift: Late in a long task, early conventions are easily ignored. Fix: write key conventions into the spec document and re-read it at each phase.
    2. RLS misconfiguration: If the app connects with an owner role, or does not set the current user, RLS may not take effect or may block incorrectly. Fix: define the application role clearly, force RLS, and set the user context at the start of each request.
    3. Missing idempotency: Repeated submissions and network retries both create duplicate tasks. Fix: a client request ID plus a database unique index.
    4. Concurrent over-charging: Read-then-write is prone to races. Fix: conditional updates, row locks, and the same transaction.
    5. Infinite queue retries: Failed tasks retrying endlessly amplify failures. Fix: a max retry count, exponential backoff with jitter, and a dead-letter store.
    6. Unvalidated image URLs: Letting the server directly fetch a user-provided URL can carry SSRF risk. Fix: restrict the scheme, domain, and IP, and block internal-network addresses.
    7. Captcha abuse: An SMS API without rate limits easily produces cost and complaints. Fix: image/behavior verification, rate limits, and IP quotas.
    8. Invisible failure status: Users don’t know why a task failed. Fix: error codes, error messages, retry counts, and a support entry point.

    10. Frequently asked questions

    Q1: What is SDD?

    SDD is spec-driven development: write the requirements, terminology, architecture, and data design first, then move into coding. It suits long-horizon, multi-module, strongly constrained system development.

    Q2: How do you treat context drift?

    Turn key conventions into a spec document and a checklist, for example unified error codes, field naming, the permission model, billing rules, and the queue state machine. Re-read the document and accept against it after each module.

    Q3: RLS or business filtering, which one?

    It is not either/or. The business layer should do least-privilege queries, and the database layer should use RLS as a backstop. RLS reduces the risk of missing filters, but policy configuration, role privileges, and connection handling must be correct.

    Q4: What should be tested before AI-generated code goes live?

    At least test functionality, permissions, idempotency, concurrent deductions, async retries, failure refunds, URL security, captcha rate limiting, log masking, and monitoring alerts. Complex systems should also run load tests and manual review in a canary environment.

    Q5: Can you write a long-horizon AI coding article without test data?

    Yes, but the boundary must be clear. Without test records, it should be written as a plan breakdown, an architecture checklist, or engineering practice, rather than a tool review, and it should avoid misleading claims like “tested,” “ran through,” or “done in a day” that lack evidence.

    11. Conclusion

    AI coding is moving from “generating code” to “taking on systems engineering.” In long-horizon tasks, what matters is not whether a single generation of code looks good, but whether developers can constrain the agent with specs, decompose complex requirements into modules, and use idempotency, transactions, RLS, async queues, monitoring, and manual review to keep the system reliable.

    For complex businesses like a video-generation SaaS, AI coding can significantly lower implementation cost, but it cannot replace security review, load testing, and pre-launch checks. A more reasonable collaboration is: developers own designing the spec, splitting tasks, and reviewing output; the agent takes on repetitive implementation and continuous iteration. Only when the spec, engineering, and security boundaries are designed together can AI coding truly be used for commercial systems.