Under the Hood · Prompt Engineering

The Complete Prompt & Context Engineering Blueprint

17 techniques, each explained from first principles. Not just what to do, but why it works at the token and probability level.

13 sections17 techniquesinteractive checklist
01

How LLMs generate text

Why constraints work

An LLM generates text one token at a time. Each token is a word, a fragment, or sometimes a punctuation mark. At each step the model produces a probability distribution over its entire vocabulary — tens of thousands of tokens — and picks the next one by sampling from that distribution. Then it repeats. The output you see is a sequence of these individual token predictions, chained together.

This is why vague prompts produce mediocre output. When the prompt is open-ended, thousands of continuations are roughly equally probable. The model samples from a wide, flat distribution — and the most likely tokens in a wide distribution are the statistical average of everything similar in the training data. Safe, generic, forgettable.

Constraints change the shape of that distribution. They rule out large sections of probability space, concentrating mass on the tokens that match what you actually want. Every concrete detail you add — a specific format, a restriction, an example — reduces the space of equally probable guesses. Better prompts are not longer prompts. They are more constrained ones.

next-token probability distribution
it
the
a
I
you
Here
Sure
Based
As
To
In
This

All tokens share roughly equal probability. Nothing is ruled out.

A prompt is not a question. It is a set of constraints that narrows the probability space. The narrower, the more predictable, the more useful.

02

Constraint-first prompting

Lock down the probability space

Every prompt should lock down three layers before asking the model to do anything. Resources: what files, references, data, or tools to work with. Guardrails: what to avoid, what tone to keep, where the limits are. Output spec: the format, length, structure, and style of the final result.

The order matters. Most people write prompts backwards — they describe what they want and then tack on constraints at the end. But the model reads top-to-bottom, and early tokens shape what follows. If the first thing the model reads is an open-ended request, the constraints you add later are fighting against a framing that is already set.

Think of it like GPS. "Drive somewhere nice" gives you nothing useful. "Drive to 42 Oak Street, avoid highways, arrive by 3pm" eliminates most of the decision space and routes you correctly. The GPS does not need to know why you are going there — it needs the constraints.

three constraint layers
Files: design-spec.md, api-schema.json
Data: last 30 days of error logs
Tools: read-only filesystem access
Context: Python 3.11, FastAPI, PostgreSQL

Write the constraints first. Then write what you want. The model reads your prompt in order, and the opening sets everything that follows.

03

Few-shot examples

Show, don't just tell

Instead of describing what you want, show it. A few-shot prompt gives the model examples of the input and the desired output, then presents the real input. The model reads the examples, detects the pattern, and extends it. This is called in-context learning — the model is not being trained on new data, it is completing a sequence.

Research on this consistently shows that the format and structure of examples matters more than whether the labels are technically correct. The model is learning the shape of the task — the input-output mapping — not memorizing the specific values. This means you can use simplified examples as long as the pattern is clear. A badly formatted example is worse than no example at all.

Two to five examples is the usual range. Start with zero and add them only when output drifts from what you want. Positive examples show what good looks like. Negative examples — with a brief note on why they are wrong — are underused and often more useful than the positive ones.

few-shot prompt builder
prompt
Classify the following customer email as:
complaint, inquiry, or praise.

[INPUT]
Email: "My order hasn't arrived in 2 weeks and nobody replied"
Category:
model output
complaint

[Note: zero-shot. The model guesses the format. It might
write a full sentence, use different label names, or add
unsolicited explanation. No pattern was established.]

The format of your examples is part of the instruction. Inconsistent formatting across examples confuses the model about the shape of the task.

04

Structured input and output

Templates the model can complete

An LLM is an autocompletion engine. Give it a labeled template and it will complete it. This sounds trivial but it changes what the model has to figure out. Without structure, the model has to decide simultaneously what to say and how to organize it — two competing constraints on the same probability distribution. With structure, the organization is already decided, and the model puts its full attention on content quality.

Structured input uses clear, labeled fields. "Project: X. Goal: Y. Constraint: Z." instead of a paragraph of prose. The labels tell the model how to weight different parts of the input — what is context, what is the task, what is a limit. Structured output pins the response to a predictable format you can parse or display reliably.

For programmatic use, JSON is the most reliable format to request. Give the model the schema — not just "return JSON" but the exact keys and types you expect. Most modern LLMs can follow a schema definition well enough to build a parser around it.

