How a Long-Horizon Agent Built a Video SaaS in Three Hours

    This article walks through an experiment in which a long-horizon AI agent designed and built a short-video asset generation SaaS for cross-border e-commerce in about three hours. It explains why long-running tasks are the real test of AI coding tools, how spec-driven development keeps an agent on track across research, architecture, and implementation stages, and how the system was broken down into identity, storage, render orchestration, and billing domains. It includes the data model, the asynchronous job state machine, idempotency and retry handling, transactional credit accounting, data security practices, and observability metrics, along with the practical pitfalls encountered and a reusable acceptance checklist.

    1. Experiment Background: Why Long Tasks Are the Real Dividing Line

    Long-horizon agents and systems engineering capability have become central to how AI coding tools are evolving. Unlike earlier rounds that focused on generating a single page in one turn, this round of evaluation shifts attention to whether a model can stay aligned with a goal across a continuous task lasting several hours, keep context coherent, and break a vague requirement into an executable, system-level plan.

    The topic chosen for this experiment is a scenario with genuine commercial value: a short-video asset generation platform for cross-border e-commerce. Users upload product images and basic information, and the system automatically produces short promotional videos ready for content distribution. It may look like “generate one video,” but it actually spans multiple subsystems: accounts, object storage, asynchronous queues, third-party generation integration, credit-based billing, failure retries, and an operations dashboard. The difficulty in this kind of project is not syntax; it is architecture.

    The practical motivation for choosing this topic is also clear: many small and mid-sized merchants do not want to learn complex workflow orchestration. What they care about is the cost per asset, daily output capacity, delivery latency, and how little effort operation takes. So the product goal was narrowed to a single sentence — turn a “tool” into a “result,” so users click and get usable assets.

    2. Method First: Constraining the Agent with Spec-Driven Development

    To keep the agent from drifting during a long task, this experiment used Spec-Driven Development (SDD). The core idea is to split development into three stages that cannot be skipped, and to require a reviewable document at the end of each stage before moving on.

    1. Research stage: produce user roles, core scenarios, non-functional targets (latency, concurrency, availability), compliance boundaries, and a cost model.
    2. Architecture stage: produce a system context diagram, module breakdown, data model, interface contracts, task state machine, and failure handling strategy.
    3. Development stage: implement module by module in batches, each with unit tests, integration tests, and an operations runbook.

    A reusable prompt skeleton looks like this (illustrative):

    1. Role: Senior system architect + full-stack engineer
    2. Goal: Build a short-video asset generation SaaS
    3. Constraints:
    4. - Proceed strictly in the order research -> architecture -> development; produce the document for each stage and wait for my confirmation
    5. - The architecture document must include: data model, interface contracts, task state machine, permission model, observability plan
    6. - In the development stage, deliver only one module at a time, with tests and rollback notes
    7. Acceptance: Provide an executable acceptance checklist and a load testing plan

    The value of this method is that it turns “generating code” into “generating verifiable engineering artifacts,” which significantly reduces goal drift in long tasks.

    3. Architecture Breakdown: From Requirements to a Deployable System

    1. Domains and Boundaries

    The system is divided into four domains: identity and permissions, assets and storage, generation job orchestration, and billing and operations. Domains communicate through explicit contracts, which keeps the agent from mixing concerns within a single turn.

    2. Data Model (Illustrative)

    1. -- Users and assets
    2. create table users(id uuid primary key, phone text unique, credits int default 0);
    3. create table assets(id uuid primary key, user_id uuid references users(id),
    4. storage_key text, status text, created_at timestamptz default now());
    5. -- Render jobs and state machine
    6. create table render_jobs(
    7. id uuid primary key,
    8. asset_id uuid references assets(id),
    9. status text check (status in ('queued','running','succeeded','failed')),
    10. attempts int default 0,
    11. error_code text,
    12. updated_at timestamptz default now()
    13. );

    3. Task State Machine and Asynchronous Queue

    Video generation is a minute-level, long-running operation and must be made asynchronous. A request returns a job ID immediately, and queue consumers pull and execute the work; the frontend gets progress through polling or a long-lived connection. The state machine only allows legal transitions, and repeated consumption must be idempotent.

    1. queued -> running -> succeeded
    2. -> failed -> (retry<=N) queued

    Key engineering points: idempotency keys, maximum retry count, backoff strategy, dead letter queue, and timeout circuit breaking. These are the dividing line between “production-ready” and “demo-only.”

    4. Authentication and Billing

    Login uses a phone number with a verification code to exchange for a short-lived token, paired with a refresh mechanism. Credit deduction and job creation sit inside the same transaction boundary and roll back on failure, avoiding the case where credits are deducted but nothing is produced. High-value operations get an audit log.

    5. Data Security

    With a relational database, row-level security policies can be enabled so users can only access their own assets; object storage uses temporary authorized URLs and public buckets are prohibited; keys are managed centrally by a key management service and never enter the code repository.

    6. Observability

    Unified log fields (job ID, user ID, duration, error code) are combined with metrics and alerts: queue backlog, failure rate, P95 latency, and abnormal credit fluctuations. A long-task system without observability cannot be operated.

    4. Real Pitfalls in the Experiment and How to Avoid Them

    • Context drift: in long tasks the agent may reinvent the wheel. The countermeasure is a mandatory “document gate” — confirm each stage’s output before continuing.
    • Over-engineering: agents tend to introduce too many abstractions. The prompt must state that a minimum viable architecture comes first.
    • Missing tests: generated code can look complete while lacking failure paths. Every module must include exception cases.
    • Compute queuing: slower execution at peak times is normal, so long tasks should be split into batches with checkpoints to allow interruption and resumption.

    5. Conclusion and a Reusable Checklist

    This experiment shows that when an agent has long-horizon context and systems engineering awareness, one person can build a runnable, extensible SaaS skeleton in a few hours. The real leverage is not “generate faster” but “clearer specs.”

    Reusable acceptance checklist:

    • Does the architecture document include the state machine and failure strategy?
    • Is the system asynchronous with idempotency and retries?
    • Are billing and output transactionally consistent?
    • Is there least privilege and auditing?
    • Are there metrics, logs, alerts, and a load testing plan?

    Upgrading “writing code” to “delivering a system” is the sign that AI programming is entering its next stage.