Skip to content

Home / Glossary / Few-Shot Prompting

Definition

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.

javascript
// 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?+
Two to five examples are typically sufficient. One example might not establish a clear pattern. More than five rarely improves results and wastes context window space. Choose diverse examples that cover the variations you care about.
What is the difference between few-shot and zero-shot prompting?+
Zero-shot prompting provides no examples—just instructions. Few-shot provides examples that demonstrate the desired pattern. Zero-shot works well when the task is well-defined and the model already knows the pattern. Few-shot is better when you need a specific format or style that the model might not default to.
Does few-shot prompting work for complex coding tasks?+
Yes, but the examples need to be well-chosen. For complex tasks like refactoring or architecture decisions, show examples of the thought process and output format you want, not just simple code snippets. Quality of examples matters more than quantity.

Related terms

コード向けプロンプトエンジニアリングSystem PromptChain-of-ThoughtZero-Shot Prompting

Master Claude Code in days, not months

37 hands-on lessons from beginner to CI/CD automation. Module 1 is free.

START FREE →
← ALL TERMS