unstructured vs structured
prompt
I need help with a bug in my payment service. It keeps throwing a timeout exception when the order total is over $500. It worked fine last week but now it's happening all the time. I'm using Stripe's API and I think maybe the webhook is the problem but I'm not sure. Can you help me figure out what's wrong?
output
This could be a few things. Stripe webhooks have a 30-second timeout, and if your server isn't responding fast enough, it might be failing silently. You should also check your payment processing logic and make sure you're handling large orders correctly. It's also possible that the issue is related to your database queries taking longer for higher-value orders, or perhaps a third-party service you're calling is...

Structured output isn't just cleaner. It removes an entire class of prompt failures — the ones where the model gives you the right answer in the wrong format.

05

Delimiters and sections

Help the model know what is what

Without clear boundaries in a prompt, the model can confuse your background information with your instructions. A long context block at the start looks like text to complete. The model may start "finishing" your context paragraph instead of following the task that comes later. Delimiters create explicit separators that help the model's attention mechanism distinguish between what to know and what to do.

The specific delimiter does not matter much — ===, XML tags, triple dashes, markdown headers all work. What matters is consistency. The model will learn your separator pattern within a few lines and apply it correctly. The failure mode is mixing formats or forgetting delimiters partway through a longer prompt.

There is also a prompt injection angle here. When an agent retrieves documents and injects them into the context, those documents could contain text that looks like instructions. Clear delimiters and explicit section labeling help the model distinguish between "this is data I retrieved" and "this is an instruction I should follow." It is not a complete defense, but it reduces the surface area.

prompt anatomy
You are analyzing a Node.js payment service.
The codebase uses Stripe API v3 on Node.js 20.
The team follows TypeScript strict mode.
Deployments run on AWS ECS with a 30-second health check.

Identify the root cause of the timeout exception
that occurs on orders over $500.
Do not suggest fixes yet.

Return: root_cause (one sentence), evidence (2-3 points),
confidence (low / medium / high)

— undivided. model may misread where context ends and task begins.

Delimiters are a communication protocol between you and the model. They tell the attention mechanism where one type of information ends and another begins.

06

Context compression

Dense beats long

The real limit is not context window size — it is attention. Self-attention distributes weight across all input tokens when computing each output token. As the input grows, useful signal gets diluted. Researchers call this the "lost in the middle" problem: models reliably recall information at the very start and end of a long context, but miss details buried in the middle. This is reproducible and consistent across model families.

The fix is not pasting shorter documents — it is compressing the information itself. A dense paragraph where every sentence carries new information beats five paragraphs of padded prose. One line per decision, one line per constraint, one line per open question. The model does not need your reasoning — it needs your conclusions.

Open every session with a structured context block: project name and objective, decisions already locked in, current blockers or unknowns, definition of done, active constraints and preferences. That is usually five to eight lines. It takes about 30 seconds to write and dramatically reduces the amount of generic or off-target output you get back.

lost in the middle
model recall by position in documentlength: 45%
high recall
low recall
high recall

drag to increase document length — watch the low-recall zone expand

compressed context block — all green
6 lines · all positions recalled
Project: payment-service (Node.js 20 / TypeScript / Stripe v3)
Objective: fix webhook timeout on orders >$500
Decided: root cause is fraud check latency (35s avg vs 30s limit)
Blocker: fraud check tightly coupled to webhook handler
Definition of done: webhook returns 200 in <5s, fraud check runs async
Constraint: no new dependencies, stay in existing AWS ECS setup

Dense context beats long context. Every filler sentence you put into a prompt is a slot that could have held useful signal — or a slot that forces useful signal into the forgotten middle.

07

Primacy and negative instructions

Front-load and fence off

The first tokens you write carry disproportionate weight over everything generated after them. This is primacy bias, and it is real and measurable. When a prompt opens with an open-ended request, the model frames the entire response around that opening — constraints added later are adjusting a trajectory that is already set. Front-load what matters most: the goal, the constraints, the definition of done.

Negative instructions work by suppressing specific token sequences. LLMs are biased toward their most common training patterns — phrases like "leverage synergies" or "it is important to note" appear constantly in training data, so the model gravitates toward them unless told not to. Negative instructions lower the probability of those specific sequences. Positive instructions tell the model what hill to climb. Negative instructions fence off the valleys you do not want it to fall into. You need both.

Common mistakes with negative instructions: being too vague ("don't be generic") or stacking too many negatives at the end. Specific negatives work better ("do not use the words 'leverage' or 'synergy'"). And put them near the front — not buried in the last paragraph after the model has already picked a direction.

