From Long-Running AI Agents to a Video Generation SaaS: A Full-Stack Engineering Practice
A practical exploration of building a video generation SaaS from scratch with long-running AI agents. The article covers specification-driven development, multi-tenant architecture, asynchronous video processing, task state management, credit accounting, media security, testing, observability, and the limitations of AI-assisted software development.
In recent years, the focus of AI-assisted programming has shifted from “Can it generate a page?” to “Can it independently complete a complex software engineering task?” The former is closer to code completion. The latter involves requirements clarification, architecture design, module decomposition, dependency management, exception handling, testing, deployment, and operations.
To evaluate the practical capabilities of long-running AI agents, I designed a complete experiment: building a video generation SaaS for content marketing from scratch. Users upload product images and provide basic marketing information. The system then generates content suitable for short-form video campaigns and uses a task queue to handle subsequent processing.
The value of this project is not that it produces a demonstration page. It combines several real engineering challenges: user registration and authentication, data isolation, asynchronous video generation, credit deduction, failed-task retries, media storage, and maintaining system stability as the user base grows.
1. Why Long-Running Tasks Are Harder Than Page Generation
Traditional code generation is usually local and incremental. A developer describes a function, and the tool returns an implementation. The developer describes a page, and the tool generates the corresponding components. This approach is highly efficient for small-scale development, but it does not directly solve system-level problems.
A complete SaaS product typically includes at least the following layers:
- Interaction layer: Login pages, dashboards, upload forms, task lists, and result previews.
- Business layer: Core entities such as users, projects, assets, generation tasks, credits, and orders.
- Service layer: Authentication, file processing, video generation, task scheduling, notifications, and callbacks.
- Data layer: Relational databases, object storage, caches, and task status records.
- Operations layer: Logs, monitoring, alerting, auditing, backups, and release processes.
These modules are not independent. For example, when a user clicks “Generate Video,” the frontend cannot simply wait for the API to return because video processing may take several minutes. The backend needs to create a task, deduct or reserve credits, and publish an asynchronous message. A worker then calls the generation service, updates the task status, and notifies the frontend when processing is complete.
The real challenge is therefore not writing a single API. It is keeping state transitions consistent across all modules.
2. Define the Specification Before Development
This project followed a specification-driven development process divided into four stages: research, design, implementation, and validation. The goal was not to ask an AI tool to generate the entire codebase immediately.
2.1 Define the User Journey
The minimum viable workflow was defined first:
- The user registers or signs in.
- The user creates a video generation project.
- The user uploads a product image.
- The user enters the product name, selling points, and target audience.
- The user submits a generation task.
- The user checks task progress.
- The user previews, downloads, or deletes the generated result.
This step limits the scope. If team collaboration, complex marketing templates, automated distribution, and analytics are added from the beginning, the system can become overly complicated before its core value has been validated.
2.2 Define the Domain Objects
Converting the requirements into domain objects makes the system boundary clearer:
User: The user and account status.Project: A video project created by a user.Asset: Uploaded images, audio, and video files.GenerationTask: One video generation task.CreditLedger: The credit transaction ledger.Output: The generated video and its metadata.AuditLog: Records of important operations.
Each object should have a clearly defined lifecycle. For example, a task may move through the states “pending,” “processing,” “succeeded,” “failed,” and “cancelled,” but it should not move directly from “succeeded” back to “processing.” These constraints reduce data inconsistencies under concurrent execution.
2.3 Design Acceptance Criteria
A specification should not remain at the level of natural-language descriptions. It should also include verifiable acceptance criteria:
- Unauthenticated users cannot access project data.
- Users can view only their own projects.
- Repeated submissions cannot result in duplicate charges.
- Failed tasks can be retried according to a defined policy.
- Every generated result must be linked to its original task.
- Deleting a project must not delete another user’s files.
- Every credit change must have a corresponding ledger entry.
With these conditions in place, an AI coding tool can work toward explicit goals instead of merely maximizing the amount of generated code.
3. Overall Architecture
An early-stage validation system can use a decoupled frontend and backend with asynchronous worker processes.
Browser client|vApplication API service|---------Relational database|---------Object storage|---------Message queue|vVideo processing worker|vMedia generation service
3.1 API Service
The API service handles authentication, parameter validation, business orchestration, and task creation. It should not directly perform time-consuming video processing.
After a user submits a generation request, the API should only:
- Verify the user’s identity.
- Check project and asset ownership.
- Verify the credit balance.
- Create a task record.
- Reserve or freeze the required credits.
- Publish a message to the queue.
- Return the task ID.
This keeps the API responsive and avoids request timeouts.
3.2 Database
A relational database is suitable for storing accounts, tasks, credits, and permission relationships. Key tables may include:
usersprojectsassetsgeneration_taskscredit_accountscredit_transactionsoutputsaudit_logs
The task table should record at least:
- Task ID
- User ID
- Project ID
- Current status
- Summary of input parameters
- Retry count
- Error details
- Creation, start, and completion timestamps
- Result location
The original media files should not normally be stored directly in the database. Instead, the database should store object-storage identifiers and file integrity information. This reduces database pressure and supports lifecycle management.
3.3 Data Isolation
Unauthorized access is one of the most easily overlooked risks in a multi-tenant SaaS. All business tables should include a user or tenant identifier, and every query must include the ownership condition.
The backend should not rely only on a project ID supplied by the frontend. It should perform logic equivalent to:
Find the projectConditions: project_id = request parameterAnd owner_id = current user
For larger systems, row-level security can also be added at the database layer. This ensures that even if application code omits a filter, data from another tenant cannot be returned. Database permissions, application authorization, and object-storage permissions should form multiple layers of protection.
4. Asynchronous Tasks Are the Core of a Video SaaS
Video generation is inherently time-consuming, so a synchronous API should not wait for the final result.
4.1 Task State Machine
A task can be modeled as a finite state machine:
pending -> queued -> running -> succeeded|-> failed|-> cancelled
States should transition only in permitted directions. When a worker claims a task, it should atomically update the task from queued to running to prevent multiple workers from processing the same task.
4.2 Polling and Notifications
An early version can use frontend polling:
Request the task status every few secondsIf the status is succeeded, load the resultIf the status is failed, display the error
As the number of tasks grows, server-side push or message notifications can be introduced to reduce unnecessary requests. Regardless of the approach, the frontend should not infer that a task has completed. The final state must come from the server record.
4.3 Idempotency and Retries
Network instability, process restarts, and temporary third-party service failures can all cause duplicate execution. Each task should therefore have an idempotency key. If the same request is submitted repeatedly, the system should return the existing task instead of charging the user again.
Retries should also be limited. Different error types can be handled differently:
- Network timeout: Retry a limited number of times.
- Invalid parameters: Fail immediately and ask the user to make corrections.
- Insufficient resources: Delay and place the task back in the queue.
- Internal service error: Record the context and send the task to a manual investigation queue.
5. Credit and Billing Systems Should Use a Ledger Model
A credit system should not maintain only a single balance field in the user table. Under concurrent deductions, this can lead to overcharging, undercharging, and poor traceability.
A more reliable design maintains:
- Current available balance
- Frozen balance
- Credit transaction history
A generation task can follow this process:
- Check the available credits.
- Create a reservation or freeze record.
- Convert the frozen credits into actual consumption when the task succeeds.
- Release the frozen credits if the task fails.
- Create an immutable ledger entry for every change.
Each ledger record should include the transaction type, related task, balance before the change, change amount, balance after the change, and operation timestamp. Top-ups, refunds, promotional credits, and deductions should use the same accounting model rather than having separate rules in different modules.
6. Media Processing and Security
Uploaded files should not be validated solely by their file extensions. The server should inspect the actual media format, file size, resolution, and encoding parameters. Images, audio, and video should also undergo malware scanning or content-safety checks.
Files in object storage should use unpredictable paths:
tenant/{tenant_id}/project/{project_id}/asset/{random_id}
When files are accessed, the system should preferably return short-lived signed URLs instead of exposing permanent public URLs to the client. Generated results may also require watermarks, download limits, and expiration-based cleanup.
Marketing copy entered by users may contain special characters or malicious instructions. It must therefore be subject to length limits, encoding safeguards, and content review. The generation system should treat all user-provided content as untrusted input.
7. What AI Coding Tools Are Best Suited For
The greatest value of a long-running AI agent in this type of project is not generating the entire codebase in one attempt. It is maintaining system context over time.
An agent can help with:
- Organizing requirements and identifying omissions
- Generating database entity relationships
- Establishing API contracts
- Writing basic business modules
- Adding test cases
- Checking exception paths
- Generating deployment documentation
- Locating problems from error logs
- Refactoring existing code
However, developers must retain control. Authorization, accounting, data deletion, secret management, and external service calls require detailed human review. Code that runs successfully should not automatically be considered ready for commercial use.
8. Common Risks During a Three-Hour Development Session
Long-running automated execution does not mean the process can be left completely unattended. Several problems commonly appear.
8.1 Context Drift
As the task continues, constraints defined early in the project may be forgotten. A project rules file should therefore be maintained with the directory structure, naming conventions, state definitions, prohibited actions, and acceptance criteria.
8.2 Local Fixes That Break the System
When an AI tool fixes one error, it may affect other modules. After every change, the project should run formatting checks, type checks, unit tests, and tests for critical workflows.
8.3 Pursuing Complete Features Too Early
Building many pages and decorative features first can delay validation of the core workflow. A more effective order is:
Sign in-> Create a project-> Upload assets-> Create a task-> Process asynchronously-> View the result-> Settle credits
Once the core workflow is stable, features such as templates, team collaboration, and analytics can be added.
9. Moving from an Experimental Prototype to a Commercial System
Completing a prototype does not mean the product is ready. Before production launch, the following capabilities still need to be added:
- Automated database backups and restore drills
- Task queue monitoring
- Centralized error-log search
- API rate limiting
- Centralized management of sensitive configuration
- User data deletion and export
- Administrator auditing
- Cost and resource-usage tracking
- Canary releases and version rollback
The system should also define clear operational metrics, such as task success rate, average processing time, retry rate, resource cost per task, and user retention. These metrics reveal whether the product is genuinely usable more effectively than the number of lines of code generated.
Conclusion
Building a video generation SaaS from scratch tests far more than page-generation speed. It tests whether a development process can handle complex constraints continuously, preserve architectural consistency, and turn requirements into verifiable engineering results.
Long-running AI agents offer individual developers and small teams a new way to work. They can participate in research, design, coding, testing, and documentation, allowing one person to achieve a level of coordination close to that of a small engineering team. They do not, however, replace architectural judgment, risk awareness, or pre-launch review.
A more reliable approach is to define boundaries through specifications, break down complexity through architecture, have AI tools implement the system in stages, and close the loop with testing, monitoring, and human review. Only then can AI-assisted development move beyond generating a demonstration page and enter the stage of maintainable, scalable, and operable software engineering.
