Skip to content
Last updated: 14m ago

AI Prompt Engineering Guide

The difference between a useful AI response and a generic one is almost always the prompt. This guide covers 8 techniques that work across GPT, Claude, Gemini, and open-source models - with before/after examples for each.

Sorted from easiest to most advanced. Start with the beginner techniques - they deliver the biggest improvement for the least effort.

Techniques at a Glance

TechniqueLevel
Be SpecificBeginner
Few-Shot ExamplesBeginner
Chain-of-ThoughtIntermediate
Role AssignmentBeginner
Structured OutputIntermediate
Constraint SettingIntermediate
Self-CritiqueAdvanced
DecompositionAdvanced

Be Specific

Beginner

Replace vague instructions with precise requirements. Specify format, length, audience, and constraints upfront.

Instead of this

Write about machine learning.

Try this

Write a 200-word explanation of gradient descent for a software engineer who has never studied ML. Use a hiking analogy. No math notation.

Why it works

Models generate text by predicting likely continuations. A specific prompt narrows the prediction space, so the model does not have to guess what you want.

Few-Shot Examples

Beginner

Include 2-5 examples of input/output pairs before your actual request. The model learns the pattern from your examples.

Instead of this

Classify this review as positive or negative: "The battery lasts forever but the screen is dim."

Try this

Classify each review. Review: "Amazing sound quality, worth every penny." Label: positive Review: "Broke after two weeks, terrible build." Label: negative Review: "The battery lasts forever but the screen is dim." Label:

Why it works

Few-shot examples set a clear pattern the model can follow. This is more reliable than describing the task because you are showing rather than telling.

Chain-of-Thought

Intermediate

Ask the model to think step-by-step before giving its final answer. This forces it to show intermediate reasoning rather than jumping to conclusions.

Instead of this

A store has 45 apples. They sell 3/5 of them, then receive 20 more. How many apples do they have?

Try this

A store has 45 apples. They sell 3/5 of them, then receive 20 more. How many apples do they have? Think through this step-by-step before giving your final answer.

Why it works

Token-by-token generation means the model can use earlier tokens as "working memory." Without chain-of-thought, it must compute the answer in a single forward pass, which fails for multi-step problems.

Role Assignment

Beginner

Tell the model to act as a specific expert. This primes it to use domain-appropriate vocabulary, reasoning patterns, and depth.

Instead of this

Is this SQL query efficient?

Try this

You are a senior database engineer with 15 years of PostgreSQL experience. Review this SQL query for performance issues. Focus on indexing, joins, and query plan optimization.

Why it works

Role assignment activates different knowledge clusters in the model. A "database engineer" persona draws on training data from DB experts, producing more specialized and accurate responses.

Structured Output

Intermediate

Request output in a specific format (JSON, markdown table, numbered list). Include the exact schema or template you want.

Instead of this

Extract the key info from this job posting.

Try this

Extract information from this job posting and return it as JSON with these exact fields: { "title": string, "company": string, "salary_range": string | null, "remote": boolean, "required_skills": string[] }

Why it works

Models that score well on IFEval (instruction following) reliably produce structured output. Providing the exact schema removes ambiguity about field names, types, and nesting.

Constraint Setting

Intermediate

Explicitly state what the model should NOT do. Constraints are as important as instructions for controlling output quality.

Instead of this

Explain quantum computing.

Try this

Explain quantum computing for a curious 14-year-old. Constraints: - No equations or mathematical notation - No analogies involving cats (Schrodinger has been overdone) - Under 300 words - End with one question that makes them want to learn more

Why it works

Without constraints, models default to their most common training patterns (which include overused analogies and verbose explanations). Constraints force creative alternatives.

Self-Critique

Advanced

Ask the model to generate a response, then review its own output for errors or improvements. Two passes produce better results than one.

Instead of this

Write a function to validate email addresses.

Try this

Write a function to validate email addresses in Python. After writing it, review your own code for: 1. Edge cases it misses 2. RFC 5322 compliance issues 3. Performance with large inputs Then provide an improved version addressing any issues found.

Why it works

Generation and evaluation use different reasoning pathways. Models are often better at spotting errors in existing text than avoiding them during generation. The second pass catches mistakes the first pass introduced.

Decomposition

Advanced

Break a large task into smaller subtasks. Handle each subtask separately, then combine results. Works better than asking for everything at once.

Instead of this

Analyze this 50-page contract and summarize all risks, obligations, and deadlines.

Try this

I will send you a contract in sections. For each section: 1. List any obligations for our company 2. Flag any risks or unusual clauses 3. Extract any deadlines or dates After all sections, I will ask you to compile a final summary.

Why it works

Context windows have attention limits. Even models with 128K+ context perform worse on information in the middle of long inputs. Decomposition ensures each subtask gets focused attention.

Model-Specific Tips

GPT Models (OpenAI)

  • - System prompts are strongly followed
  • - JSON mode produces reliable structured output
  • - Function calling works better than asking for JSON directly
  • - Temperature 0 for deterministic tasks, 0.7+ for creative

Claude Models (Anthropic)

  • - Excels with detailed, nuanced system prompts
  • - XML tags in prompts help structure complex instructions
  • - Responds well to "think step by step" for reasoning
  • - Extended thinking mode for complex analysis tasks

Gemini Models (Google)

  • - Strong with multimodal prompts (text + images)
  • - Grounding with Google Search for factual accuracy
  • - Large context windows handle long documents well
  • - Good at following complex multi-part instructions

Open-Source Models

  • - System prompt support varies by model and serving framework
  • - Be more explicit with formatting instructions
  • - Smaller models benefit more from few-shot examples
  • - Check the model card for recommended prompt templates
Frequently Asked Questions

The core techniques (specificity, few-shot, chain-of-thought) work across all major models. But each model has quirks. Claude responds well to detailed system prompts. GPT models handle structured output reliably. Open-source models may need more explicit formatting instructions. Always test your prompts on the specific model you are deploying.

More relevant than ever. As models become more capable, the gap between a mediocre prompt and an optimized one grows wider. A well-crafted prompt can get GPT-5.4 or Claude Opus to produce work that would require a much more expensive model with a generic prompt. Prompt engineering is now a recognized skill in job listings.

For most tasks, 2-5 examples hit the sweet spot. One example is often not enough to establish a pattern. More than 5 rarely improves results and wastes context window. For complex classification with many categories, you may need one example per category. Always include edge cases in your examples.

Use system prompts for persistent instructions (role, constraints, output format) and user prompts for the specific task. System prompts are given higher priority by most models and persist across conversation turns. Not all API providers support system prompts - check your model documentation.

Being vague. "Write something good about X" will always produce generic output. The most impactful improvement is being specific about format, audience, length, constraints, and success criteria. Think of it this way: if two reasonable people could interpret your prompt differently, it needs to be more specific.

Prompt Engineering Guide - AI Techniques (2026) | LM Market Cap