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

    An end-to-end test of building a commercially usable short-video content SaaS platform from scratch in roughly three hours with an AI coding agent, without line-by-line human guidance. Rather than narrating the process, this article focuses on the engineering decisions that actually carried the weight: a spec-driven development (SDD) workflow that blocks implementation until the spec and architecture are approved, six core design choices (async backend layering and interface contracts, idempotency keys, multi-tenant row-level security, a queue-backed job state machine, pre-charge and settle credit billing, media storage and observability), and five recurring failure modes such as context drift and silent job stalls. It closes with a four-point human acceptance gate, a minimal regression suite, and four verifiable signals for judging whether a coding agent can deliver a complete system rather than a convincing demo.

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

    1. The overlooked shift: in AI coding, the hard part moved from syntax to engineering

    For the past two years, most people’s mental model of AI coding has been stuck at “autocomplete this function” or “explain this error message.” But the moment a coding agent runs autonomously for hours and maintains the same design constraints across dozens of files, the thing being tested changes. The question is no longer whether a model can produce one correct line of code, but whether it can hold consistency and engineering judgment across a long-running, multi-module, multi-constraint task.

    That is exactly why long-horizon tasks and agentic capability keep coming up in discussion. The capability breaks down into three layers that build on each other:

    • Intent decomposition: turning one vague product requirement into an executable requirements list and task tree.
    • Context continuity: still remembering the original non-functional constraints (for example, “billing must be idempotent,” “tenant data must be isolated”) after thousands of lines of code and many rounds of edits.
    • Systems engineering perspective: proactively filling in the parts nobody stated but that will definitely break — migration scripts, failure retries, observability, cost control.

    I ran one deliberately extreme validation of this: have a coding agent build, from zero, a commercially usable SaaS platform for batch generation of short-video commerce assets, with no human guidance at the line level. The whole thing ran in the order research → architecture → development, end to end, and finished in the range of about three hours. This article does not retell that process. It distills the decisions and the pitfalls that actually had technical substance.

    2. Why this problem: package the tool as an outcome, or efficiency means nothing

    Why this topic? I noticed a counter-intuitive pattern. Most practitioners in cross-border e-commerce and content commerce do not want to learn a visual workflow tool. What they care about is ROI — whether a single asset costs a few cents or a few dollars, whether they can reliably produce a thousand-plus assets in a day, and whether there is an interface where you upload an image, click once, and get a video.

    That constrains the technical shape of the product in three hard ways:

    1. Cost and throughput must be computable: per-run cost stays controllable and predictable, and daily capacity can be scaled through concurrency and queueing policy.
    2. The process must be asynchronous: video generation is a minute-scale task, and any synchronous wait-for-response design falls apart once concurrency arrives.
    3. Results must be reusable: generated assets, scripts, and shot lists need to be captured in structured form, not thrown away after a single use.

    Together these three make a genuine systems engineering exam. It does not test whether the model can write an endpoint. It tests whether it understands how queueing, idempotency, consistency, multi-tenancy, and cost governance relate to each other.

    3. Methodology: spec-driven development in three phases

    I used an SDD (spec-driven development) workflow: produce the spec first, then the architecture, and only then write code. The core constraint was this: implementation is not allowed to start until both the spec and the architecture have been confirmed. That discipline matters more than any prompt trick, because it pushes the entropy of “model improvises freely” as low as it will go.

    Phase 1: Research and requirements spec

    The agent was required to do a market and competitor feature breakdown first, then output three kinds of documents:

    • Roles and scenarios: what the platform operator, the media buyer, and the asset reviewer each care about.
    • Functional spec: user uploads a product image → script and shot list are generated → video is composed → preview and download, with the input/output contract for every step.
    • Non-functional spec: this one has to be explicit — P95 response targets, maximum task concurrency, per-account daily quota, failure retry policy, audit log retention requirements.

    The key technique: non-functional requirements must be frozen as concrete line items in this phase. Experience shows that if you defer them to late development, the agent will usually paper over them with the simplest possible synchronous implementation.

    Phase 2: Architecture design

    The deliverable had to be a set of decidable technical choices, not a list of nouns. A passable architecture document includes at least:

    • Service decomposition and module boundaries (gateway layer / business orchestration layer / task execution layer / media processing layer).
    • Data model ER diagram and migration strategy.
    • Task state machine definition and exception branches.
    • Authentication and billing flow diagrams.
    • Integration points and failure-degradation plans for third-party capabilities (model APIs, SMS gateway, payment gateway, object storage).

    Phase 3: Implementation and self-verification

    Once coding started, the required order was data layer → auth layer → business orchestration → async executor → frontend interaction, with the existing test suite run and the results reported after each layer was finished. Having the agent run its own tests, read its own errors, and fix them itself is the single highest-value instruction in a long-horizon task.

    4. Six key design decisions in the architecture

    4.1 Backend layering and interface contracts

    The backend used an asynchronous Python web framework. The reason is not just performance: in an IO-intensive orchestration scenario like video generation, an async model lets a single instance hold far more in-flight requests. The layering constraints were:

    • The routing layer does parameter validation and authentication only, no business logic.
    • The service layer owns domain rules (quota, billing, state transitions), and transaction boundaries must be contained here.
    • The repository layer encapsulates data access, and every query is required to carry a tenant condition.
    • The executor layer calls the video generation model and transcoding services, decoupled from the business layer so providers can be swapped.

    One constraint has to be in place up front: every creation endpoint must accept an idempotency key. The frontend generates a request-unique identifier when the user clicks “generate,” and the server builds a unique index on it. Otherwise a user tapping repeatedly on a weak network connection causes duplicate charges and duplicate renders — the kind of problem that is more damaging in production than any ordinary bug.

    4.2 Multi-tenant data model and row-level security

    The first step toward a commercial product is multi-tenancy. The options are separate databases, separate schemas, or shared tables with a tenant column. Weighing cost against maintainability, shared tables plus a tenant column is the most practical, but it depends on discipline in the application layer. So I recommend enabling row-level security (RLS) at the database layer, which turns isolation from a code convention into a hard database constraint.

    1. CREATE TABLE video_jobs (
    2. id UUID PRIMARY KEY,
    3. tenant_id BIGINT NOT NULL,
    4. user_id BIGINT NOT NULL,
    5. source_uri TEXT NOT NULL, -- object storage key for the source asset
    6. script JSONB, -- generated script and shot list
    7. status TEXT NOT NULL DEFAULT 'QUEUED',
    8. cost_credits INTEGER NOT NULL DEFAULT 0,
    9. idem_key TEXT NOT NULL,
    10. provider_task TEXT,
    11. error_code TEXT,
    12. created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    13. updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    14. UNIQUE (tenant_id, idem_key) -- idempotency key persisted in the database
    15. );
    16. CREATE INDEX idx_jobs_user_time ON video_jobs (user_id, created_at DESC);
    17. ALTER TABLE video_jobs ENABLE ROW LEVEL SECURITY;
    18. CREATE POLICY tenant_isolation ON video_jobs
    19. USING (tenant_id = current_setting('app.tenant_id')::BIGINT);

    Related requirements: migration scripts must be repeatable and versioned, and every destructive change (dropping a column, changing a type) must ship with a paired rollback script. This is one of the hard rules I forced the agent to follow. Without it, one mid-project schema adjustment invalidates all the test data accumulated up to that point.

    4.3 Async tasks and the state machine

    Video generation is a minute-scale task, so it has to run through a message queue plus executor pattern. The state machine was defined as:

    1. CREATED → QUEUED → RUNNING → SUCCEEDED → DELIVERED
    2. └→ FAILED (retryable / terminal)

    Four things had to be implemented:

    • Retry and backoff: exponential backoff retries apply only to retryable errors (provider timeouts, rate limiting, transient 5xx). Parameter-type errors go straight to a terminal state so they do not burn capacity for nothing.
    • Dead letter handling: tasks that exceed the maximum retry count move to a dead letter queue and raise an alert. No silent dropping.
    • Timeout fallback: tasks need a lease mechanism. If an executor misses its heartbeat, the task can be rescheduled, so an executor crash cannot leave a task stuck forever.
    • Concurrency control: rate limits are applied per tenant and per provider key separately, so one account exhausting its quota cannot degrade the whole system.

    4.4 Authentication, credits, and billing consistency

    The auth flow: phone number plus SMS verification code login → issue a stateless signed token (a common standard such as JWT is fine) → the gateway validates uniformly and injects the tenant ID into the request context.

    Credit deduction is the other high-risk area. The correct approach is pre-charge, settle, compensate — not run the task first and deduct afterward.

    1. -- Pre-charge: row lock + conditional update, so concurrent requests cannot drive the balance negative
    2. BEGIN;
    3. SELECT balance FROM credit_accounts WHERE user_id = :uid FOR UPDATE;
    4. UPDATE credit_accounts
    5. SET balance = balance - :cost
    6. WHERE user_id = :uid AND balance >= :cost; -- 0 rows affected means insufficient balance
    7. COMMIT;

    Related requirements:

    • Each deduction writes a ledger entry that records the associated idempotency key, so a failed task can be refunded along the same key.
    • The top-up side relies on the payment gateway’s asynchronous callback, which must verify the signature, verify the amount, and handle duplicate callbacks.
    • Add a reconciliation job that periodically compares total ledger amount against balance and alerts on any mismatch.

    4.5 Media storage and delivery

    Uploaded assets and generated results go to object storage. The database stores keys only — never base64 blobs and never local paths. Downloads use time-limited pre-signed URLs. Finished videos and cover images are delivered through a content delivery network. Large uploads use multipart upload with direct-to-storage transfer from the frontend, which keeps the server’s bandwidth pressure down. Heavy CPU work such as video transcoding is its own async stage, with status tracked separately from the main task.

    4.6 Observability and cost governance

    Once a long-horizon system is live, observability is its lifeline:

    • Structured logging: every log line carries task ID, tenant ID, duration, and provider response code. Logging secrets or full request bodies is prohibited.
    • Metrics and alerting: task success rate, queue backlog depth, average cost per asset, P95 latency — any of them out of bounds triggers an alert.
    • Cost dashboard: consumption aggregated by tenant, model, and time range, so operations can judge ROI directly instead of guessing.

    5. Real pitfalls: five lessons worth more than the feature list

    1. Context drift: late in the task, the agent “forgets” earlier non-functional constraints and regresses — calling video generation synchronously, or deducting credits without an idempotency key. The fix is to write the critical constraints into a persistent “project constitution” file and require the agent to re-read it at the start of every session.
    2. Cross-file inconsistency: after a data model change, the frontend type definitions, API docs, and test cases were left out of sync. The fix is to require all three to be updated after each model change, followed by a full test run.
    3. Secret leakage risk: during configuration setup, real credentials can end up hardcoded in example files. This one needs human review. Credentials may only be read from environment variables or a secrets management service, and the repository should contain placeholders only.
    4. Silent failure in long-running jobs: without leases and dead letter handling in the early version, tasks stayed permanently in RUNNING after an executor restart. A textbook case of “it looks like it works, and it will definitely break in production.”
    5. Compute and quota contention: at peak, tasks arrive in a burst, the provider rate-limits, and failures spread. The fix is a priority-tiered queue, deliberate slowdown for non-critical tasks, and rotation across multiple provider keys to spread quota usage.

    6. Working with the agent correctly: turn acceptance into hard gates

    Finishing the project in three hours does not mean the human steps away. Real efficiency comes from concentrating human attention on judgment rather than on typing. My approach was to enforce manual gates at four points:

    • Spec sign-off: once the feature list and non-functional targets are approved, scope expansion is not allowed to creep in.
    • Architecture review: focus on the data model, the state machine, and transaction boundaries — mistakes in these three are the ones you cannot fix later.
    • Code review: read the diff, not the explanation. Prioritize three high-risk categories: authentication, billing, and SQL string concatenation.
    • Regression verification: run one real end-to-end pass with real assets, deliberately triggering failure branches to confirm refunds and retries work.

    I also recommend a minimal regression suite. Five main-path cases — login, create task, deduct credits, failure refund, list pagination — cover most of the regression risk. Without a test baseline, the output of a long-horizon task cannot be trusted.

    7. Conclusion: evaluate long-horizon capability by the deliverable, not the demo

    The takeaway from this experiment: the competitive focus of AI coding has shifted from “how well does it write a single function” to “can it deliver a complete system — with data, concurrency, billing, and observability — across a long unattended stretch of time.” Four verifiable signals tell you whether a coding agent has that ability:

    1. Does it proactively ask about and fill in non-functional requirements?
    2. Can it keep contracts consistent across modules instead of letting each one go its own way?
    3. Does it run tests on its own and iterate based on the errors?
    4. Does it account for idempotency, retries, isolation, and audit by default?

    For development teams, the value of this class of tool is not replacing people who write code. It is compressing the time from zero to a deployable skeleton down to hours, so that people can spend their attention on product judgment, architecture trade-offs, and business validation. And when it comes to running these systems in the cloud, the real dividing line between a demo and a stable commercial product is how well you compose the commodity capabilities — object storage, message queues, container platforms, logging, and monitoring and alerting — together with least-privilege access and centralized secret management.