A prompt is the only code an LLM runs. This guide climbs from the plainest question to ReAct agents, testing every technique on one stubborn little problem, and shows you exactly when a change of wording turns a wrong answer right. There is a live reasoning-vote you can drag, two agent loops you scroll through, and a lab at the end.
Here is the same question handed to a capable model as a bare instruction, the way most people prompt on their first day. Read what comes back.
The real answer is 9. Morning removes 8 and leaves 12; afternoon removes a quarter of twelve, which is 3; nine remain. The model did not reason toward 7. It produced the most probable-looking sentence, and adding two percentages looks probable. Nothing about the output signals doubt, which is the whole danger.
This is the ground everything else stands on. The model is a next-token predictor: it reads your text and guesses the next word, then the next, each guess shaped by the mountain of writing it learned from. It has no separate place where facts live and reasoning happens. The guessing is the thinking. The prompt is your only lever on what it guesses. Every technique in this guide is a different way to pull that lever, and we will test each one on these same 20 apples so you can see exactly what each is worth.
The cheapest upgrade costs no new technique, only precision. A zero-shot prompt gives no examples; it leans entirely on the instruction, so the instruction has to carry the load. Vague asks get vague, average completions. Hover each part of the prompt below to see what job it does.
Precision alone will not fix the apples yet, because the failure is in the reasoning, not the phrasing. But clarity removes an entire class of failure: the model answering a question you did not mean to ask. Most bad outputs are bad inputs wearing a disguise.
Chat models read two channels. The system prompt sets standing rules, persona, and boundaries for the whole conversation; the user prompt carries the turn-by-turn request. Assigning a role in the system channel shifts the whole distribution of the reply: vocabulary, caution, format, what it assumes you know.
The role is not decoration. "You are a careful math checker who works step by step" quietly imports a behavior we are about to make explicit. A good persona is a compressed instruction. A costume with no instructions inside it ("you are a world-class genius") buys nothing.
Ask for prose and you get prose-shaped thinking. Ask for a table, a JSON object, or numbered steps, and the required shape drags the reasoning with it. Forcing a field called "steps" before a field called "answer" is a back-door way to make the model reason before it commits, which is the whole idea of the next part. Click through three output contracts for the apples task.
"steps" field before an "answer" field does the same thing to the model: the shape of the answer makes it think before it lands. The container you ask for changes what gets poured into it.Instead of describing the behavior you want, demonstrate it. Few-shot prompting puts a handful of worked examples in the context before the real question. The model reads the pattern and continues it. Each example teaches something specific: the format, the tone, the edge case, the method. Hover the examples below to see what each one is actually buying.
Few-shot is powerful and quietly expensive. Every example rides in the context on every single call, costing tokens and latency forever. It also caps out: showing the method is not the same as making the model perform the method on a genuinely new shape. For that, you stop showing answers and start demanding the work.
Four words changed prompting more than any prompt library: let's think step by step. Chain-of-thought asks the model to write its reasoning before its answer. Because each token it writes becomes context for the next, the written steps literally give it more room to compute before it commits. Scroll, and watch the scratchpad fill.
We append one line to the prompt: Let's think step by step. That is the entire technique. No examples, no tools.
The model now narrates. It writes down the starting amount before doing anything to it. A number on the page is a number it will not misremember.
It computes the morning sale and the remainder as a separate written fact: 8 sold, 12 left. This is the exact step the blurted answer skipped.
Now the move that decides everything. The afternoon percentage is applied to the 12 it just wrote, not to the original 20. Twenty-five percent of twelve is three.
Twelve minus three. The answer is not guessed; it is the last line of an argument the model can see. Nine.
Nothing was added to the model. The reasoning was always latent; the prompt gave it room to run before committing. That is the recurring lesson: the words do not describe the computation, they are the computation.
Chain-of-thought helps, but a single reasoning path can still slip. Self-consistency runs the reasoning several times at nonzero temperature, so each run wanders a little, then takes the majority answer. Wrong paths tend to fail in scattered, different ways; the right path keeps landing on the same number. The vote finds it.
This is the one control that carries the whole guide, so it is real. Every time you move the slider the page samples that many independent reasoning attempts and tallies them live. Set the reasoning quality with the chips, then drag the number of samples and watch the majority sharpen.
Two lessons hide in that slider. Reasoning quality sets where the vote tends to land, and sample count sets how reliably it lands there. Self-consistency buys reliability with compute: five to forty times the cost for one answer. You spend it only where being wrong is expensive.
All of Part II reasons inside the model's own head, which fails the moment the answer lives in the world: today's price, a database row, a fact past the training cutoff. ReAct interleaves reasoning with actions. The model writes a Thought, chooses an Action (a tool call), reads the Observation that comes back, and loops until it can answer. Scroll the loop.
A question the model cannot answer from memory: it needs two live prices. Pure reasoning would hallucinate them. ReAct refuses to guess.
It reasons about what it is missing and what to do about it: it needs this shop's price, and there is a tool for that.
It emits a structured tool call. The runtime executes it and feeds the real result back in as an Observation. This line is not the model's imagination; it is data.
One price is not enough. The model thinks again, calls the market tool, and reads the second real number. Grounding accumulates.
Only now, holding two observed facts, does it compute and answer. Every number in the conclusion traces to a tool result, not a guess.
Strip the jargon and an agent is this loop plus tools plus a stopping rule. Frameworks dress it up, but underneath, every one of them is prompting the model to produce a Thought and an Action, running the Action themselves, and pasting the Observation back into the prompt.
ReAct only works because the Action is machine-readable. Modern models are trained for tool calling: you describe each tool as a name, a description, and a typed parameter schema, and the model replies with a structured call your code can execute. Click each layer of one tool round-trip.
Retrieval is just a tool. Point the search action at your own documents and you have the backbone of retrieval-augmented generation: the model asks for what it needs, your code fetches the real passage, and the answer is grounded in your data instead of the model's memory.
Chain-of-thought walks a single path. Some problems need search. Tree-of-thought lets the model branch: generate several candidate next steps, evaluate them, keep the promising branches, prune the rest. It trades a lot of compute for the ability to back out of a dead end that a single chain would ride to a wrong answer.
The humbler cousin of search is decomposition: break a hard task into ordered sub-tasks and prompt each one separately, feeding results forward. This is prompt chaining, and it is how most real systems are built. One reliable prompt per step beats one heroic prompt attempting everything, because you can test, cache, and fix each link alone.
Everything here bends the model's output without changing the model. That power has hard edges, and pretending otherwise is how prompt-engineered systems break in production.
It is sampling, not thinking. Chain-of-thought text is a useful artifact, but it is not a faithful log of a hidden reasoning process. A model can write correct steps and a wrong answer, or the reverse. Trust the method because it raises accuracy on your evals, not because the steps look convincing.
Prompts are brittle. Reordering examples, renaming a field, or a model version bump can move results more than you expect. A prompt is code with no type system. The only defense is an evaluation set you run on every change.
Prompt injection is unsolved. The moment your prompt includes untrusted text, a web page, a user document, an email, that text can try to hijack the instructions. Tool-using agents raise the stakes, because a hijacked instruction can now act. The weeds cover the partial defenses; none are complete.
More scaffolding costs more. Self-consistency multiplies calls, ReAct adds round-trips, tree search fans out. Reliability is bought with latency and money, and past a point a smaller, cheaper technique with a good eval beats an elaborate one you cannot afford to run.
Zero-shot asks.
Chain-of-thought reasons.
ReAct goes and finds out.
Pick the smallest one that gets you to nine.
The guide moved one variable at a time. Here they compose. Build a prompt stack for a task, and watch estimated reliability climb while cost and latency climb with it. The presets rebuild the setups this guide argued about. There is no single right answer; there is a right answer for your budget.
Seven rabbit holes the main path stepped around. Each stands alone.