Prompt Engineering for Code-Generated Images and Videos: A Four-Part Contract for Reproducible Output

    This article explains why code-generated visuals usually fail not because of weak models but because prompts describe only the desired result, without constraining implementation, quality level, or acceptance criteria. It introduces a four-part prompt structure covering the runtime target, scene description with detail density, technical determinism requirements such as pure time functions, fixed random seeds, period alignment, and geometry-based lighting, and delivery standards with a negative list. Two examples, an aurora fjord animation and an afternoon interior still, show the shared skeleton, followed by practical advice for containerized rendering and operations.

    1. The Problem Is Not Model Capability, It Is a Missing Engineering Contract

    When you generate images and videos with pure code, failure usually has nothing to do with the model being unable to write code. It happens because the prompt only describes what is wanted, and says nothing about how to build it, to what standard, or how to verify it. A single natural-language sentence such as “make a nice-looking afternoon light and shadow image” can be translated into countless implementations on the execution side: one approach stacks a filter over a glow, another hand-draws shadow polygons, another brings in a particle system, and another makes the camera shake at random. The result is that every rerun looks different. The frame may look finished, but it cannot be reproduced, and nobody can tell what is actually “wrong.”

    For this reason, a prompt should be written as a short engineering contract. It has to constrain four categories of information at the same time: the runtime target, the visual content, the technical implementation, and the delivery method. If any one of the four is missing, implementation freedom spirals out of control.

    2. The Four-Part Prompt Structure

    2.1 Part One: Specify the Tool and Runtime Environment

    The opening must state clearly what is used to render, and at what size and frame rate. For example, “use a canvas-style 2D rendering API to output a 1920×1080 still image,” or “output a 1920×1080 looping animation at 30 frames per second with a duration of 30 seconds.” The purpose of this part is to narrow the implementation path so the model does not pick a completely unrelated technology stack. If video is involved, also state how frames are captured and assembled, for example “capture frames one by one with an automated browser, then package them into a video file with a command-line assembly tool,” without binding to any specific product name.

    2.2 Part Two: Scene Description and Detail Density

    The middle section describes the scene, composition, color relationships, and a “detail density standard.” Take an aurora fjord night scene as an example. It should specify: a fjord late at night; snow-covered mountains on both banks rendered as silhouettes; a calm water surface that fully mirrors the sky; aurora rising from the horizon as vertical light bands with internal longitudinal fiber structures, swaying slowly, mainly green with edges fading to pale purple; stars that stay fixed and do not twinkle; and a small cabin in a corner on the shore, its window glowing warm yellow as the only warm color in the entire frame. For style, borrow only the texture and warm-cool contrast of long-exposure nature photography. No text, no watermarks, no recognizable landmarks, and no reproduction of any specific photograph.

    The key is the “detail density” sentence: it determines whether the implementer treats the fiber structure, the reflection coupling, and the layered snow as mandatory items rather than optional ones.

    2.3 Part Three: Technical Constraints and Determinism Requirements

    This part is the core of whether pure-code generation is reproducible at all. It should contain at least four items:

    • The animation is a pure function of time t, exposing an interface that fetches a frame by time (such as seek(t)), and keeps no cross-frame state.
    • All random numbers use a fixed seed.
    • All periodic terms divide the total duration evenly, so that the frame at second 30 is identical to the frame at second 0.
    • Light and shadow must be computed from geometric relationships. Hand-drawn shadow shapes are not allowed, and glow overlaid with filters is not allowed.

    Illustrative code:

    1. // Pseudocode: pure time function + fixed seed + period alignment
    2. const TOTAL = 30; // seconds
    3. const PERIOD = 6; // 6 divides 30, guaranteeing first/last frame match
    4. function renderAt(t) {
    5. const phase = (t % PERIOD) / PERIOD; // depends only on t
    6. const rnd = mulberry32(20240501); // fixed seed, identical every run
    7. drawSky(rnd, phase);
    8. drawAuroraFibers(phase); // longitudinal fiber structure
    9. drawReflection(phase); // reflection synced with the light bands
    10. drawStarsFixed(rnd); // stars do not twinkle
    11. drawCabinWarmLight(); // the only warm color
    12. }

    2.4 Part Four: Delivery Standards and the Negative List

    The ending must state the acceptance rhythm and the prohibited items. The suggested delivery rhythm is: first render a 5-second sample or a single still frame for confirmation, then produce the full piece after approval; also attach a one-page note stating which effects are simulated and which are not implemented. The negative list should be concrete, for example: no particle bursts, no bloom or glow, no gradient transitions, no bouncy easing, no random camera shake, no template-style oversized headline text, no all elements entering or exiting simultaneously, and no cartoon outlines or anime-style characters. The value of a negative list is that it rules out “cheap-looking” implementation approaches in advance.

    3. Case Comparison: From a Night Animation to an Afternoon Still

    The second case can be written as: use a canvas-style 2D rendering API to output a 1920×1080 afternoon indoor light image. White walls, a wooden floor, and a glass door with panes; sunlight slants in from outside the door, casting crisp door-pane light and shadow on the wall, with a small patch of light on the floor; on the table, a white porcelain vase holding thin orange branches; a hat on a wooden chair; a few books stacked in the corner. The style is quiet interior photography, with all texture coming from the geometric relationship between the window panes and the sunlight. The technical constraint is that a still image is enough, and a fixed seed guarantees consistency when re-rendering. Delivery is a still frame first to confirm composition and light, followed by a 20-second slow loop after approval, with the light patch moving over time and the period dividing the total duration evenly.

    Both cases share the same skeleton: tool first, visuals second, technical constraints as the safety net, and delivery plus the negative list to close it out.

    4. Why “One-Shot Perfect Output” Is Most Likely a Disaster

    Pure-code generation is search-based creation: the model samples one path through an enormous implementation space. Without constraints, it makes random trade-offs between “looks right” and “runs at all”; with determinism constraints, the sampling space is compressed into a verifiable subset. The fixed seed solves “the same result every time,” the pure time function solves “arbitrary seeking and looping,” period alignment solves “seamless first and last frame,” geometric computation solves “believable light and shadow,” and sample confirmation solves “the direction does not drift.” Together these five things turn uncontrollable creation into a controllable engineering process.

    5. Practical Recommendations

    When writing the prompt, fill in the four parts in order: first the tool plus size and frame rate, then the visuals plus detail density, then the pure function, fixed seed, period alignment, and geometric lighting, and finally sample confirmation, full delivery, the explanatory page, and the negative list. If you use cloud compute, you can containerize the rendering job, write per-frame output to object storage, and use a logging service with monitoring and alerting to track rendering progress and retry failures, so that the whole pipeline is reproducible and operable.