Building a Video Generation SaaS with an AI Agent: From Requirements to Full-Stack Implementation
This article explores how an AI agent can handle long-horizon, system-level software development by building a video generation SaaS from scratch. It covers specification-driven development, backend architecture, PostgreSQL row-level security, asynchronous task processing with Celery and Redis, authentication, credit management, reliability patterns, and the practical limitations encountered during implementation.
As large language model agents continue to evolve, software development is undergoing a fundamental transformation. Recent frontier models are no longer positioned primarily as tools for “writing web pages.” Instead, their capabilities increasingly center on agentic behavior, long-horizon tasks, and system engineering.
This shift means a model can do more than generate isolated code snippets. It can decompose complex system requirements, design multiple components, and maintain context throughout hours of automated development—much like an experienced software architect.
1. Why Use a Video Generation SaaS as the Test Case?
Many developers already use AI to build small utilities or demo pages. However, these projects do not fully demonstrate the potential of AI-assisted programming. A commercial SaaS platform introduces a much higher level of complexity:
- How should the backend services be structured?
- How should the database support multi-tenancy and row-level security?
- How should video jobs that take several minutes be managed with asynchronous queues and polling?
- How should authentication, credit deductions, and order or task states work together?
These are not simply coding questions. They are system architecture problems.
Video generation is also a practical business scenario. Many cross-border sellers need automated tools to produce large volumes of promotional videos, while traditional workflows often require too much technical knowledge. Merchants are usually less concerned with implementation details than with practical outcomes: the cost per video, daily production capacity, and whether the platform is ready to use.
A SaaS product that allows users to upload images and automatically generate promotional videos therefore provides both commercial relevance and a demanding test of an AI agent’s ability to handle real-world engineering work.
2. The Experiment and Prompt Design
The experiment followed a specification-driven development (SDD) process. The model was instructed to follow a strict sequence:
- Research and clarify the requirements.
- Design the architecture.
- Implement the system module by module.
The initial prompt defined the project goals, technology stack, and core requirements:
- Python and FastAPI
- PostgreSQL
- Redis
- Celery
- Phone-based verification-code login
- JWT authentication
- A user credit system
- An asynchronous video-generation task queue
The model was also instructed to produce a system architecture diagram and a database entity-relationship diagram before beginning modular implementation.
The development process took approximately three hours. Because increased user demand slowed API responses, the actual elapsed time was longer. During the process, the model demonstrated strong context continuity. After completing the database design, it was able to reference previously defined tables and function names when generating API routes and business logic, without requiring those definitions to be repeated constantly.
This long-horizon capability is one of the major differences between an AI agent designed for system-level work and a conventional code-completion assistant.
3. System Architecture and Key Technical Decisions
3.1 Layered Backend Architecture
The backend followed a conventional layered design:
- API layer: FastAPI provides RESTful endpoints for registration, login, credit queries, video-generation requests, and task-status queries.
- Business logic layer: This layer encapsulates core rules, including credit validation, file-upload checks, and video-generation parameter assembly.
- Data access layer: SQLAlchemy is used to interact with PostgreSQL. The main entities include users, tasks, image assets, and orders.
- Asynchronous task layer: Celery and Redis process time-consuming video-rendering requests. After the API submits a task, it immediately returns a
task_id. The frontend then polls the status endpoint to retrieve the result.
The generated implementation included a finite state machine for task processing:
pending → processing → completed / failed
It also added retry logic and timeout handling, reflecting concerns normally associated with production environments rather than simple prototypes.
3.2 Row-Level Security and Tenant Isolation
A SaaS platform must isolate data belonging to different users. The model used user_id as a foreign key and applied PostgreSQL Row-Level Security (RLS) to enforce isolation at the database level.
RLS was enabled when the tables were created, with a policy similar to:
USING (user_id = current_setting('app.current_user_id')::UUID)
At the API layer, middleware injects the current user context through:
SET app.current_user_id = ...
This approach reduces the risk of data exposure caused by accidentally omitting a WHERE clause in application code.
The design also included indexing improvements:
- A composite index on
user_idandcreated_atto speed up task-list queries. - A partial index on
statusto improve frequently executed status lookups.
3.3 Asynchronous Queues and Status Polling
Video generation can take anywhere from 30 seconds to several minutes. The proposed workflow is:
- The frontend submits a generation request.
- The API creates a task record with
status = pending. - The API triggers a Celery task using
delay(). - The worker generates the video, uploads it to object storage, and updates the task to
status = completed. - The generated video URL is stored with the task.
- The frontend polls the task-status endpoint every two seconds.
If processing fails, the system records the error, updates the task to status = failed, and triggers a credit refund.
The model also implemented idempotency safeguards. The task table includes a unique request_id constraint to prevent duplicate submissions. Celery is configured with acks_late and task_reject_on_worker_lost to reduce the risk of losing tasks when workers terminate unexpectedly.
3.4 Authentication and Credit Management
The authentication flow uses phone-based verification codes. The backend calls an SMS provider to send a randomly generated six-digit code. The code is stored in Redis with a five-minute expiration time and a maximum of five attempts.
After successful verification, the system issues a JWT valid for 24 hours. Subsequent requests include the token through the Bearer authentication scheme.
The credit system uses a total balance field for each user. When a video-generation request is submitted, the backend performs the following operations inside a transaction:
- Check the user’s balance.
- Deduct the required credits.
- Create the generation task.
This ensures atomicity. If video generation fails, a Celery on_failure callback automatically restores the credits and records the transaction in a financial ledger.
4. Problems Encountered and Optimization Strategies
The experiment exposed several common issues in long-running AI-assisted development.
4.1 Context-Window Overflow
The project grew to more than 5,000 lines of code. When working on later modules, the model occasionally forgot utility functions defined earlier in the process.
Two mitigation strategies proved useful:
- Periodically ask the model to produce a summary of the current project structure.
- Move shared utilities into a dedicated module and repeatedly reference that module path in later prompts.
4.2 Slower API Responses
As more users accessed the model, response latency increased. The development workflow was adjusted by:
- Reducing the output length of individual requests.
- Splitting large modules into smaller files.
- Generating database models, routes, and service-layer code separately.
- Temporarily storing generated code fragments in the local file system.
This modular approach also made it easier to review and integrate the generated code.
4.3 Hallucinated Third-Party APIs
The model occasionally described incorrect usage patterns for the latest versions of third-party libraries. A practical response was to require the model to specify the assumed library version and to suggest references to the official documentation in code comments.
The generated code still required manual validation against the relevant API documentation.
5. Conclusion: AI-Assisted Development Enters a New Phase
This end-to-end SaaS experiment shows that AI agents are increasingly capable of handling genuine system-level development tasks. The model was able to decompose requirements, design an architecture, generate runnable code, and maintain logical consistency across a long development process.
For developers, this means an individual or small team may be able to turn an idea into a working product without waiting for a complete engineering organization. However, not every part of the process can be automated. Environment configuration, cloud deployment, and integration with real third-party APIs still require human involvement. The core business logic, however, can increasingly be implemented with substantial assistance from an AI agent.
As context windows grow and tool-calling capabilities improve, models may take on more complex operational and architectural responsibilities. For independent developers, small teams, and internal innovation groups, AI-agent-based full-stack development is likely to become a standard capability.
A practical starting point is specification-driven development. Clearly defining inputs, outputs, constraints, architecture, and acceptance criteria gives long-horizon models the structure they need to perform at their best.
