The Two Most Research-Validated Techniques
Of all the prompting techniques that have emerged since 2020, two stand out for the volume of research evidence behind them:
- Few-shot prompting: Providing examples of desired input-output pairs before your actual request
- Chain-of-thought (CoT) prompting: Asking the model to reason step-by-step before producing a final answer
---
Few-Shot Prompting: The Science
Few-shot prompting exploits the model's in-context learning capability. Large language models can infer the pattern of a task from examples alone, without any parameter updates. When you show the model examples, you are effectively teaching it the task in the prompt.
Zero-Shot vs One-Shot vs Few-Shot
The few-shot version makes it clear what mixed sentiment means and how to handle it β the model can infer from the NEUTRAL example that mixed reviews do not default to NEGATIVE.
Designing Effective Examples
Principle 1: Cover edge cases, not just the easy cases. If your task has ambiguous or boundary cases, include examples that demonstrate how to handle them. Easy cases the model handles fine without examples. Edge cases are where few-shot prompting earns its value.
Principle 2: Keep examples structurally consistent. Every example should follow the exact same format. Inconsistency in example structure confuses the pattern inference.
Principle 3: Use real examples, not invented ones. When possible, use actual inputs and their correct outputs. Invented examples often contain subtle inconsistencies that the model learns from unintentionally.
Principle 4: 3-5 examples is usually optimal. More examples cost more tokens. Beyond 5-7 examples, the marginal quality improvement drops significantly. For classification tasks, aim for at least one example per class.
Few-Shot for Code Generation
``` Convert these natural language specifications to TypeScript function signatures with JSDoc comments.
EXAMPLE 1:
Spec: "Takes a user ID and returns all their published posts sorted by date"
Output:
/*
Retrieves all published posts for a user, sorted by publication date descending.
@param userId - The UUID of the user
@returns Promise resolving to an array of Post objects, newest first
@throws DatabaseError if the query fails
/
async function getUserPublishedPosts(userId: string): Promise
EXAMPLE 2: Spec: "Takes an email and password, validates them, creates a user account, and returns the new user or an error"
Get advanced blueprint resources
Enter your email to receive technical blueprints, checklists, and visual configurations accompanying this chapter.
No spam. Unsubscribe any time.
Output:
/*
Creates a new user account with the provided credentials.
Validates email format and password strength before creation.
@param email - Valid email address for the new account
@param password - Password (minimum 8 characters, at least one number)
@returns Promise resolving to the created User or an AuthError
*/
async function createUserAccount(email: string, password: string): Promise
Now convert: Spec: "Takes a product ID and quantity, checks inventory, reserves the stock if available, and returns a reservation ID or throws if insufficient stock"
"Think through this step by step before giving your final answer." "Reason through this carefully before responding." "Work through this problem, showing each step." Q: A store sells apples for $1.20 each. If I buy 7 apples and pay with a $10 bill, how much change do I get? A: Let me work through this step by step. Cost per apple: $1.20 Number of apples: 7 Total cost: $1.20 x 7 = $8.40 Payment: $10.00 Change: $10.00 - $8.40 = $1.60 Answer: $1.60 change.Q: [your actual question]
Debug this function by tracing its execution step by step for the given input. Identify exactly where the logic goes wrong.Function: function findSecondLargest(arr) { let largest = -Infinity; let second = -Infinity; for (let i = 0; i < arr.length; i++) { if (arr[i] > largest) { second = largest; largest = arr[i]; } else if (arr[i] > second) { second = arr[i]; } } return second; }
Input: [5, 5, 3] Expected output: 3 Actual output: -Infinity
Trace the execution step by step, then explain the bug and fix it.
EXAMPLE: Input: [task input] Reasoning: [step-by-step reasoning] Output: [final answer]EXAMPLE: Input: [task input] Reasoning: [step-by-step reasoning] Output: [final answer]
Now solve: Input: [your actual input] Reasoning: ```
By showing examples that include the reasoning chain, you teach the model both what kind of reasoning to apply and what format to produce. This is called "few-shot chain-of-thought" and is the most powerful baseline for complex, high-stakes tasks.