Long-Horizon Agent Coding in Practice: Building a Video Generation SaaS in 3 Hours

    This article reports on a hands-on experiment that used a long-horizon coding agent to build a short-video generation SaaS from scratch in roughly three hours. Its core argument is that the real bottleneck in AI-assisted development has shifted from writing code to defining verifiable specifications. It walks through a specification-driven workflow (research, architecture, development), then covers the five architecture decisions that must be right on the first pass: service layering, data modeling with multi-tenant isolation, long-running jobs backed by a queue and state machine, idempotent billing, and the delivery API shape. It closes with the pitfalls encountered along the way and a reusable checklist for shipping this kind of system, plus how to shift success metrics from code volume to task success rate, unit cost, and P95 latency.

    1. The Bottom Line First: The Bottleneck Has Moved from “Writing Code” to “Defining the Problem”

    One complete experiment convinced me that AI coding is moving from “completing code” to “owning long-horizon tasks.” But what really decides success is no longer how elegant the functions a model can write are, it’s whether a person can break system-level requirements into specifications that can be accepted and verified.

    The project I chose was to build a short-video e-commerce material generation platform from zero: users upload a product image and copy, and within minutes the system produces ready-to-run vertical (portrait) videos. The problem looks simple, but it actually spans seven categories of problems: backend services, a relational data model, multi-user data isolation, an asynchronous task queue, a state machine, authentication with credit-based billing, and asset storage and distribution. Their complexity doesn’t add up, it multiplies.

    My past judgment was that something like this would take at least a five-person team two weeks. This time I handed the entire project to a coding agent running in a mode with long context and tool-calling capabilities, using a specification-driven workflow of “research, then architecture, then development.” A complete, runnable loop takes roughly three hours on the happy path. I hit several typical traps along the way, which I’ll cover separately below.

    There’s also a product-level reason for picking this problem. The upside in overseas short-video e-commerce is plainly visible, but the target customers (small and medium sellers) don’t care about workflow orchestration. They only care about three things: how much a single video costs, how many they can produce in a day, and whether there’s a one-click entry that spits out a video. In the AI era, delivering results beats delivering methods. That dictates the technical focus: not “lots of features,” but unit cost, throughput, and stability.

    2. What’s Actually New About Long-Horizon Task Agents

    Traditional code completion solves “the next token.” A long-horizon task agent solves “the next verifiable milestone.” The differences come down to three points:

    1. Task decomposition: breaking “build a SaaS” into independently acceptable sub-goals such as data model, interface contracts, task scheduling, and billing, rather than writing downward from a routing file.
    2. Context consistency: staying consistent on interface naming, field types, and error-code semantics after hours of work and dozens of tool calls. This is where long-horizon tasks most easily degrade.
    3. A self-verification loop: being able to run commands, read errors, modify code, and run again, forming a “generate, verify, fix” cycle. Without this link, a long task just amplifies its errors.

    The boundary needs to be clear: it can’t make business tradeoffs for you, and it won’t automatically guarantee a sound architecture. The speed of generating code far exceeds the speed at which you can review it, so the engineering focus must shift forward to specifications and acceptance.

    3. Specification-Driven Development: Write the Prompt as a Process, Not a Wish

    The core approach I used is Specification-Driven Development: produce the spec first, then the architecture, and only then the implementation. The skeleton of the prompt looks like this:

    1. # Role
    2. A senior backend architect who strictly follows the specification-driven workflow.
    3. # Inputs
    4. spec.md: functional boundaries, user stories, acceptance criteria, non-functional targets
    5. # Workflow (steps must not be skipped; wait for confirmation at the end of each phase)
    6. 1. Research: output a requirements-clarification list + risk items (compliance, cost, concurrency)
    7. 2. Architecture: output the layered structure, data model, interface contracts, task state machine
    8. 3. Development: implement module by module in batches, each batch with unit tests and database migration scripts
    9. # Hard constraints
    10. - Store amounts and credits as integers; no floating point
    11. - All create-type APIs must be idempotent (idempotency key)
    12. - No hardcoded secrets; configs go through environment variables or a secret management service
    13. - Update interface contracts and the data dictionary in sync with every change

    The key point is that each phase is forced to produce a reviewable document artifact and required to “wait for confirmation” before continuing. This step turns an uncontrollable long run into several rollback-able checkpoints.

    4. System Architecture: Five Decisions You Must Get Right the First Time

    4.1 Service layering

    Expose only a stateless API service externally (implementable with a Python asynchronous web framework), and split it internally into an access layer (authentication, rate limiting, parameter validation), a domain layer (users, tasks, billing), and an infrastructure layer (object storage, message queue, database, and media processing components). Video generation takes minutes, so it must never occupy an HTTP request thread.

    4.2 Data model and multi-tenant isolation

    Store all amounts and credits as integers (for example, using the “li” as the smallest unit) to avoid floating-point accumulation error; the job table uses an idempotency key to prevent duplicate submissions from causing duplicate billing.

    1. CREATE TABLE render_jobs (
    2. id UUID PRIMARY KEY,
    3. user_id BIGINT NOT NULL,
    4. idempotency_key VARCHAR(64) NOT NULL,
    5. status SMALLINT NOT NULL, -- 0 queued, 1 running, 2 success, 3 failed, 4 refunded
    6. cost_credits BIGINT NOT NULL DEFAULT 0,
    7. asset_ref TEXT,
    8. error_code VARCHAR(64),
    9. created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    10. UNIQUE (user_id, idempotency_key)
    11. );
    12. ALTER TABLE render_jobs ENABLE ROW LEVEL SECURITY;
    13. ALTER TABLE render_jobs FORCE ROW LEVEL SECURITY;
    14. CREATE POLICY owner_only ON render_jobs
    15. USING (user_id = current_setting('app.current_user_id')::bigint)
    16. WITH CHECK (user_id = current_setting('app.current_user_id')::bigint);

    After enabling Row-Level Security (RLS), you need to reserve a separate exception channel for operations and background tasks. Otherwise scheduled jobs get blocked by the policy, which is the most common “everything’s configured, but now nothing can be queried” failure.

    4.3 Long-running tasks: queue + state machine + preemptive claiming

    The producer only writes the job record and returns a job ID; the consumer uses a database-level row lock to claim jobs in batches, which naturally supports horizontal scaling:

    1. async def claim_batch(limit: int = 20):
    2. return await tx.execute("""
    3. WITH picked AS (
    4. SELECT id FROM render_jobs
    5. WHERE status = 0
    6. ORDER BY created_at
    7. FOR UPDATE SKIP LOCKED
    8. LIMIT :limit
    9. )
    10. UPDATE render_jobs j SET status = 1, updated_at = now()
    11. FROM picked WHERE j.id = picked.id
    12. RETURNING j.id, j.user_id
    13. """, {"limit": limit})

    Companion requirements: failures must record an error_code rather than only writing a log; jobs that exceed a threshold go to a dead-letter queue and trigger an alert; and after a worker crashes, a “running-timeout” scan re-queues the affected jobs.

    4.4 Billing and debit: idempotency + optimistic locking

    1. async def debit(user_id: int, amount: int, biz_key: str):
    2. async with session.begin():
    3. await tx.insert_intent(user_id, amount, biz_key) # unique constraint blocks duplicates
    4. res = await tx.execute(
    5. "UPDATE credits_account SET balance = balance - :a, version = version + 1 "
    6. "WHERE user_id = :uid AND balance >= :a",
    7. {"a": amount, "uid": user_id})
    8. if res.rowcount != 1:
    9. raise InsufficientCredits()

    You must also implement failure refunds: when generation fails, roll the credits back under the original idempotency key, and make the refund action idempotent too. Otherwise retries will corrupt the balance.

    4.5 Delivery interface shape

    For minute-level tasks, the recommended shape is “submit and get back a job ID + poll for status + optional callback notification.” Polling needs exponential backoff so the client doesn’t overwhelm the service.

    5. The Traps I Hit

    • Interface drift: mid-task, the model changed a field’s name but only updated half of the call sites. The fix is to treat the interface contract and data dictionary as the single source of truth and require them to be synced on every change.
    • Non-reversible migrations: the generated migrations only had forward changes. I required paired up/down scripts and ran them on an empty database before merging.
    • Tests that “looked like they passed”: the generated cases usually covered only the happy path. I enforced an acceptance gate that requires dedicated cases for each exception path (insufficient balance, missing assets, upstream timeout).
    • Missing secure defaults: a verification-code endpoint must ship with rate limiting and human verification enabled by default, otherwise SMS gets abused; and secrets must never enter the database or the repo, going through environment variables or a secret management service instead.
    • The cost blind spot: the real expense is “compute wasted on failed retries.” After getting the features working, the first thing I did was measure the task success rate, not optimize generation speed.

    6. A Reusable Rollout Checklist

    1. Write a one-page spec covering the functional boundaries and acceptance criteria before letting the agent start.
    2. Force a document artifact at each phase and set confirmation points, forming rollback-able checkpoints.
    3. Front-load the architecture risks: idempotency, concurrent deduction, the task state machine, and data isolation, none of which can be patched in later.
    4. Build an acceptance gate: static checks + unit tests + migration script dry runs + exception-path cases.
    5. Commit versions continuously throughout long tasks, rather than producing one big-bang output.
    6. Switch the evaluation metric from “code volume” to “task success rate, per-unit cost, and P95 latency.”

    A system like this is ultimately deployed on public cloud infrastructure, typically combining common capabilities such as object storage, message queues, a container platform, logging, and monitoring/alerting (for example, the equivalent infrastructure categories offered by Baidu AI Cloud), without introducing any special dependencies for the architecture.

    The next stop for AI coding isn’t “writing code for you,” but pushing the developer’s role from implementer toward architect and reviewer. What can be automated is the act of coding; what can’t be replaced is defining the problem, making tradeoff judgments, and setting acceptance criteria, and those three things are exactly what truly decides whether a long-horizon task can run to completion.