Skip to content
AI Engineering
Prompting & Context

Zero-shot, few-shot and chain-of-thought

Three basic techniques cover most prompting. Each trades more tokens for more reliability.

Chapter 4: Prompting & ContextLesson 3 of 8Level: StarterAbout 15 min
Words on this page (7)
token
: A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
few-shot
: Giving a few worked examples in the prompt.
chain of thought
: Having the model write out intermediate reasoning steps before the final answer.
prompt
: The input text you give a model, including instructions, examples and data.
tool use
: The model asks your program to run a function and reads the result.
tool
: A function the model can ask your program to run, described by a name, description and argument schema.
training
: Adjusting a model's parameters on data so its predictions improve.

Zero-shot

You describe the task and give no examples. Modern models are good at this: "Classify the sentiment of this review as positive, negative or neutral." Use it first. If the output format or the edge cases go wrong, move to .

Few-shot

You show a few input and output pairs before the real input. The model infers the pattern. Choose examples that cover the varied cases, including a hard one, and keep their format identical. Three to five is typical. Examples also fix formatting better than a long description.

Chain-of-thought

You ask the model to reason step by step before answering, or you show worked reasoning in the examples. It helps on arithmetic, logic and multi-step questions. For reasoning models that think internally, skip it. For simple lookups it only adds .

Self-consistency samples several reasoning paths and takes the majority answer. chaining splits a task into prompts whose outputs feed the next. ReAct interleaves reasoning with . Tree of thoughts explores several branches. Each has its own page in the reference chapter later in this path, with worked examples.

A worked example: zero-shot, then few-shot

Zero-shot: "Classify this review: 'The battery died in two days.'" The model answers "Negative." Fine. Now you need labels like "battery", "shipping", "price". Few-shot: give three examples ("Arrived late" gives shipping, "Too expensive" gives price, "Won't hold a charge" gives battery) and then the new review. The output follows your labels exactly.

A worked example: chain of thought

Question: "A shop sells pens at 3 for $2. How much do 12 pens cost?" A model might blurt "$12". Asking it to "work step by step" gives: 12 pens is 4 groups of 3; 4 × $2 = $8. Writing the steps made the answer more reliable.

Why each technique works

Few-shot works because the examples become part of the text the model continues. It predicts what comes next in the pattern you set up. works for a different reason. The model produces one token at a time, and each token is computed from all the text before it. Writing the steps down puts intermediate results on the page where later tokens can use them. Zero-shot relies only on what taught.

The cost of each

Each technique spends tokens. Invented numbers: three examples of 40 tokens each add 3 × 40 = 120 tokens to every call, which is 1,200,000 extra tokens over 10,000 calls. Chain-of-thought changes the output side: a reply of 5 tokens may become 120. Choose the cheapest technique that passes your tests, and move up only when a test fails.

What goes wrong

With few-shot, the model may copy surface details. If your last example is "negative", it may lean toward "negative". To check, shuffle the examples. If results change a lot, the prompt is fragile. With chain-of-thought, fluent steps can still be wrong, and the written steps are not a reliable record of how the model really got its answer. Check the final answer independently rather than trusting a convincing explanation.

Try it yourself: zero-shot against few-shot

Write 10 short reviews and decide the right label for each. Run them zero-shot and count the correct labels. Then add three examples and run the same ten. Expect the biggest gain on labels and format, and little change on clearly positive or negative reviews. If there is no gain, the first prompt was already enough, and the examples only added cost.

Common mistakes

Adding step-by-step instructions to reasoning models that already think, which can make them worse. Using examples that all look the same, so the model copies the pattern without learning the range.

Check yourself

The format keeps coming out wrong with zero-shot prompting. What next?

When should you skip chain-of-thought prompting?

You show three examples that are all positive reviews. What risk does that create?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page