Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Prompting Fundamentals

Controlling Output Format and Length

Ask for lists, tables, headings or exact word counts and verify the result in code. Learn format instructions, length control and post-processing checks.

Left alone, a model chooses its own format and length: sometimes a paragraph, sometimes bullets with bold headings and a closing offer to help further. That is fine in a chat, but not when a person scans the result, a downstream program parses it, or it must fit a space. You control it by asking explicitly, and you confirm it with code.

Ask for a format by name

Name the format and describe its shape. The more exact, the better.

TEXT
Return a markdown table with columns: Feature | Benefit | Risk.
Return a numbered list of exactly 5 steps, each starting with a verb.
Return plain text only. No markdown, no headings, no emoji.
Return the answer on one line: YES or NO.

Specifying what not to include is useful for formats: many models add a friendly preamble ("Sure! Here is...") or a closing sentence. If your program expects only the payload, say so: "Output only the list, with no introduction or closing remarks."

Control length in measurable units

Lengths work best in units a model can roughly track: sentences, bullets, paragraphs. Word and character limits work but are approximate, because models do not count exactly. Expect small overruns, and design for them.

Instruction Reliability
"Exactly 3 bullets" High
"One sentence" High
"At most 50 words" Good, may overshoot a little
"Between 140 and 160 characters" Poor, verify in code

When a hard limit exists (a tweet, a meta description, a database column), ask for a bit less than the limit, then check and retry in code.

Python Verify format and length in code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two sample outputs are hand-written for the demo (not model output). In a real application, check_output would sit between the model call and the rest of your program.

Retry loops

When a check fails, send the problem back to the model. A tiny loop does this. The block uses a stand-in model that produces a too-long reply on the first try and an acceptable one after receiving feedback, so you can see the control flow without any API.

Python Check-and-retry loop (stand-in model)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Always cap the number of retries, as above. A loop that retries forever can burn money on a prompt that simply cannot meet the constraint.

Use examples and templates for tricky formats

When a format is complex, show it. Give a skeleton with placeholders:

TEXT
Use exactly this layout:

Title: <5 to 8 words>
Summary: <one sentence>
Tags: <3 comma-separated lowercase tags>

A filled-in example (see few-shot prompting) is even stronger than a description.

Match the format to the reader

  • For people: headings, short paragraphs, bullets. Put the answer first.
  • For programs: a strict machine format such as JSON (see Structured Output), with no prose around it.
  • For both: ask for a short human summary plus a separate structured block, clearly delimited.

Truncation and the max tokens trap

If you request a long answer but set a low max-tokens cap, the output is cut mid-thought, and no prompt wording can fix it. When a length requirement and a settings cap disagree, the cap wins. Check model settings when output ends abruptly.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which length instruction is usually followed most reliably?

    Models track sentences and bullets better than exact character counts.
  2. 2. Why add "output only the list, with no introduction"?

    Preambles like "Sure! Here is..." are common and undesirable when a program consumes the output.
  3. 3. A hard length limit exists. What is the safest approach?

    Verify in code because models count approximately, and cap the retries.
  4. 4. Output stops mid-sentence even though the prompt asks for a long answer. What should you check?

    A low max-tokens cap truncates output no matter how the prompt is worded.

Summary

  • Name the format and its shape; say what not to include (preambles, closings, markdown).
  • Use measurable lengths (sentences, bullets) and verify limits in code.
  • Retry with specific feedback, but cap the attempts.
  • Show a template or example for complex formats, and make sure max tokens does not truncate the answer.

Next: Delimiters and Prompt Templates