When to Use This Guide

Use this guide when you want to get better, more reliable outputs from any large language model. Whether you are writing prompts for ChatGPT, Claude, Gemini, Llama, or any other model, the principles here apply universally. This is the foundation that makes every other prompt in this library work.

Prompt Engineering: How to Write Effective Prompts

Prompt engineering is the discipline of crafting inputs to large language models (LLMs) that produce reliable, high-quality outputs. It is not guesswork. It is a repeatable skill built on clear principles.

This page covers the core techniques, from basic structure to advanced strategies, with practical examples you can use immediately.


The Anatomy of a Great Prompt

Every effective prompt has five structural elements. Missing any of them degrades output quality.

ElementWhat It DoesExample
RoleSets the persona and expertise level“You are a senior backend engineer”
ContextProvides background the model needs“The codebase uses FastAPI with SQLAlchemy”
TaskStates exactly what you want done“Review this function for security vulnerabilities”
FormatSpecifies the output structure“Return a numbered list with severity ratings”
ConstraintsSets boundaries and rules“Do not suggest changes to the database schema”
The 5-element checklist. Before sending any prompt, ask yourself: Did I specify the role, context, task, format, and constraints? If any are missing, add them. This single habit eliminates most prompt failures.

Core Techniques

### Step 1: Be Specific, Not Vague The number one cause of bad AI output is vague instructions. Compare these two prompts: **Bad:** ``` Write about marketing. ``` **Good:** ``` Write a 500-word blog post explaining three email marketing strategies for B2B SaaS companies with under 1,000 subscribers. Include one real-world example for each strategy. Use a professional but conversational tone. ``` The good prompt specifies: length, topic, audience, quantity, evidence requirements, and tone. The model has no room to drift. **Rule of thumb:** If a human intern would ask clarifying questions after reading your prompt, the prompt is too vague. ### Step 2: Use Structured Formatting LLMs respond well to structured input. Use headings, numbered lists, and labeled sections to organize your prompt. ``` ROLE: Senior technical writer CONTEXT: - Product: Cloud-based project management tool - Audience: Non-technical small business owners - Existing docs: None TASK: Create a "Getting Started" guide with these sections: 1. Account creation (3 steps max) 2. Creating your first project 3. Inviting team members FORMAT: - Use H2 for section headers - Include a tip callout after each section - Keep sentences under 20 words CONSTRAINTS: - Do not mention API access or developer features - Do not reference competitor products ``` ### Step 3: Provide Examples (Few-Shot Prompting) Few-shot prompting means giving the model examples of the input-output pattern you want. This is one of the most powerful techniques available. ``` Classify the following customer messages by intent. Example 1: Input: "I can't log in to my account" Output: {"intent": "authentication", "urgency": "high", "sentiment": "frustrated"} Example 2: Input: "Do you have a dark mode option?" Output: {"intent": "feature_inquiry", "urgency": "low", "sentiment": "neutral"} Example 3: Input: "Your product saved me 3 hours this week!" Output: {"intent": "positive_feedback", "urgency": "none", "sentiment": "positive"} Now classify this message: Input: "The export button doesn't work on Firefox" Output: ``` The model learns the exact JSON structure, the field names, and the classification taxonomy from your examples. No lengthy explanation needed. ### Step 4: Chain-of-Thought Reasoning For complex tasks that require reasoning, tell the model to think step by step. This dramatically improves accuracy on math, logic, analysis, and multi-step problems. ``` A store sells notebooks for $4 each. A customer buys 3 notebooks and pays with a $20 bill. The store has no coins, only $1 and $5 bills. What is the correct change, and what bills should the cashier give? Think through this step by step before answering. ``` **Why it works:** Without chain-of-thought, the model jumps to an answer and sometimes gets it wrong. With it, the model "shows its work" and self-corrects along the way. ### Step 5: Assign a Role with Depth A shallow role ("You are a helpful assistant") adds nothing. A deep role with specific expertise, constraints, and behavioral rules transforms the output. ``` You are a database performance consultant with 15 years of experience in PostgreSQL optimization. You specialize in query tuning for high-traffic SaaS applications (10,000+ queries per second). When reviewing queries, you: - Always check for missing indexes first - Prefer covering indexes over table scans - Flag N+1 query patterns immediately - Recommend EXPLAIN ANALYZE output when you need more data - Never suggest denormalization without quantifying the trade-off The user will paste SQL queries. Analyze each one for performance issues. ```

