02 / 16

Prompting & Context Engineering

The cheapest lever you have: what goes into the window.

0/12 studied
01Anatomy of a prompt

A prompt is a list of role-tagged messages: system for stable rules, user for data and the question, assistant for prior turns. Put long documents first, the question last, and stable parts before variable ones.

Core◇ Diagram</> Code
02Zero-shot vs few-shot prompting

Zero-shot gives only instructions; few-shot adds a handful of input→output examples, which teach format and edge cases better than prose. The model also copies their quirks, so choose them deliberately.

Core◇ Diagram</> Code
03Chain-of-thought (CoT)

Have the model write intermediate steps before the answer: it buys accuracy on multi-step problems at the cost of tokens and latency. Reasoning models do it natively, so set their effort knob instead.

Core◇ Diagram</> Code
04Structured outputs & JSON mode

Don't ask for JSON, enforce it: strict schema mode masks every token that would break your schema, so output always parses. It guarantees shape, not truth, so still validate values and retry.

Core◇ Diagram</> Code
05Prompt techniques that matter

Treat the model like a brilliant new hire with zero context: say exactly what you want and why, separate data from instructions, show an example, and specify the output format.

Core◇ Diagram</> Code
06Context engineering

Deciding, at every step of an agent or pipeline, exactly which tokens enter the window (instructions, tools, documents, tool results, memory, history), aiming for the smallest set of high-signal tokens.

Advanced◇ Diagram</> Code
07Prompt caching

The provider keeps the computed KV state of a recently seen prompt prefix, so repeat calls skip that prefill: put static content first, variable content last, and cut repeated input cost by up to ~90–95%.

Core◇ Diagram</> Code
08Long context vs RAG

Stuff the whole corpus in the window when it's small, static and shared; use RAG when it's large, changing, permissioned or high-volume. Often you do both: retrieve whole documents, then reason in long context.

Advanced◇ Diagram</> Code
09Lost in the middle & context rot

Models don't use long context evenly: facts in the middle get used worst, and quality decays as input grows even on easy tasks. Send less, better context, with key parts at the edges.

Core◇ Diagram</> Code
10Prompts as code: versioning

Treat every prompt as a deployable artifact: templated, versioned with its model and parameters, reviewed, eval-gated in CI, rolled out gradually and logged with every trace so you can roll back in seconds.

Core◇ Diagram</> Code
11Self-consistency & best-of-N

Sample several reasoning paths at temperature > 0 and keep the answer most of them agree on, or let a verifier pick the best. Buys accuracy and a confidence signal for N× the tokens.

Advanced◇ Diagram</> Code
12Automatic prompt optimization (DSPy)

Stop hand-tuning prompts: define the task, an eval set and a metric, and let an optimizer search instructions and few-shot examples that maximize the metric. Prompts become learned parameters you recompile per model.

Advanced◇ Diagram</> Code