Zero-shot
Direct instruction, no examples. Baseline for comparison. Best token efficiency when it works. Strong on simple classification and extraction; weak on multi-step reasoning.
# Zero-shot: Direct instruction
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": "Classify this review as positive, negative, or neutral: 'The product arrived late but works great.'"
}]
)
Few-shot
Provide two to five examples to establish the pattern. About +12% over zero-shot on average. Diminishing returns beyond five. Best for format-sensitive output and domain jargon; costs context window.
# Few-shot: Provide examples to establish pattern
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": """Classify reviews as positive, negative, or neutral.
Review: "Absolutely love it, best purchase ever!"
Classification: positive
Review: "Broken on arrival, total waste of money."
Classification: negative
Review: "It's okay, nothing special but works fine."
Classification: neutral
Review: "The product arrived late but works great."
Classification:"""
}]
)
Chain-of-Thought
Request step-by-step reasoning. Most effective on math and logic. Adds 20–80% latency; can yield 15–20% accuracy gains on complex tasks. Weak on factual recall.
# Chain-of-Thought: Request step-by-step reasoning
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": """Solve this step by step:
A store has 45 apples. They sell 1/3 of them in the morning
and 1/2 of the remaining in the afternoon.
How many apples are left?
Let's think through this step by step:"""
}]
)
Self-consistency
Run the same prompt multiple times with temperature > 0, then vote on the most common answer. High cost but highest accuracy for critical decisions.
import asyncio
from collections import Counter
async def self_consistency(prompt: str, n: int = 5) -> str:
"""Run prompt n times, return majority answer."""
responses = await asyncio.gather(*[
client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
temperature=0.7 # Need variation
)
for _ in range(n)
])
# Extract final answers and vote
answers = [extract_answer(r.choices[0].message.content)
for r in responses]
most_common = Counter(answers).most_common(1)[0][0]
return most_common