System vs User Messages

OpenAI models use a three-role message structure:

RolePurposeWhen to Use
systemSets persistent behavior, personality, rulesOnce at the start. Put role, constraints, format rules here
userThe human’s inputEvery turn. Contains the actual task or question
assistantThe model’s responseIncluded in history for context continuity
{
  "model": "gpt-4o",
  "messages": [
    {"role": "system", "content": "You are a concise code reviewer. Reply only with the issue and fix. No explanations."},
    {"role": "user", "content": "Review this: def get_user(id): return db.query(f'SELECT * FROM users WHERE id = {id}')"}
  ]
}

The system message persists across the entire conversation. Put all your rules, persona, and format instructions there.

Claude uses a system parameter (separate from messages) plus user and assistant turns:

{
  "model": "claude-sonnet-4-20250514",
  "system": "You are a concise code reviewer. Reply only with the issue and fix. No explanations.",
  "messages": [
    {"role": "user", "content": "Review this: def get_user(id): return db.query(f'SELECT * FROM users WHERE id = {id}')"}
  ]
}

Claude’s system prompt is strongly followed. It is the best place for behavioral rules, output format, and constraints.

Gemini uses system_instruction at the model level:

{
  "model": "gemini-2.0-flash",
  "system_instruction": "You are a concise code reviewer. Reply only with the issue and fix.",
  "contents": [
    {"role": "user", "parts": [{"text": "Review this function..."}]}
  ]
}

Gemini also supports responseMimeType: "application/json" with a responseSchema for enforced structured output — no prompt tricks needed.

Open-source models (Llama, Mistral, etc.) use chat templates. The system prompt goes in the template header:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

You are a concise code reviewer. Reply only with the issue and fix.<|eot_id|>

<|start_header_id|>user<|end_header_id|>

Review this function...<|eot_id|>

<|start_header_id|>assistant<|end_header_id|>

System prompt adherence varies by model. Llama 3.3 70B follows system prompts reliably. Smaller models may need reinforcement in the user message.


Temperature and Sampling Parameters

Temperature controls randomness. It is the single most misunderstood parameter in prompt engineering.

TemperatureBehaviorBest For
0.0Deterministic. Same input produces same output.Code generation, factual Q&A, classification, data extraction
0.3 - 0.5Slightly varied. Minor wording changes between runs.Technical writing, summarization, structured analysis
0.7 - 0.8Creative variation. Different approaches each run.Marketing copy, brainstorming, creative writing
1.0+High randomness. Unpredictable, sometimes incoherent.Experimental ideation only. Rarely useful in production.
Default recommendation: Use temperature 0 for any task where you want consistent, reproducible results. Use 0.7 for creative tasks. There is rarely a reason to go above 1.0.

Other important parameters:

ParameterWhat It ControlsRecommendation
top_pNucleus sampling — limits token pool to top P% probability massKeep at 1.0 unless you know what you are doing
max_tokensMaximum output lengthSet explicitly to avoid truncation. 4096 for long outputs
stopStop sequences — model stops generating when it hits theseUseful for structured output: stop at \n\n or ---
frequency_penaltyPenalizes repeated tokens0.0 default. Increase to 0.3-0.5 for varied creative writing

Advanced Techniques

Technique: Prompt Chaining

Break complex tasks into a sequence of simpler prompts, where each prompt’s output feeds into the next.

Example — Research Report Pipeline:

Prompt 1 (Research): "Search for the top 5 competitors in the
AI writing assistant market. For each, list: name, pricing,
key features, and target audience."

Prompt 2 (Analysis): "Given this competitor data: [output from Prompt 1].
Identify the three biggest gaps in the market that no competitor
is addressing well."

