M/Visual Library
  1. Visual Library
  2. Gen AI
  3. Prompt Engineering: How to Write Prompts That Work

Gen AI

Prompt Engineering: How to Write Prompts That Work

Prompt engineering is writing instructions that get a model to do what you mean: the core techniques, how image prompts differ and where language does the work.

Also known as: prompt engineering, what is prompt engineering, prompt design, prompt writing, prompting techniques, prompt engineering for images, image prompt engineering, few-shot prompting, mahsan prompts

A new name for an old skill

Prompt engineering is the practice of designing the input to a generative model so that its output matches what you intended. The term spread around 2020 and 2021, when GPT-3 showed that a large language model could do a new task from a few examples written into the prompt, with no retraining. Researchers called this in-context learning. Within a year, people were writing prompts for image models like DALL·E 2, Midjourney and Stable Diffusion, and the same word covered both.

The word 'engineering' oversells and undersells it at once. There is no formula that always works, and models change faster than recipes. But there is a method: state the task clearly, give the model what it needs to know, show rather than hint, test, and change one thing at a time. Underneath it is an old human skill, saying what you mean to someone who cannot read your mind. That makes it a language problem as much as a technical one, which is why this library treats it through semantics, pragmatics and description.

The core techniques

Be specific

Name the subject, the action, the setting and the result you want. Vague in, average out.

Give the goal, not only the task

Say what the output is for: 'a cover image for a book on grief' guides choices that 'a sad image' cannot.

Zero-shot and few-shot

Zero-shot is an instruction alone. Few-shot adds a few worked examples, which models copy closely, including their flaws.

Role and audience

Telling a text model who it is speaking as, and to whom, sets register, depth and vocabulary.

Structure

Separate instructions, material and examples with headings or tags so the model can tell them apart.

Constraints

Length, format, what to include and what to leave out. State the positive version where you can.

Step by step

For reasoning tasks, asking a model to work through steps before answering often improves accuracy (chain-of-thought).

Iterate

Change one variable at a time and keep what worked. A prompt is a draft, not a spell.

How image prompts differ

Description, not instruction

Image models respond to what the picture contains, not to requests. 'A red chair by a window' beats 'please draw me a chair'.

Order and weight

Earlier words usually carry more weight; the main subject belongs near the start.

Visual vocabulary

Light, lens, composition, palette and material words do more than adjectives like 'beautiful' or 'stunning'.

Negation is weak

'No people' can add people. Describe what is there instead, or use a separate negative field.

Settings are part of the prompt

Aspect ratio, seed, style strength and reference images change the result as much as words.

Where language does the work

Ambiguity

'A bat on the table' has two readings; models pick one silently. Disambiguate in the prompt.

Implicature

Models fill gaps with the most typical reading. What you leave unsaid becomes the default.

Prototypes

'A bird' becomes a sparrow-like bird. Name the category member you actually want.

Compositionality

Models are good at single concepts and weaker at binding: which object is red, which is on the left. Spell relations out.

Register

Technical, poetic or casual wording pulls the output toward images that were captioned that way.

The words to describe it

Precise words for the qualities above. Combine a few of them instead of naming the style or artist alone.

  • prompt engineering
  • in-context learning
  • zero-shot prompting
  • few-shot prompting
  • chain-of-thought
  • prompt template
  • system prompt
  • negative prompt
  • prompt weighting
  • iteration
  • ambiguity
  • specificity
A description to start from

'A cover image for a short book about grief: an empty wooden chair by a rain-streaked window, soft grey morning light, a cardigan left over the back of the chair, muted blue and cream palette, 35mm lens at eye level, generous empty space at the top for a title.'

How to put it into words

  • State the subject, action and setting first.
  • Add the visual decisions: light, lens, composition, palette, material.
  • Say what it is for and who will see it.
  • Change one thing at a time, and write down what each change did.

Where we usually get it wrong

Collecting magic words. Lists of 'best prompts' and trick phrases like 'masterpiece, 8k, trending' worked on some models for a while and then stopped. What lasts is the ability to describe an image precisely, which carries across models and versions. The words for light, space and material are the real technique.

Try this

Take a prompt you used recently. Rewrite it three ways: once with only the subject, once with subject plus three visual decisions (light, lens, palette), once with the purpose added ('for a poster about...'). Run all three with the same seed and settings and note which words changed the image.

Sources and further reading

  • Tom Brown et al., 'Language Models are Few-Shot Learners' (2020).
  • Vivian Liu and Lydia Chilton, 'Design Guidelines for Prompt Engineering Text-to-Image Generative Models', CHI (2022).
  • Jason Wei et al., 'Chain-of-Thought Prompting Elicits Reasoning in Large Language Models' (2022).
  • Pengfei Liu et al., 'Pre-train, Prompt, and Predict: A Systematic Survey of Prompting Methods in NLP', ACM Computing Surveys (2023).
  • Jonas Oppenlaender, 'A Taxonomy of Prompt Modifiers for Text-to-Image Generation' (2023).

Keep looking