Prompt Engineering

Advanced Patterns & Best Practices for Better LLM Outputs

Core Patterns

PatternWhen to UseWhat to Do
Zero-shotSimple, well-defined tasksJust ask directly.
Few-shotStrict output format neededShow 2–5 examples before the real query.
Chain-of-ThoughtReasoning, math, multi-stepAsk to "think step by step" first.
ReActAgent/tool-using flowsInterleave Thought → Action → Observation.
ReflectionEditing/improving outputAsk model to critique, then revise.

Reusable Template

# Role You are a [ROLE] helping [AUDIENCE] with [TASK]. # Context [background, docs, constraints] # Task [exactly what you want] # Output format [structure: sections, JSON keys, etc.] # Rules - Do not [avoid this] - If unsure, say "I don't know"

Anti-Patterns (Stop Doing These)

Quick Wins

Token Cost Formula

For self-consistency sampling (n completions, average m tokens output):

cost = n × (input_tokens × price_in + m × price_out)

Example: 100 input tokens, 10 samples × 50 output tokens with Claude Sonnet = 100×$3 + 500×$15 = $7.80 total.