Prompt 3 (Strategy): "Given these market gaps: [output from Prompt 2].
Write a positioning strategy for a new AI writing tool targeting
[specific audience]. Include messaging pillars and differentiation."

Why it works: Each prompt is simple and focused. The model does one thing well instead of juggling everything at once. Error rates drop dramatically.

Technique: Self-Consistency Verification

Ask the model to generate multiple answers and then evaluate which one is best. This reduces hallucination on complex reasoning tasks.

Answer the following question three different ways, using different
reasoning approaches each time. Then compare your three answers
and select the one you are most confident in. Explain why.

Question: Should a 50-person startup adopt microservices or
a modular monolith?
Technique: Constraint Stacking

Layer constraints to progressively narrow the output space. Each constraint eliminates a category of bad output.

Write a product description for wireless headphones.

Constraints:
1. Exactly 3 paragraphs
2. First paragraph: the problem (max 2 sentences)
3. Second paragraph: the solution with 3 specific features
4. Third paragraph: social proof + CTA
5. Reading level: 8th grade (Flesch-Kincaid)
6. No superlatives (no "best", "amazing", "incredible")
7. Include one specific number (battery life, weight, or price)

Each constraint eliminates a category of generic output. The result is specific and structured.

Technique: Meta-Prompting

Use the AI to improve your prompts. This is recursive prompt engineering.

I want to write a prompt that generates SQL queries from natural
language questions. Here is my current prompt:

[paste your prompt]

Analyze this prompt for weaknesses. Then rewrite it to be more
robust, handling edge cases like ambiguous table names, aggregate
queries, and JOINs across 3+ tables.

This is especially useful when a prompt works 80% of the time but fails on edge cases. The model can often identify the failure patterns you missed.

Technique: Output Scaffolding

Give the model a partial output structure and ask it to fill in the blanks. This guarantees format compliance.

Fill in the sections marked [FILL] based on the codebase analysis:

## Architecture Overview
[FILL: 2-3 sentence summary of the system architecture]

## Tech Stack
| Layer | Technology | Version |
|-------|-----------|---------|
| Frontend | [FILL] | [FILL] |
| Backend | [FILL] | [FILL] |
| Database | [FILL] | [FILL] |

## Key Design Decisions
1. [FILL: Decision about data storage] — Rationale: [FILL]
2. [FILL: Decision about auth] — Rationale: [FILL]
3. [FILL: Decision about deployment] — Rationale: [FILL]

Common Mistakes

Avoid these patterns. They are the most frequent causes of poor output.

MistakeWhy It FailsFix
“Be creative”Too vague. The model has no constraints to work within.Specify the type of creativity: “Write 3 alternative headlines using metaphor”
Wall of text promptThe model loses focus in long, unstructured paragraphsUse sections, headers, numbered lists, and labeled blocks
No examplesThe model guesses your preferred formatAdd 2-3 examples of the exact output you want
Asking for perfection“Write the perfect email” creates anxiety in the outputAsk for a specific draft: “Write a 3-paragraph cold email following AIDA framework”
Contradictory instructions“Be concise but thorough” paralyzes the modelChoose one priority and be explicit: “Prioritize brevity. Max 200 words.”
Ignoring the system promptPutting everything in the user message wastes the system promptMove rules, persona, and format to system. Keep task in user message.
No output format specThe model picks a random format each timeAlways specify: JSON, markdown, table, numbered list, etc.
Assuming contextThe model does not remember previous sessionsInclude all relevant context in each prompt. Models are stateless between sessions.
The iteration mindset. Your first prompt will rarely be perfect. Write it, test it, identify where it fails, and refine. Most production prompts go through 5-10 iterations before they are reliable. That is normal and expected.

Quick Reference: Prompt Template

Use this template as a starting point for any new prompt:

ROLE:
[Who is the AI in this context? What expertise does it have?]

CONTEXT:
[What background does the model need? What has happened before?]

TASK:
[What exactly should the model do? Be specific.]

FORMAT:
[How should the output be structured? Give an example if possible.]

CONSTRAINTS:
[What should the model NOT do? What are the boundaries?]

EXAMPLES (optional):
[Input/output pairs showing the pattern you want]