What is Prompt Engineering?

Prompt engineering is the practice of designing and optimizing inputs (prompts) to get the best possible outputs from Large Language Models (LLMs). It's both an art and a science - combining understanding of how LLMs work with creative problem-solving.

A well-crafted prompt can be the difference between a generic, unhelpful response and a precise, valuable answer. As LLMs become more integrated into applications, prompt engineering has emerged as a critical skill for developers, data scientists, and anyone working with AI.

Core Prompting Techniques

1. Zero-Shot Prompting

Ask the model directly without examples. Works well for simple, well-defined tasks.

# Zero-shot prompt
prompt = """Classify the sentiment of this review as positive, negative, or neutral.

Review: "The food was delicious but the service was slow."

Sentiment:"""

# Output: neutral (or mixed)

2. Few-Shot Prompting

Provide examples to guide the model's behavior. Essential for consistent formatting and complex tasks.

# Few-shot prompt
prompt = """Classify sentiment as positive, negative, or neutral.

Review: "Amazing product, exceeded expectations!"
Sentiment: positive

Review: "Terrible experience, never buying again."
Sentiment: negative

Review: "It's okay, nothing special."
Sentiment: neutral

Review: "The food was delicious but the service was slow."
Sentiment:"""

# Output: neutral (follows the pattern)

3. Chain-of-Thought (CoT)

Ask the model to show its reasoning step by step. Dramatically improves performance on complex problems.

# Without CoT
prompt = "If a store has 15 apples and sells 7, then receives 12 more, how many apples are there?"
# Model might give wrong answer

# With CoT
prompt = """If a store has 15 apples and sells 7, then receives 12 more, how many apples are there?

Let's think step by step:
1. Starting apples: 15
2. After selling 7: 15 - 7 = 8
3. After receiving 12: 8 + 12 = 20

Answer: 20 apples"""

4. System Prompts

Set the model's persona, role, and behavior at the beginning of the conversation.

# System prompt for a customer service bot
system_prompt = """You are a helpful customer service representative for TechCorp.

Guidelines:
- Be friendly and professional
- If you don't know something, say so
- Never share confidential information
- Offer to escalate to a human if needed
- Always end with asking if there's anything else

Product knowledge: [detailed product info here]"""

5. Role Prompting

Assign the model a specific role or persona to get domain-specific responses.

# Role prompting examples
prompts = [
    "As an experienced Python developer, review this code...",
    "You are a senior data scientist. Explain this dataset...",
    "Acting as a legal expert, identify issues in this contract...",
    "As a patient teacher explaining to a beginner..."
]

Advanced Techniques

Self-Consistency

Generate multiple responses and use the most common answer.

# Generate 5 responses, take majority vote
responses = []
for _ in range(5):
    response = llm.invoke(prompt)
    responses.append(response)

# Most common answer is likely correct
final_answer = most_common(responses)

ReAct (Reasoning + Acting)

Combine reasoning with action-taking for agent-like behavior.

prompt = """Answer the question using this format:

Thought: I need to figure out...
Action: Search[query] or Calculate[expression]
Observation: [result of action]
... (repeat as needed)
Thought: I now know the answer
Final Answer: [answer]

Question: What is the population of the capital of France?

Thought: I need to find the capital of France first.
Action: Search[capital of France]
Observation: Paris is the capital of France.
Thought: Now I need the population of Paris.
Action: Search[population of Paris]
Observation: Paris has approximately 2.1 million people.
Thought: I now know the answer.
Final Answer: The capital of France is Paris, with a population of about 2.1 million."""

Tree of Thoughts (ToT)

Explore multiple reasoning paths before deciding on the best answer.

Least-to-Most Prompting

Break complex problems into simpler sub-problems, solve them in order.

prompt = """To solve this problem, let's break it down:

Problem: Build a user authentication system

Sub-problems:
1. First, how do we securely store passwords?
2. Then, how do we implement login verification?
3. Finally, how do we manage sessions?

Let's solve each one:
1. Password storage: [solve]
2. Login verification: [solve using #1]
3. Session management: [solve using #1 and #2]"""

Prompt Design Principles

Be Specific

Bad: "Write about dogs"

Good: "Write a 200-word paragraph about the history of Golden Retrievers as family pets"

Provide Context

Bad: "Summarize this"

Good: "Summarize this research paper for a non-technical audience in 3 bullet points"

Specify Format

Bad: "List some ideas"

Good: "Provide 5 ideas in a numbered list, each with a title and one-sentence description"

Set Constraints

Bad: "Explain quantum computing"

Good: "Explain quantum computing in simple terms, using no jargon, in under 100 words"

Define Success

Bad: "Make this code better"

Good: "Refactor this code to improve readability and reduce time complexity from O(n²) to O(n)"

Use Delimiters

Use clear separators like ```, ---, or XML tags to distinguish instructions from content

Structured Output Prompts

Getting consistent, parseable outputs from LLMs:

JSON Output

prompt = """Extract information from this text and return as JSON.

Text: "John Smith is a 35-year-old software engineer from San Francisco."

Return ONLY valid JSON in this format:
{
    "name": "...",
    "age": ...,
    "occupation": "...",
    "location": "..."
}"""

Using XML Tags

prompt = """Analyze the following code and provide feedback.


def calculate(x, y):
    return x + y


Provide your response in this format:

    Brief overview
    List of problems
    Improvements
"""

Common Mistakes to Avoid

  • Being too vague: Ambiguous prompts get inconsistent results
  • Overloading the prompt: Too many instructions can confuse the model
  • Ignoring model limits: Context windows have limits; be concise
  • No example outputs: For specific formats, show what you want
  • Assuming knowledge: Models may not have domain-specific knowledge
  • Not iterating: First prompt is rarely the best; refine and test
  • Negative instructions: "Don't do X" is less effective than "Do Y instead"

Prompt Templates with LangChain

from langchain.prompts import ChatPromptTemplate, FewShotPromptTemplate

# Simple template
template = ChatPromptTemplate.from_messages([
    ("system", "You are a {role} assistant."),
    ("human", "{question}")
])

# Few-shot template
examples = [
    {"input": "happy", "output": "sad"},
    {"input": "tall", "output": "short"},
]

few_shot = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Give the antonym of each word.",
    suffix="Input: {input}\nOutput:",
    input_variables=["input"]
)

# Use in a chain
from langchain_openai import ChatOpenAI

chain = template | ChatOpenAI()
result = chain.invoke({
    "role": "coding",
    "question": "How do I reverse a list in Python?"
})

Prompt Engineering for Different Tasks

Code Generation

Specify language, style, include error handling, add docstrings.

Summarization

Define length, audience, key points to include, format.

Classification

List all categories, provide examples, handle edge cases.

Data Extraction

Specify exact fields, format (JSON/XML), handling of missing data.

Translation

Source/target language, formality level, preserve formatting.

Creative Writing

Tone, style, length, target audience, inspiration sources.

Master Prompt Engineering

Our Agentic AI program includes extensive training on prompt engineering - from basics to advanced techniques for production AI systems.

Explore Agentic AI Program

Related Articles