Long-Horizon Tasks in Practice: Building a Commercial SaaS Platform with AI Agents

    This article documents a hands-on experiment in which an AI coding agent was used to build a commercial e-commerce short-video generation SaaS platform from scratch. It explains why a production-grade SaaS is the ultimate test of long-horizon AI tasks, covering backend orchestration, multi-tenant data design with row-level security, asynchronous task processing driven by a state machine, and idempotent billing. It then walks through the spec-driven development workflow (research → architecture → development), effective prompting practices, and the pitfalls encountered along the way, concluding that the developer's role is shifting from writing code to defining specs, guarding architecture, and validating results.

    1. AI Coding Is Moving from “Writing Code” to “Building Systems”

    Over the past two years, the general perception of AI coding has mostly stayed at the level of “autocomplete a function” or “generate a page.” But a new generation of agentic coding tools is pushing the capability boundary from “code snippets” toward “system-level engineering.” What sets them apart is not whether they can produce a polished UI, but three things: agent-style task planning, context coherence across long-horizon tasks, and requirement decomposition from a systems-engineering perspective.

    These three points map onto a harsh reality: a product that can go live is far more complex than a demo page that merely runs. The former tests whether the model can, over hours of continuous work, remember the initial goal, maintain architectural consistency, and make autonomous decisions when intermediate problems arise.

    So I framed this as a real experiment: using nothing but an AI coding agent, build an e-commerce short-video generation SaaS platform from zero. Users upload product images, the system invokes video generation capabilities, and it automatically produces short promotional videos ready for content distribution.

    2. Why a Commercial-Grade SaaS Is the Ultimate Test for Long-Horizon Tasks

    I chose this topic because it has inherently “exponential complexity.” Building a demo takes one page and one endpoint; building a commercial SaaS means facing an entire network of interlocking complexity.

    2.1 Backend Services: Orchestration, Not Just Endpoints

    The backend is not a pile of REST endpoints. It needs a unified layered structure (routing layer, service layer, data-access layer), a clear error-code convention, and transaction-boundary control for generation workloads. More importantly, video generation is a long-running operation, so the backend must be designed “async-first” from the start, rather than written as synchronous blocking code and reworked later.

    2.2 Data Layer: Multi-Tenancy and Row-Level Security

    Multi-tenant isolation is core to SaaS. The schema design must cover entities such as users, product assets, generation tasks, and billing records, and it must enforce row-level security policies on every table that touches user data, ensuring that user A can never read user B’s records. If the tenant dimension isn’t planned properly from the beginning, retrofitting it later is extremely costly. This is also a litmus test for whether the agent has a “systems-engineering perspective.”

    2.3 Asynchronous Tasks: Keeping Slow Generation from Blocking the Main Flow

    Generating a single promotional video can take several minutes. If you wait synchronously, requests time out and the user experience collapses. The right approach is to introduce a message queue for task dispatch, decoupling “submit a task” from “execute generation,” and then use a state machine to manage transitions through queued, running, succeeded, and failed-with-retry states.

    1. # Simplified task state machine
    2. TASK_STATES = {
    3. "PENDING": ["QUEUED", "FAILED"],
    4. "QUEUED": ["RUNNING", "FAILED"],
    5. "RUNNING": ["SUCCEEDED", "FAILED"],
    6. "SUCCEEDED": [], # terminal state
    7. "FAILED": ["QUEUED"], # retries allowed
    8. }
    9. def transition(task, target):
    10. assert target in TASK_STATES[task.state], "Illegal state transition"
    11. task.state = target
    12. return task

    The frontend or calling party retrieves results by polling task status or listening for callback notifications. Generated images and videos should be stored in object storage, with the database holding only reference URLs, avoiding large files polluting structured data.

    2.4 Authentication and Billing: Accuracy and Idempotency Are the Baseline

    Phone-number verification-code login and token issuance/validation are standard. The real difficulty lies in billing: credit deduction must stay in a consistent state with task creation. It must neither charge users for a generation that failed, nor allow duplicate requests to be exploited for free usage. This requires an idempotency-key mechanism.

    1. # Idempotency-key-based credit deduction
    2. def deduct_credits(user_id, idempotency_key, cost):
    3. # If this idempotency key was already processed, return the previous
    4. # result to avoid double-charging
    5. if already_processed(idempotency_key):
    6. return get_previous_result(idempotency_key)
    7. # Deduct credits and create the task within the same transaction/reliable flow
    8. with atomic_transaction():
    9. deduct(user_id, cost)
    10. create_task(user_id, idempotency_key)
    11. return build_success_result()

    At the cloud-infrastructure level, most of these capabilities can be reused from general-purpose categories: object storage for assets, a message queue for task dispatch, and a container platform for service deployment, combined with logging and monitoring/alerting for observability. By leveraging these general cloud capabilities (for example, product categories such as object storage, message queues, and container platforms offered by Baidu AI Cloud), developers can focus their energy on business logic rather than infrastructure setup.

    3. Three Capabilities That Long-Horizon Tasks Really Test

    After running the entire pipeline, I’m more convinced that the hard part of long-horizon tasks is not “writing” but “holding the line”:

    • Goal anchoring: still remembering the original system-level constraints after spanning many stages, without drifting from the overall architecture because of a local implementation.
    • Context coherence: after changing the data model, updating the dependent endpoints, validation, and frontend calls accordingly, rather than leaving broken gaps.
    • Autonomous decision-making: at forks like “async or sync” or “do we introduce a queue,” giving reasonable defaults based on engineering trade-offs rather than repeatedly asking for instructions.

    4. Spec-Driven Development: A Practical Path from Research → Architecture → Development

    I didn’t let it start coding right away. Instead, I adopted spec-driven development (define the spec first, then the architecture, and only then write code), enforcing a three-stage progression:

    1. Research stage: first produce a breakdown of product requirements, target-user personas, a core feature list, and key metrics, making clear “whose problem this SaaS actually solves.”
    2. Architecture stage: produce the technology-selection rationale, system-module breakdown, data-model design (including multi-tenancy and row-level security), and the async-task and billing schemes, followed by a feasibility assessment.
    3. Development stage: implement in module batches, running a self-check and regression after each completed module to ensure front-to-back consistency.

    The value of this ordering is that it moves “high-level, irreversible decisions” to the cheapest point in time. Once the architecture has been validated in a document, subsequent coding is just translating the spec into an implementation, greatly reducing the risk of drifting off course midway through a long-horizon task.

    5. Writing Prompts That Actually Carry Weight

    A good prompt isn’t a one-line wish; it fits the agent with a steering wheel and guardrails. A few key points:

    • Define the role and deliverables: ask it to act as a senior architect, delivering a document (spec, architecture) at each stage before delivering code.
    • Lock down the constraint list: multi-tenant isolation, async-first, idempotent billing, and row-level security, emphasized repeatedly as non-negotiable hard constraints.
    • Enforce the process and self-checks: require strict adherence to the “research → architecture → development” order, with a consistency check at the end of each step.
    • Advance in chunks: break the system into modules implemented one at a time, avoiding context overload from generating everything at once.

    6. Pitfalls and Lessons Learned

    • Noticeable slowdown during compute peaks: concentrated calls during peak hours reduce response speed, and long-horizon tasks are especially sensitive. Stagger usage and leave buffer room.
    • “Local forgetting” over long horizons: the further along you go, the more likely front-to-back inconsistencies appear. Fall back on stage self-checks and regression tests.
    • Don’t over-trust a one-shot result: a commercial system must have testing and validation, turning “looks correct” into “verified correct.”
    • Spec documents must be actionable: if the architecture document is vague, the coding stage is doomed to repeated rework. The more precise the spec, the higher the degree of automation.

    7. A Product Insight: From “Teaching to Fish” to “Giving a Fish”

    This experiment corrected one of my assumptions. I used to be keen on teaching people to build all sorts of automation workflows and to “craft” videos step by step. But real commercial users, especially cross-border e-commerce sellers, often don’t want to learn tools at all. They only care about ROI: what does a single video cost? Can I reliably run a thousand a day? Is there a platform that gives results with one click?

    The conclusion is direct: in the AI era, packaging capabilities into a product that users “don’t need to learn and can use right away” is more valuable than teaching users to operate a tool. Lowering the barrier to use is itself a core competitive advantage. And the maturity of AI coding agents is exactly what turns “one person building a usable product in a few days” from a concept into an executable path.

    8. Conclusion

    When a coding agent possesses long-horizon task and systems-engineering capabilities, the developer’s role is shifting from “the person who writes code” to “the person who defines specs, guards the architecture, and validates results.” It won’t take on business judgment for you, but it can free you from repetitive implementation, giving one person output close to that of a small team. The real barrier has also moved up, from “can it be written” to “can you think it through clearly and can you verify it holds up.” This is, I believe, the real meaning of AI coding entering its next stage.