Skip to content
anythingai.space
Module 06

Prompting & Context

Prompt fundamentals, few-shot, chain/tree of thought, context engineering, structured output and injection defense.

lessons
8
steps
67
time
90 min
CONSISTENCY AS A SYSTEM PROPERTYtemperature=0Effort is the dial now,not sampling.Eval harnesstemperature: 0Model APIversion 2026-10-21400Pin model+effortSchema / enumInput-hash cachePass rateN runs per case
01beginner·11 min·9 steps

Prompt Engineering Fundamentals

Treat a prompt like code: state the intent, fence the data, respect the instruction hierarchy, and get consistency from the system rather than from temperature.

  • Write prompts as intent plus constraint
  • Explain the instruction hierarchy and why it is not a security boundary
  • Use roles for priorities and delimiters to separate data from instructions
  • Get reproducible outputs now that APIs reject temperature
Start →
EXAMPLE 1Input: “weather okay, flight late”inputWhy: neutral weather, bad servicereasoning (optional)Sentiment: Mixedgold outputEXAMPLE 2Input: “staff were lovely”Why: praises the peopleSentiment: PositiveNEW CASE: THE MODEL CONTINUES THE PATTERNInput: “food cold, view stunning”Sentiment: MixedNo weights change. The examples live only inthe prompt.
02beginner·10 min·8 steps

Few-Shot and In-Context Learning

Teach a task with examples in the prompt instead of training: how many, which ones, in what order, and when many-shot beats fine-tuning.

  • Build a good example: input, optional rationale, gold output
  • Pick the number of examples by model size
  • Select examples per query and keep them out of the cached prefix
  • Avoid label and recency bias
Start →
OUTPUT SHAPES THAT CAN TRIGGER IT<thinking>…</thinking>{"reasoning": …}scratchpad before answerASK FOR THIS INSTEAD{"explanation": short}{"evidence": […]}summarized thinking blocksPrompt“write <thinking> first”Newest ClaudeOpus 5.5 · Fable 5.1Refusalreasoning_extractionbilled
03intermediate·11 min·9 steps

Chain-of-Thought

Intermediate reasoning steps make hard problems tractable. Reasoning models now do this internally, so you control them with effort, not with prompts.

  • Explain why intermediate steps help on multi-step problems
  • Choose between zero-shot, few-shot and programmatic CoT
  • Control reasoning models with effort, and avoid reasoning-extraction refusals
  • Monitor actions rather than chains of thought
Start →
ProblemThought AThought BThought CA1dead endA2goodB1B2maybeSolution
04intermediate·10 min·9 steps

Tree-of-Thought

Explore several reasoning paths, score them, prune dead ends and backtrack. Know when an explicit search beats one long reasoning call.

  • See why a single chain can't recover from an early mistake
  • Run the propose → evaluate → search loop
  • Explain why the evaluator decides whether ToT works
  • Weigh ToT's cost against a reasoning model's internal search
Start →
src/auth.tsjust that fileIN CONTEXT: IDENTIFIERS ONLYsrc/auth.tsdocs/api.mdorders#4471tool_search()TOKEN CUTS FROM DYNAMIC LOADING (CURSOR, VENDOR-REPORTED)Static tool descriptions−60%Total, MCP sessions−46.9%Agentread_fileon demandRepo / databasenot in context
05intermediate·14 min·9 steps

Context Engineering

A 1M-token window is a budget, not free space. Curate the smallest high-signal set of tokens on every turn, and keep the prefix cacheable and append-only.

  • Explain context rot and budget each part of the window
  • Apply compaction, just-in-time loading and sub-agent isolation
  • Order a prompt for attention and for the cache
  • Keep the harness append-only so reasoning and caches survive
Start →
compiletoken maskOUTPUT: ONLY A PHONE NUMBER CAN COME OUT415-555-0199MASKED AT EVERY STEPSurecall(me\nRegex / CFG\d{3}-\d{3}-\d{4}Grammar backendXGrammar · llguidancecachedSelf-hosted LLMvLLM / SGLang
06intermediate·11 min·8 steps

Structured Generation

Get JSON that always parses by constraining decoding at the engine, not by asking nicely, then validate the content and retry with the error.

  • Explain how a grammar mask makes invalid output impossible
  • Constrain self-hosted models with regex and grammars
  • Replace forced tool calls and prefill on the newest Claude models
  • Split hard extractions and recover from validation errors
Start →
try candidateoutputsscoreCANDIDATES TRIED“Be helpful.”“Think carefully.”3 bootstrapped demosTrain setfew hundred labeledMIPROv2Bayesian searchPrograminstructions + demosMetric
07advanced·10 min·7 steps

Prompt Optimization with DSPy

Stop hand-tuning prompt text. Declare what each step does, define a metric, and let an optimizer compile the prompts, and even the program shape, for each model.

  • See why hand-tuned prompts break when the model changes
  • Separate logic (signatures, modules) from prompt text
  • Design the metric the optimizer will chase
  • Tell MIPROv2, GEPA, Flex and ReAnchor apart
Start →
TRUSTED · PRIVILEGEDUNTRUSTED · QUARANTINEDtool callssummarize $email1readvalue only$summary1 (reference)never sees itUser requestPrivileged LLMplans · calls toolsToolspolicy-checkedControllerstores $summary1Quarantined LLMno toolsUntrusted emailhidden instruction
08intermediate·13 min·8 steps

Prompt Injection and Defense

Some injections will always get through, so design the system so that a successful injection has nothing dangerous to do.

  • Tell direct from indirect injection
  • Read current attack-success baselines and what 1% means at scale
  • Separate privileges with the dual-LLM pattern
  • Break the lethal trifecta and bind approvals to actions
Start →
Read deeper

Guide chapters · Prompting & Context

Next module · 07
Retrieval & RAG