Prompt & context engineering
The model is fixed; the one thing you control is what goes into the context window. Prompt engineering is crafting that input for a single call; context engineering is managing everything the model sees across a whole task or agent run. Both are about getting a next-token predictor to produce exactly what you need.
Basics Anatomy of a prompt
Most prompts have the same parts. Naming them helps you write better ones:
🎭 Role / system
Who the model should be and its rules ("You are a careful support agent…").
🎯 Task
The specific instruction — clear, unambiguous, one job.
📎 Context
The data it needs: documents, examples, prior turns.
📐 Format
Exactly how to answer — length, structure, JSON schema.
Toolbox Prompting techniques
| Technique | What & when |
|---|---|
| Zero-shot | Just ask. Fine for simple, well-known tasks. |
| Few-shot | Show 2–5 input→output examples so the model matches the pattern & format. |
| Role / persona | Set expertise & tone in the system prompt. |
| Chain-of-thought | Ask it to reason step by step before answering — better on multi-step problems. |
| Decomposition | Break a big task into smaller prompts/steps (see prompt chaining). |
| Give an "out" | Let it say "I don't know" to curb hallucination. |
| Delimiters | Fence untrusted input (quotes/XML tags) so instructions and data don't blur. |
System: You classify support tickets. Reply with ONE word: refund | tech | sales.
# few-shot examples set the pattern
"My card was charged twice" → refund
"The app crashes on login" → tech
"Do you offer volume discounts?" → sales
# the real input, fenced so it can't inject instructions
"<ticket>I can't reset my password</ticket>" →
Reliability Structured output
For anything a program will consume, don't parse prose — ask for JSON and, ideally, enforce a schema. Modern APIs support structured outputs / tool schemas that guarantee valid JSON, so you get typed data instead of "please output JSON" and hoping.
Extract the order into this exact JSON (no prose):
{ "id": string, "total": number, "status": "paid" | "pending" | "refunded" }
# Better: use the API's structured-output / tool-schema feature so the
# response is GUARANTEED to match — no fragile string parsing.
The budget The context window
Everything the model uses shares one token budget: system prompt + instructions + examples + retrieved data + conversation history + the output. Overflow it and older content is dropped or the call fails.
Save cost Prompt caching
If a large chunk of your prompt is the same every call — a long system prompt, a policy doc, few-shot examples — prompt caching stores the model's processed state for that prefix so repeat calls skip re-processing it: much cheaper and faster.
system=[{
"type": "text",
"text": LONG_STABLE_INSTRUCTIONS_AND_EXAMPLES,
"cache_control": {"type": "ephemeral"} # cache this prefix
}]
# put stable content first (cached), variable content last — big savings at volume
The frontier From prompt to context engineering
For a single call you write a prompt. For an agent running many steps, the harder problem is context engineering: deciding what the model sees at each step as history grows — what to keep, retrieve, summarize, or drop, and how to structure tools and memory.
✂️ Curate
Feed only what's relevant now; retrieve on demand (RAG) instead of stuffing everything.
🗜️ Compact
Summarize old turns / tool output so history doesn't blow the budget.
🧠 Remember
Persist durable facts to a memory store, not the running context.
🛠️ Shape tools
Clear tool names & descriptions are prompts — the model reads them to decide.
Cheat sheet
| Question | Short answer |
|---|---|
| Prompt vs context engineering? | Prompt = one call's input; context = everything the model sees across a task/agent run. |
| Highest-leverage habit? | Be specific + show the exact output format. |
| When to use few-shot? | To lock a pattern/format the model keeps getting wrong. |
| Machine-readable output? | Ask for JSON with a schema; use structured-output APIs. |
| Is more context better? | No — relevant & concise beats big; watch lost-in-the-middle. |
| Repeating a big prefix? | Prompt caching — stable content first, variable last. |
Knowledge check
Six questions on prompting & context. Pick an answer for each, then check your score.
Next steps
🧠 How LLMs work
The model underneath — why context & tokens are the levers.
🔎 RAG vs CAG
Grounding — the biggest source of good context.
🛠️ Harness vs Loop vs Graph
Context engineering as the harness engineer's craft.
📓 All tutorials
The hub — LLMs, agents, ML & more.