prompt ordering
■ constraint■ task■ output spec
Write a competitive analysis comparing Notion, Obsidian, and Roam.
task
Cover features, pricing, target audience, and integrations.
Be thorough and detailed.
Do not write a sales pitch.
constraint
Do not use the word "leverage".
constraint
Format it as a markdown table.
output spec
Keep it under 500 words.
output spec

If your constraints aren't in the first quarter of your prompt, they're working harder than they need to. Put them at the top.

08

One task, think first

Single objective + chain-of-thought

"Research and write and edit and format" is four tasks. Each pulls the probability distribution in a different direction. The model compromises, so none of them get full attention. Single-task prompts let the model allocate its entire generation budget to one objective. If you catch yourself using "and" more than once in a task description, you have too many tasks.

Chain-of-thought means forcing the model to reason through intermediate steps before giving a final answer. For anything involving math, logic, comparison, or multi-step decisions, explicitly asking the model to plan before executing improves accuracy measurably. Without it, the model jumps to an answer using shallow pattern matching. With it, each intermediate step narrows the probability space for the next one.

Two patterns worth keeping: "Do not produce anything yet. Outline your approach and let me review it." (good for longer outputs where a bad plan wastes effort) and "Think through this step by step before giving your final answer." (good for self-contained reasoning). The first gives you a review checkpoint. The second improves accuracy within a single response.

task focus comparison
prompt
Research best practices for microservices and write a blog post and create a code example and add tests and format it for Medium.

attention budget split

research
write
code
tests
output
# Microservices Best Practices

Microservices architecture offers several advantages over monolithic design...

Here's a basic example:
```typescript
// basic service skeleton
```

[truncated — depth thin across all four tasks, none done well]

The model's output quality scales inversely with the number of things you ask it to do at once. One task is not a limitation — it's a discipline.

09

Iterative refinement and chaining

Prompts in sequence

A single prompt asks the model to make many sequential decisions with no feedback. Errors in early decisions compound — a wrong framing in paragraph one makes paragraph two worse, paragraph three worse still. Iterative prompting breaks this by inserting review points where you can catch and correct course before an error propagates through the rest of the output.

Prompt chaining works the same way at a larger scale. Instead of one big prompt covering research, planning, and execution, you write three separate prompts where each takes the previous output as input. Each step gets a smaller, more focused problem — which the model handles better. The output of step one becomes constrained input for step two.

This does not mean micromanaging every sentence. The point is that complex work has natural phases, and those phases often need different prompting strategies. Broad and generative for ideation. Structured and evaluative for selection. Specific and constrained for execution. Keeping these separate keeps each prompt's objective clean.

prompt chain
Step 1
Generate 5 angles for an article on microservices failure modes.
output

1. Cascading failures and circuit breakers

2. Retry storms in distributed systems

3. Service mesh overhead vs. naked HTTP

4. Database connection pool exhaustion under load

5. Config drift across environments and deployment targets

output feeds next prompt
Step 2
Using angle #1 (cascading failures), create a 5-section article outline.
output feeds next prompt
Step 3
Write section 2 (circuit breakers) in a direct, technical tone. Under 200 words.

Every time you insert a review point between prompt and output, you reduce the blast radius of an early mistake.

10

Session management

Split, hand off, consolidate

Long conversations degrade. Not dramatically — the model does not suddenly forget your name. It is subtler: earlier context gets diluted, the model starts contradicting things it said 20 messages ago, and the broad exploratory thinking from the research phase bleeds into the execution phase where you need precision. Context decay and task interference are separate problems that look similar and both get worse the longer a session runs.

The fix is splitting work across focused sessions. One session for research. One for planning. One for execution. Not because these cannot happen in sequence — they often do — but because carrying the full context of all three phases into the execution session adds noise and increases the chance that the model revisits decisions that are already made.

The hand-off prompt matters. Never say "based on what we discussed." State exactly what was decided and exactly what to do next. "We chose approach B. The constraints are X and Y. The output should be Z. Begin." This gives the next session everything it needs and nothing it does not.

The consolidation pattern: end every long session by asking the model to summarize it. "What decisions were made? What is unresolved? What context would a fresh session need?" Save that output. Paste it as the opening of the next session. You are using the model's strength — compression — to patch its weakness: no persistent memory.

