CampusFlow
Data SciencePrompt Engineering

Prompt Engineering

Designing effective prompts to get the best output from LLMs.

What is Prompt Engineering?

Prompt engineering is the practice of carefully crafting input text to guide large language models (LLMs) toward desired outputs. Well-structured prompts dramatically improve response quality, reduce hallucinations, and enable complex reasoning chains.

# Bad prompt — vague and ambiguous
prompt = "Tell me about AI"
# Likely result: a generic paragraph with no depth

# Good prompt — specific, structured, with constraints
prompt = """
You are an expert AI researcher. Explain the following concepts
to a university CS student in 3-4 sentences each:
1. Transformer architecture
2. Self-attention mechanism
3. Fine-tuning vs RAG

Use analogies where helpful and avoid jargon without explanation.
"""

# Result: focused, educational, and well-structured response

Core Prompt Techniques

Zero-Shot Prompting

Ask directly without examples. Works best for simple, well-known tasks. E.g., 'Translate to French: Hello.'

Few-Shot Prompting

Provide 2-5 examples before asking the target question. Helps with formatting, style, and complex tasks.

Chain-of-Thought (CoT)

Prompt the model to reason step-by-step before answering. Significantly improves math, logic, and multi-step problems.

Tree-of-Thought (ToT)

Explore multiple reasoning paths simultaneously, then evaluate and choose the best one. Useful for planning and strategy.

Role Prompting

Assign a persona (e.g., 'You are a senior software engineer') to set context, tone, and expertise level for the output.

Structured Output

Request specific formats: JSON, markdown, bullet points, or tables. Use explicit schemas to get parseable results.

Advanced Strategies

Combine techniques for complex real-world tasks. Chain prompts together, use temperature/ top-p controls, and iterate based on model feedback.

# Multi-step prompt chain
def research_topic(topic: str) -> dict:
    """Chain multiple prompts to generate a structured report."""

    # Step 1: Outline
    outline_prompt = f"""
    Create a detailed outline for a 500-word article on '{topic}'.
    Include: introduction, 3 key concepts, applications, and conclusion.
    Output as JSON with keys: sections, subsections.
    """

    # Step 2: Expand each section using the outline
    expand_prompt = f"""
    Using this outline: {outline_prompt}
    Expand each section with specific examples, data points,
    and practical code snippets where applicable.
    Keep each section under 150 words.
    """

    # Step 3: Review & refine
    review_prompt = f"""
    Review the following article for accuracy, clarity, and completeness.
    Identify any claims that need citations and suggest improvements.
    ---
    Article: {expand_prompt}
    """

    return {"outline": outline_prompt, "draft": expand_prompt, "review": review_prompt}

# Parameters that affect output
config = {
    "temperature": 0.3,    # 0 = deterministic, 1 = creative
    "top_p": 0.9,          # nucleus sampling threshold
    "max_tokens": 2048,    # response length limit
    "presence_penalty": 0, # discourage topic repetition
    "frequency_penalty": 0 # discourage word repetition
}

System Prompts & RAG

System prompts set the model behavior, constraints, and context. In RAG (Retrieval-Augmented Generation), prompts incorporate retrieved documents to ground the model in factual information.

# System prompt pattern
SYSTEM_PROMPT = """
You are a helpful coding assistant for university students.
Rules:
- Always explain concepts before showing code
- Use Python with simple examples
- If unsure, say "I don't know" instead of guessing
- Keep responses under 200 words
"""

# RAG prompt pattern
RAG_PROMPT = """
Context documents:
{document_1}
{document_2}

Question: {user_question}

Answer based only on the provided context.
If the context does not contain the answer, say so.
Cite the relevant context in your answer.
"""

# Few-shot example embedded in prompt
FEW_SHOT = """
Classify the sentiment of each review:

Review: "This product exceeded my expectations!"
Sentiment: Positive

Review: "Terrible quality, broke in a day."
Sentiment: Negative

Review: "It's okay for the price."
Sentiment: Neutral

Review: "{input_text}"
Sentiment:"""

Interview Questions

Q: What is chain-of-thought prompting and when should you use it?

Chain-of-thought (CoT) prompts the model to reason step-by-step before answering. Use it for arithmetic, logical reasoning, multi-step problems, and any task requiring intermediate steps. It reduces errors by making the reasoning process explicit.

Q: How does temperature affect LLM output?

Temperature controls randomness: 0 makes output deterministic (always picks highest probability token), values closer to 1 increase creativity and variety. Use low temperature (0.1–0.3) for factual tasks, high temperature (0.7–0.9) for creative writing.

Q: What is the difference between a system prompt and a user prompt?

A system prompt sets the model's behavior, persona, and constraints (e.g., 'You are a helpful tutor'). It persists across a conversation. User prompts are the actual queries or instructions and can vary with each turn. System prompts have stronger steering influence.

Q: How does few-shot prompting differ from fine-tuning?

Few-shot prompting provides examples in the prompt at inference time — no training needed. Fine-tuning updates the model's weights on labeled data, permanently altering its behavior. Few-shot is quick and flexible; fine-tuning is more thorough but expensive.

Q: What is RAG and how does it improve prompt engineering?

RAG (Retrieval-Augmented Generation) retrieves relevant documents from a knowledge base and injects them into the prompt as context. This grounds the model in factual information, reduces hallucinations, and enables question answering over private data without retraining.