Skip to Content

Prompt engineering patterns

The same model gives a chatty paragraph or a clean, parseable answer depending on how you build the prompt. See the parts of a good prompt go in one by one, and what each part changes.

9 minFree

Prompt engineering patterns
  1. Build the prompt
  2. Generate
  3. Parse
  4. Result
FIG. LLM-PROMPT-PATTERNS / 06 PARTSYour appsupport inboxSystem instr.role, steps, formatExamplesfew-shotGeminigemini-3.8-flashYour parserexpects category:Team queuerouted ticket

Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.

00/06
from google import genai

client = genai.Client()

# A zero-shot prompt: no role, no rules, no examples, no output format.
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input="Classify this support message: I paid Rs 450 for a recharge, "
    "it failed, where is my money?",
)

print(interaction.output_text)

What your app received

Nothing yet. Press Play.

Background: the idea in plain words1 min read

When a person reads “classify this message”, they ask what the categories are and what form the answer should take. A model can't ask. It fills the gaps with its best guess, and its guess is usually a friendly paragraph written for a human.

A prompt is the text you send to a model. Prompt engineering means building the prompt so there are no gaps to fill. A prompt for an app usually has four parts:

  • A system instruction (your fixed rules for the model) with the role (who the model is), numbered steps (what to do, in order) and the output format (what the answer must look like).
  • Few-shot examples: two or three worked examples of input and the exact answer you want.
  • Context: any documents or data the model needs.
  • The task itself, placed last.

Each part does a job. The role and steps tell the model what to do. The examples show what a good answer looks like, and the model copies their pattern. The task comes last, so it does not get lost.

The running example is a support inbox for a UPI payments app (UPI is a common way to pay by phone in India). Each message needs a category so it can go to the right team. Say a customer writes that a Rs 450 recharge failed. With a one-line prompt, the model replies with friendly paragraphs. With the full prompt, it replies with a line such as category: PAYMENT_FAILED, which your code can read.

Press Play to see a one-line prompt, then switch the Try it selector to the structured version and watch the same message come back in a form your code can use.

What to notice

  1. The model wasn't wrong the first time. It answered the question it was given. The bare prompt never listed the categories or said what to return, so the answer was written for a person, not for your parser.
  2. The system instruction carries the rules that never change. Gemini's prompting guide recommends putting the role, key constraints and output format in the system instruction (the system_instruction parameter), or at the very start of the prompt.
  3. Examples do more than instructions. Google's guide recommends always including few-shot examples, and says clear examples can even replace some instructions. Keep every example in exactly the same format, because the model copies the pattern, labels and spacing included.
  4. Structure and order matter. Separate the parts with XML-style tags (<role>, <task>) or Markdown headings, and pick one style. With long context, put the context first and the question at the end.
  5. “Step by step” means steps for the task. Gemini 2.5 and 3 models already think internally before they answer, so you don't need to ask them to show their reasoning. Numbered steps in the instruction are about your procedure: read, pick a category, write a reply.
PatternWhat it fixes
Role in the system instructionThe model doesn't know who it is meant to be
Clear constraints (“exactly one of …”)Vague or ambiguous terms
Few-shot examplesWrong format, phrasing or length
Tags or headingsInstructions mixed up with data
Context first, task lastThe question gets lost in a long prompt
Break into smaller promptsOne prompt trying to do too much

When the format really matters A prompt can ask for a format, but it can't force one. When your code must parse the answer every time, use structured output, the next lesson: Structured output.

Go deeper

Words to know

1 min read
WordMeaning in simple words
PromptThe text you send to a model
System instructionThe fixed rules for the model: its role, the steps and the output format
RoleWho the model is meant to be for this job
Few-shot examplesTwo or three worked examples that show the exact answer you want
Zero-shotA prompt with no examples at all
ParserThe part of your code that reads the model's answer and pulls out the values

Check your understanding

1. The bare prompt returned three helpful paragraphs and no category. What is the best first fix?
2. Why must every few-shot example use the same layout?
3. You are passing a 20-page policy document plus a question. Where should the question go?
Sources: the official documentation2 links

This lesson is based on the official documentation. Check it for the latest details.

Something wrong or unclear in this lesson? Tell us, it gets fixed.