Loading
0x40Lesson 5 of 16

Shape prompts and manage context

Write clear prompts with roles, examples and output formats, and keep what matters inside the context window.

22 min 6-question quiz 2 code exercises
By the end of this lesson you can
  • Structure a prompt with role, task, context and format
  • Use few-shot examples and validate structured output
  • Manage a limited context window

Chat models receive a list of messages: a system message (standing instructions from the application), then alternating user and assistant turns. Under the hood these become one long token sequence. Good prompts usually include:

  • the task, stated plainly - and why, if it helps the model make judgment calls;
  • the context needed, clearly separated from instructions (for example in XML-style tags);
  • examples of good outputs (few-shot prompting) when the format or style matters;
  • the output format, ideally a schema your code validates.

Asking a model to think step by step before answering often improves accuracy on reasoning tasks; many current models do this automatically as built-in “extended thinking”.

Try it

Up to 15 XP

Fit a conversation in the window

A support chat has grown long. Switch between window sizes and see what gets dropped to keep the system prompt, the reference document, the question and room for the answer. Then ask: could the model still answer if it lost turn 1, where the customer gave their order number?

Context window:
System prompt
Turn 3
Turn 4
Latest question
answer

Using 3,750 of 4,000 tokens (1,000 reserved for the answer) · the full conversation is 8,550 tokens.

  • System prompt600
  • Turn 1 (order number given)900
  • Turn 21,400
  • Turn 31,200
  • Returns policy document3,500
  • Turn 4800
  • Latest question150
Dropped to fit: Turn 1 (order number given), Turn 2, Returns policy document. The model will never see them - it can’t answer from what was cut.

Context windows are big now - hundreds of thousands of tokens - but not free: every token costs money and time, and models use information in the middle of very long contexts less reliably. When history grows, summarize old turns, keep key facts (like that order number) pinned in the system prompt or a notes block, and retrieve documents instead of pasting everything.

messages.py
1messages = [
2    {"role": "system", "content": "You are a concise support assistant."},
3    {"role": "user", "content": "Where is order 4471?"},
4]
5print([message["role"] for message in messages])
Output
['system', 'user']

Key takeaways

  • State the task, separate context from instructions, show examples, specify the format.

  • Validate structured output in code; never assume it’s well-formed.

  • Budget the context window: keep key facts, summarize or drop old turns, retrieve documents.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Python

Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.

Exercise 1

Build a few-shot prompt

+25 XP

Lines before --- are labeled examples text|label; the line after is the new text. Print:

1Classify the sentiment of each review as positive or negative.
2
3Review: TEXT
4Sentiment: LABEL
5
6...one block per example...
7
8Review: NEW TEXT
9Sentiment:
  • Two examples
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 2

Validate a model’s JSON

+25 XP

Each line is a model reply that should be a JSON object with a string name and an integer age. Print valid, invalid: not JSON, invalid: missing FIELD (first missing, in that order) or invalid: FIELD should be TYPE (str or int; booleans are not ints).

  • Five replies
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…