Prompt Engineering with Large Language Models: A Practical Guide to Structured Prompt Compilation
\nThis article explains how large language models can be used to compile natural language intent into structured, reproducible prompts for image and video generation. It covers a three-layer prompt compilation pipeline, automated prompt optimization strategies, controlled vocabulary integration, quality evaluation loops, and engineering practices for cost and reliability control.
\n## 1. Background: The Bottleneck Is Shifting from Model Capability to Expression Precision\n\nOn March 17, 2025, a short-video platform announced that its in-house video generation model had been fully integrated with a deep reasoning large language model. In scenarios such as video generation and image generation, the language model can help generate or refine prompts. Its “Inspiration” capability can also work with a built-in inspiration vocabulary to further improve visual expressiveness.\n\nThe technical significance of this is not simply “one more chat entry point.” It reflects an engineering judgment that is becoming an industry consensus: as improvements in the capability of underlying generation models slow down, the key variable determining the upper limit of output quality is gradually shifting to the precision of prompt expression.\n\nFor developers, this shift creates three practical pain points:\n\n- Prompts are unstructured input. Natural language is ambiguous, and different expressions of the same intent can lead to completely different compositions, materials, and camera language.\n- Iteration is expensive. A single video generation inference takes much more time and compute than text generation, making manual trial-and-error tuning extremely costly at the margin.\n- Quality is not reproducible. Without version management and evaluation mechanisms, teams struggle to accumulate reusable prompt assets.\n\nIntroducing a language model into the generation pipeline is essentially adding a semantic compilation layer before the generation model: it compiles a user’s vague natural language intent into structured, enumerable, and reproducible generation instructions.\n\n## 2. Overall Architecture: A Three-Layer Decoupled Prompt Compilation Pipeline\n\nA practical engineering architecture is typically divided into three layers, decoupled through explicit contracts:\n\n1. Intent layer: Receives the user’s original description and performs clarification, completion, and constraint extraction.\n2. Compilation layer: A large language model maps the intent to a structured prompt object (JSON), with fields for the subject, scene, camera, lighting, style, duration, negative constraints, and more.\n3. Execution layer: Calls image/video generation capabilities and stores the returned results together with the prompt version for evaluation and retraining.\n\nAn example structured prompt object is shown below:\n\njson\n{\n "subject": "A pedestrian in a dark trench coat walking against the light",\n "scene": "A rainy city street at night with reflective puddles on the ground",\n "camera": { "shot": "medium", "move": "slow_push_in", "focal_len": 35 },\n "lighting": { "key": "backlight", "color_temp": 4300 },\n "style": ["cinematic", "shallow_depth_of_field"],\n "duration_sec": 5,\n "negative": ["text watermark", "body distortion", "frame jitter"]\n}\n\n\nThe value of this field-based representation is that it turns “writing longer” into “writing more precisely.” Fields can be enumerated, validated, and selectively replaced, turning prompts from personal experience into configurations that a team can maintain.\n\n## 3. Automated Prompt Optimization: From Synonym Replacement to Constraint Solving\n\nWhen a language model participates in prompt optimization, it should go beyond synonym replacement that simply adds adjectives. It should combine four types of operations:\n\n- Intent clarification: Proactively ask about missing dimensions or fill them in with default strategies, such as defaulting to a medium shot when no camera shot is specified.\n- Making constraints explicit: Convert implicit requirements into explicit fields, for example mapping “premium feel” to low-saturation colors, soft key lighting, and shallow depth of field.\n- Conflict resolution: When “wide angle” conflicts with “no character distortion,” trim or compromise according to priority rules.\n- Negative prompt expansion: Generate negative constraints based on common failure modes to reduce distortion, flickering, garbled text, and similar issues.\n\nA minimal usable optimization function signature can be abstracted as:\n\npython\ndef optimize_prompt(raw: str, ctx: dict) -> PromptSpec:\n \"\"\"Compile natural language intent into a structured prompt specification. ctx carries style preferences and platform constraints.\"\"\"\n ...\n\n\nIn engineering practice, it is recommended to lower the temperature parameter, require the model to output strict JSON, and perform schema validation with retries on failure. This prevents free-form text from flowing back into the execution layer and causing uncontrollable fluctuations.\n\n## 4. Inspiration Vocabulary Integration: Turning Random Inspiration into a Controllable Variable\n\nThe technical essence of the inspiration vocabulary integration mentioned above is a controlled vocabulary space. The problem with pure free generation is that the distribution is uncontrollable, and the model may produce descriptions that do not match the platform’s rendering capabilities. A vocabulary fixes high-frequency, validated expressions as searchable entries, forming a dual channel of “generation + constraint”:\n\n- The language model handles combination and rewriting to ensure diversity.\n- The vocabulary handles boundaries and quality baselines to ensure usability.\n- The two are cross-retrieved through a tagging system (theme, emotion, camera, color tone) to achieve “controlled randomness.”\n\nA practical recommendation is to establish tag dimensions and hit-rate statistics for the vocabulary, periodically remove low-efficiency entries, and retain A/B testing capability so that data, rather than intuition, decides which entries stay.\n\n## 5. Quality Evaluation and Feedback Loop: Making Optimization Measurable\n\nWithout evaluation, there is no optimization. It is recommended to build three layers of metrics:\n\n1. Semantic consistency: How well the generated result matches the structured fields, which can be scored using an image-text matching model.\n2. Stability: The variance of results from multiple generations using the same prompt, measuring reproducibility.\n3. Human preference: Blind evaluation win rates on small samples, used to calibrate automatic metrics.\n\nThe feedback loop is: generation → automatic scoring → attribution of low-scoring samples (missing intent or conflicting constraints) → feeding back into prompt templates and the vocabulary → regression validation. All prompts and results should be stored with version numbers to make issues traceable.\n\n## 6. Engineering Implementation and Cost Governance\n\n- Caching and reuse: Hash normalized prompts for caching, and skip the language model call on a cache hit.\n- Tiered strategy: Route simple intents through templates and only call the reasoning model for complex intents to control unit cost.\n- Asynchronous and streaming processing: Video generation takes a long time, so use a task queue with status callbacks to avoid request pileups.\n- Observability: Use logging services and monitoring alerts to track failure rates, latency, and score distributions.\n- Security and compliance: Perform content moderation and sensitive-word filtering on inputs and outputs, and retain audit records.\n\nIf the entire pipeline is deployed in the cloud, container platforms can host inference services, object storage can hold assets and prompt libraries, and message queues can smooth out task peaks, forming an elastic generation architecture. In such scenarios, Baidu AI Cloud typically provides support through a combination of general cloud products.\n\n## 7. Conclusion\n\nUsing large language models to optimize prompts marks the transition of generative content production from “manual parameter tuning” to “semantic compilation.” What developers truly need to build is not longer prompts, but a verifiable, measurable, and reusable compilation pipeline: with structured specifications as the contract, a controlled vocabulary as the boundary, and an evaluation loop as the driver. Only in this way can the expressiveness and stability of generated content be raised to a new level while keeping compute costs under control.