session lifecycle
session flow
example hand-off prompt for session 3
We chose approach B (async fraud check via SQS queue).
Constraints: no new dependencies, stay in existing ECS setup.
Decided: webhook handler returns 200 immediately, fraud check runs in background.
Decided: failure in fraud check triggers a separate retry queue, not webhook retry.
Open: error handling if SQS is unavailable during fraud check submission.
Output: TypeScript implementation of updated webhook handler. Do not add tests yet.

A session summary is a portable save file. Write it at the end of every significant session, before you close the tab.

11

Temperature and sampling

Controlling randomness

Temperature controls how the model samples from the probability distribution it computes at each step. At temperature 0, the model always picks the highest-probability token — deterministic, consistent, good for factual extraction and code. At high temperature (above 0.8), the model samples broadly, giving lower-probability tokens a real chance. This produces more varied output and more creative combinations, but also more errors and more hallucination.

The practical ranges: 0–0.2 for tasks where accuracy and consistency matter (data extraction, classification, code, factual QA). 0.3–0.7 for general writing, planning, and summaries where you want coherent output with some natural variation. 0.8–1.0 for brainstorming, ideation, and creative writing where variety is the point.

Most consumer tools do not expose temperature directly. But knowing how it works changes how you prompt. When you want deterministic output, phrase your prompt so there is one clearly correct answer — this reduces the number of equally valid continuations, which has the same effect as lowering temperature. When you want creative output, use open-ended framing that expands the space of valid continuations.

temperature explorer
distribution width at temp 0.50balanced
0 — deterministic1 — maximum entropy
prompt: "What's a good name for a coffee shop?"temp 0.5 — balanced
"Grounds & Co." — clean and versatile
"The Press" — minimal, appeals to specialty crowd
"Common Cup" — approachable, everyday feel

Temperature is not a creativity dial. It's a precision dial. Lower means more precise. Higher means more varied. Use it based on what the task actually needs.

12

Self-consistency, priming, meta prompting

The last 10%

Self-consistency: for high-stakes reasoning, run the same prompt multiple times and compare outputs. A single chain of reasoning can go wrong early and produce a confidently wrong answer with no visible error signal. Running multiple paths and taking the most frequent answer is like polling a jury — statistically more reliable than any single response. This is useful for math, logic, and multi-step problems. It costs more tokens but is worth it when the answer actually matters.

Output priming: give the model the first few words of the answer you want. "Begin your response with: 'Based on the three quarterly reports, the primary risk is...'" The model continues from where you left off rather than choosing its own starting point. This removes one of the biggest sources of unexpected output format and tone — the model's choice of how to open a response. It sounds like a small thing. In practice it removes a surprising amount of variance.

Meta prompting: ask the model to improve your prompt before you use it. "I want to write a prompt to get a competitive analysis of three tools. Improve this prompt for clarity, structure, examples, and output format." The model has been trained on enormous amounts of prompt-response pairs, so it has implicit knowledge of what structure produces better results. Using it to critique and refine its own input is a feedback loop that often catches ambiguities you missed.

advanced techniques
without
Q: A store sells apples at $1.20, $1.80, and $2.40 per pound.
If you buy 2 lb of each, what is the total cost?

[run once → answer: $10.80]
[one chain of reasoning, one potential error point]
with
[run the same prompt 3× independently]

→ Run 1: $10.80
→ Run 2: $10.80
→ Run 3: $11.40

Most frequent: $10.80 ✓
(single-run confidence: 67% → ensemble: 90%+)

mechanism

Statistical majority vote across multiple reasoning paths reduces single-path error propagation. One path can go wrong early and produce a confidently wrong answer with no visible error signal. Three paths polling the same result are much harder to fool.

when to use

Math, logic, multi-step reasoning, any calculation where a confident wrong answer has real cost. The token overhead is real — only use it when accuracy matters more than speed.

These are multipliers, not foundations. Get the basics right first. Then use these to close the last 10% of the gap between good and reliable.

13

The complete checklist

One page, everything you need

All seventeen techniques reduce to one principle: the most useful prompt is not the longest one — it is the one that leaves the model the fewest things to guess about. Every open decision in a prompt is a roll of the dice. Constraints load those dice in your favor.

prompt engineering checklist

0/ 17 complete

Foundationitems 1–4
Contextitems 5–7
Executionitems 8–13
Advanceditems 14–17

Verify everything. The model generates plausible text — not accurate text. This is not a solvable problem; it is a fundamental property of how these systems work.