Home / Glossary / Few-Shot Prompting
Few-Shot Prompting
Few-shot prompting is a technique where you include a small number of example input-output pairs in your prompt to demonstrate the pattern you want the AI to follow. By showing the model 2-5 examples of the desired behavior, it learns the format, style, and logic you expect—without any model training or fine-tuning. This is one of the most effective techniques for getting consistent, formatted output from LLMs.
How few-shot prompting works
When you provide examples in your prompt, the model uses in-context learning to identify the pattern and apply it to new inputs. The model is not being trained—its weights do not change. Instead, the examples act as demonstrations that prime the model to produce output in the same format and style. For coding tasks, this means you can show the model one or two examples of how you want a function structured, and it will follow that pattern for subsequent generations.
Few-shot prompting for code
Few-shot is particularly powerful for enforcing coding conventions that are hard to describe in words. Instead of explaining your team's error handling pattern in abstract terms, show two examples of functions that follow the pattern. The model picks up on naming conventions, error structures, logging formats, and architectural patterns from examples much more reliably than from descriptions.
// Few-shot prompt for consistent API route handlers
// Prompt:
// "Generate a PATCH /users/:id endpoint following this pattern:
//
// Example 1:
// app.get('/products/:id', auth, async (req, res) => {
// try {
// const product = await Product.findById(req.params.id);
// if (!product) return res.status(404).json({ error: 'Product not found' });
// res.json({ data: product });
// } catch (err) {
// logger.error('GET /products/:id', err);
// res.status(500).json({ error: 'Internal server error' });
// }
// });
//
// Example 2:
// app.delete('/products/:id', auth, async (req, res) => {
// try {
// const product = await Product.findByIdAndDelete(req.params.id);
// if (!product) return res.status(404).json({ error: 'Product not found' });
// res.status(204).send();
// } catch (err) {
// logger.error('DELETE /products/:id', err);
// res.status(500).json({ error: 'Internal server error' });
// }
// });
//
// Now generate PATCH /users/:id:"In Claude Code, the most effective form of few-shot prompting is referencing existing files: "Follow the pattern in routes/products.ts." The agent reads the file and applies the same patterns, which is even better than pasting examples into your prompt.
How many examples should I include?+
What is the difference between few-shot and zero-shot prompting?+
Does few-shot prompting work for complex coding tasks?+
Master Claude Code in days, not months
37 hands-on lessons from beginner to CI/CD automation. Module 1 is free.
START FREE →