Shape prompts and manage context
Write clear prompts with roles, examples and output formats, and keep what matters inside the context window.
- 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 XPFit 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?
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
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.
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])['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.
Build a few-shot prompt
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
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.
Validate a model’s JSON
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
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…