← All tutorials
Prompt & Context

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.

The core skill. Be specific, give examples, show the format you want, and supply the right context — no more, no less. Vague in, vague out.

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.

Biggest wins, in order: be specific about the task → show the desired output format → give a few examples → supply only the relevant context.

Toolbox  Prompting techniques

TechniqueWhat & when
Zero-shotJust ask. Fine for simple, well-known tasks.
Few-shotShow 2–5 input→output examples so the model matches the pattern & format.
Role / personaSet expertise & tone in the system prompt.
Chain-of-thoughtAsk it to reason step by step before answering — better on multi-step problems.
DecompositionBreak a big task into smaller prompts/steps (see prompt chaining).
Give an "out"Let it say "I don't know" to curb hallucination.
DelimitersFence untrusted input (quotes/XML tags) so instructions and data don't blur.
few-shot + role + format, in one prompt
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.

ask for a schema, not prose
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.

system+ instructions+ context / RAG+ history+ output≤ context window
More context isn't always better. Irrelevant or excessive context adds cost, latency, and "lost-in-the-middle" errors (models attend less to the middle of long inputs). Curate: retrieve the relevant chunks, summarize history, drop the rest.

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.

cache a stable prefix (Anthropic-style)
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
Structure for the cache: stable stuff first (cached), variable stuff last. This is also the mechanism behind CAG — see RAG vs CAG.

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.

The shift. As models and agents get more capable, the leverage moves from clever wording to managing the context — which is why "context engineering" is now the frontier skill (and a key job of the harness engineer).

Cheat sheet

QuestionShort 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.

1. What's the difference between prompt and context engineering?

2. What is few-shot prompting?

3. For output a program will parse, you should…

4. Is stuffing more context always better?

5. What does prompt caching save on?

6. Why does context engineering matter for agents?

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.