From Long-Running AI Agents to Video Generation SaaS: Engineering Practices for Production Systems

    Building a short-video generation SaaS platform requires far more than generating pages or calling a video model. This article explains how to design the system around asynchronous jobs, data isolation, quota accounting, retries, observability, and phased delivery. It also examines how specification-driven development and long-running AI agents can support system-level engineering rather than simple code generation.

    1. From “Generating Pages” to Delivering a Complete System

    AI coding tools have often been evaluated using straightforward metrics: how quickly they can generate a web page, whether the interface looks polished, and whether the code passes basic tests. These capabilities are useful for demonstrations, but they are not enough to support a reliable commercial system.

    Once the goal becomes building a short-video generation SaaS platform from scratch, the problem expands from isolated coding tasks into full-scale systems engineering:

    • How do users register, sign in, and sign out?
    • How are images, copy, and other assets uploaded and stored?
    • How are video generation jobs submitted, queued, and executed?
    • How does the frontend obtain progress updates when generation takes a long time?
    • How is data isolated between users?
    • Can failed jobs be retried?
    • How are user quotas deducted, and how are credits refunded after failures?
    • How can service failures be diagnosed?
    • Can the system scale horizontally as usage grows?

    The real value of AI-assisted programming is therefore not simply generating more code. It lies in understanding the goal, decomposing the work, establishing dependencies, and maintaining system context over an extended period.

    This is the fundamental difference between long-running agentic development and ordinary code completion.

    2. Why Short-Video Generation Is a Useful Validation Scenario

    A short-video generation SaaS product combines content creation, asynchronous computation, and commercial billing. That makes it a representative scenario for evaluating system design capabilities.

    E-commerce merchants generally do not care how the underlying model works. They care about business outcomes such as:

    1. Whether a usable video can be generated quickly from a product image;
    2. Whether multiple templates, languages, and styles are available;
    3. Whether the cost of each video is controllable;
    4. Whether jobs can be created in batches;
    5. Whether failed jobs can be retried automatically;
    6. Whether historical results can be viewed and downloaded again;
    7. Whether data remains securely isolated when multiple people collaborate.

    These requirements show that the core product is not merely a “video generation API.” It is a complete business system organized around generation jobs.

    From a product perspective, a minimum viable version should include at least the following modules:

    • Account and identity management;
    • Asset upload and management;
    • Video generation parameter configuration;
    • Job creation and status queries;
    • Preview and download of generated results;
    • User quota or credit management;
    • An administration console and operational monitoring.

    If the product only provides an input field and a Generate button, the result is a demonstration program rather than a SaaS product ready for real operation.

    3. Use Specification-Driven Development Instead of Coding Immediately

    To prevent AI-assisted development from becoming an uncontrolled cycle of writing and rewriting, the process can follow a specification-driven approach with three stages: discovery, architecture, and implementation.

    1. Discovery: Define Boundaries and Constraints First

    Discovery does not require extensive market research. Its purpose is to convert requirements into verifiable engineering constraints.

    Important questions include:

    • What user roles exist?
    • What inputs are included in a generation job?
    • Is video generation synchronous or asynchronous?
    • How many jobs may one user run concurrently?
    • How should generation failures be handled?
    • How long should video files be retained?
    • Should credits be deducted when a job is created or only after it succeeds?
    • Can users cancel jobs?
    • Is batch processing required?

    The more ambiguous the requirements, the more likely the generated code is to contain conflicting logic. For example, “support credit billing” does not mean that credits should simply be deducted when the user clicks a button. Duplicate submissions, network timeouts, failed jobs, and concurrent requests must also be considered.

    2. Architecture: Establish Module Boundaries

    A reliable system can be divided into the following layers:

    • Presentation layer: Handles page rendering, form submission, and status polling;
    • API layer: Handles parameter validation, authentication, and responses;
    • Business layer: Handles users, jobs, quotas, and permissions;
    • Data layer: Handles relational data, transactions, and queries;
    • Job layer: Handles asynchronous queues, retries, and scheduling;
    • Storage layer: Handles large files such as images and videos;
    • Observability layer: Handles logs, metrics, and incident alerts.

    This layered design makes it possible to replace the video generation provider, database, or deployment model later without changing the entire system.

    3. Implementation: Deliver Features in Dependency Order

    A reasonable implementation sequence is usually not to build the interface first. A better order is:

    1. Initialize the project structure;
    2. Define data models and database migrations;
    3. Implement identity and authentication;
    4. Implement asset uploads;
    5. Implement job creation;
    6. Integrate the asynchronous executor;
    7. Implement job status queries;
    8. Add quota deduction and failure rollback;
    9. Complete frontend interactions;
    10. Add tests and deployment configuration.

    This sequence reduces rework. If the entire interface is built first and the task state model or permission model later proves incorrect, substantial parts of the application may need to be redesigned.

    4. Asynchronous Video Jobs Are the Core of the System

    Video generation often takes tens of seconds or longer, so an API should not remain open while waiting for the final result. The correct approach is an asynchronous workflow:

    submit a job → process it in the background → query its status

    1. Design the Job Lifecycle

    A job should include at least the following states:

    • pending: The job has been created and is waiting to run;
    • running: The job is being processed;
    • success: The job completed successfully;
    • failed: The job failed during execution;
    • canceled: The job was canceled by the user or the system.

    State transitions should follow explicit rules, such as:

    1. pending -> running -> success
    2. pending -> running -> failed
    3. pending -> canceled

    A job should not move directly from success back to pending without an audit record. The clearer the state machine, the easier it is to investigate problems later.

    2. Why the Frontend Should Not Simply Wait

    If the frontend waits directly for the generation result, several problems may occur:

    • Requests may time out;
    • Server connections remain occupied for long periods;
    • Progress is lost when the page is refreshed;
    • Network interruptions may make users believe the job failed;
    • High concurrency can overwhelm the API service.

    A more reliable workflow is:

    1. The user submits a generation request;
    2. The server creates a job record;
    3. The server returns a job ID;
    4. A background executor claims the job;
    5. The frontend queries the status at fixed intervals;
    6. The result URL is displayed after completion.

    More complex systems can use server push mechanisms, but polling is easier to implement and is appropriate for an initial version.

    3. Idempotency and Duplicate Submissions

    Users may click the Generate button repeatedly because of network delays. Without idempotency controls, the same asset may be processed multiple times and the user may be charged repeatedly.

    One solution is to generate an idempotency key for each request and enforce a unique constraint:

    1. user ID + idempotency key = unique job

    When the server receives the same request again, it should return the original job instead of creating a new one.

    5. Data Models Should Represent Business Facts

    A maintainable data model should not be organized only around page fields. It should represent the underlying business facts.

    The basic tables may include the following.

    User Table

    Stores user identity, account status, creation time, and quota information. A quota system should not rely only on a single balance field. It should also include a quota ledger so that every increase and decrease can be traced.

    Asset Table

    Stores original images, copy, dimensions, formats, user ownership, and storage locations. Assets should be decoupled from generation jobs because the same asset may be reused for multiple videos.

    Job Table

    Stores the job ID, user ID, input assets, generation parameters, current status, failure reason, start time, and completion time.

    Result Table

    Stores generated file URLs, cover image URLs, file size, expiration time, and processing results. Separating results from jobs makes it easier to support multiple output files for a single job.

    Quota Ledger Table

    Records top-ups, deductions, refunds, manual adjustments, and other events. Every quota change should have a source, timestamp, and associated business reference ID.

    At a minimum, data protection should include the following practices:

    • Add user ownership conditions to every query;
    • Separate administrator permissions from regular user permissions;
    • Avoid permanently exposing file URLs;
    • Keep audit records for important operations;
    • Manage database connections and sensitive configuration through environment variables.

    6. Quota Deduction Is More Than a Single Update Statement

    Credit and quota systems are especially vulnerable to concurrency problems. Suppose a user has only one credit remaining and sends two requests at the same time. If the system checks the balance first and performs the deduction afterward, both requests may see a sufficient balance, resulting in overuse.

    A more reliable approach is to use a transaction and a conditional update:

    1. Begin transaction
    2. Check whether the available quota is sufficient
    3. Lock the user's quota record
    4. Write a quota deduction ledger entry
    5. Create the generation job
    6. Commit transaction

    If the background job later fails, the system must refund the quota according to the business rules. Refund operations must also be idempotent so that repeated execution does not create an incorrect balance.

    A quota system is therefore not just a balance field. It is an accounting subsystem that requires auditing, transactions, and failure recovery.

    7. The Real Value of AI Coding in Long-Running Tasks

    When a project continues for hours or longer, the value of an AI coding tool is mainly reflected in the following capabilities.

    1. Maintaining Context Over Time

    The tool must remember the existing directory structure, data models, API contracts, and unfinished work. Later changes should not break functionality that has already been implemented.

    2. Proactively Decomposing Complex Problems

    An effective workflow begins by creating a task list and implementing it according to dependency relationships, rather than generating a large amount of code immediately after receiving an instruction.

    3. Iterating Based on Error Feedback

    Real development inevitably involves dependency conflicts, failed database migrations, type errors, permission issues, and deployment problems. The tool must be able to read error messages, identify the root cause, apply a fix, and verify the result.

    4. Maintaining a System-Level Perspective

    When a field in the job table changes, the tool should also check related API parameters, background execution logic, frontend status displays, and test cases. It should not modify only one file and ignore the surrounding system.

    AI does not replace engineering judgment. Developers remain responsible for defining requirements, designing security policies, ensuring data compliance, controlling costs, and performing final acceptance testing.

    8. What Must Be Added Before Moving from an Experiment to a Commercial System

    A project completed in roughly three hours is usually an operational prototype rather than a production-ready product. A commercial system still needs to address the following areas:

    • Comprehensive automated testing;
    • API rate limiting and abuse prevention;
    • Validation of uploaded file types and sizes;
    • Sensitive-content detection;
    • Job timeouts and automatic retries;
    • Manual intervention for failed jobs;
    • Log redaction;
    • Monitoring metrics and alerts;
    • Data backup and recovery drills;
    • Cost tracking and usage analysis;
    • Staged releases and version rollback;
    • Privacy policies and data deletion mechanisms.

    In video generation, storage and compute costs can grow quickly as usage increases. The system should begin recording job duration, file size, failure rate, and per-user usage as early as possible. This information supports future pricing decisions and resource planning.

    9. Conclusion: The Next Stage of AI-Assisted Programming Is System Collaboration

    The most important progress in long-running AI agents is not simply that they write code faster. Software development itself is becoming more collaborative and system-oriented.

    In the past, a complete project often required separate people to handle product management, architecture, backend development, frontend development, testing, and deployment. Today, AI coding tools can take on part of this cross-module coordination. However, developers must still provide clear specifications, appropriate boundaries, and verifiable acceptance criteria.

    For a short-video generation SaaS product, the most important question is not whether the interface can be generated quickly. It is whether asynchronous jobs, data isolation, quota settlement, error recovery, and operational monitoring can be connected into a complete loop.

    When AI can continuously understand requirements, maintain context, execute multi-stage tasks, and revise its approach based on feedback, AI-assisted development moves beyond being a code generator and begins to function as a system engineering collaborator.