Prompt Engineering

MEDIUM3 min readby AdminJune 20, 2026History
0.0|0 ratingsLog in to rate

Prompt Engineering is the practice of designing and optimizing input instructions to guide LLMs to produce accurate, high-quality, and structurally consistent r...

#ai-engineering#course#prompt

Definition

Prompt Engineering is the practice of designing and optimizing input instructions to guide LLMs to produce accurate, high-quality, and structurally consistent responses. Core techniques include:

  • System Prompts: High-priority instructions defining the model's persona, boundaries, and rules.
  • User Prompts: The specific query or request input by the user.
  • Few-Shot Prompting: Providing input-output examples inside the prompt to guide formatting.
  • Chain of Thought (CoT): Instructing the model to break down its reasoning step-by-step before outputting a final answer.

Why It Exists

Because LLMs are generalized text-predictors, they lack built-in context about your application's business rules. Prompt engineering acts as the runtime configuration layer, guiding the model's generation behavior without requiring costly retraining (fine-tuning).

How It Works

python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PROMPT ANATOMY
┌──────────────────────────────────────────────┐
│ SYSTEM PROMPT                                │ (Define boundaries/rules)
│ "You are a helpful coding assistant..."       │
├──────────────────────────────────────────────┤
│ FEW-SHOT EXAMPLES                            │ (Provide examples)
│ "Input: 2+2 | Output: Four"                  │
├──────────────────────────────────────────────┤
│ CHAIN OF THOUGHT INSTRUCTION                 │ (Force logical analysis)
│ "Let's think step-by-step before answering..."│
├──────────────────────────────────────────────┤
│ USER PROMPT                                  │ (Provide target query)
│ "Write a script that sorting numbers"        │
└──────────────────────────────────────────────┘

1. **System Directives**: The model processes the system prompt first, establishing formatting rules and guardrails.
2. **Contextual Grounding**: Few-shot examples define the target formatting pattern.
3. **Reasoning Space**: Chain-of-Thought prompts give the model token space to calculate intermediate steps, which improves reasoning accuracy.

Real-World Example

Example

In Cursor or Copilot, the editor injects a hidden system prompt specifying details like "You are an expert developer. Output only raw code. Use the selected language framework...", followed by files and your explicit user instruction.

Python Example

Example

Here is a system that dynamically composes a structured prompt payload using Few-shot examples and Chain-of-Thought instructions:

python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from typing import List, Dict

class PromptComposer:
    """Composes dynamic prompt templates for LLM client integration."""
    def __init__(self, system_instruction: str):
        self.system = system_instruction
        self.examples: List[Dict[str, str]] = []

    def add_few_shot_example(self, user_input: str, assistant_output: str):
        self.examples.append({"input": user_input, "output": assistant_output})

    def compose(self, query: str, force_cot: bool = True) -> str:
        prompt = f"System Instruction: {self.system}\n\n"
        
        if self.examples:
            prompt += "Examples of correct formats:\n"
            for ex in self.examples:
                prompt += f"Input: {ex['input']}\nOutput: {ex['output']}\n---\n"
                
        if force_cot:
            prompt += "Instruction: Let's break this down step-by-step to explain our reasoning first, then output the final result.\n"
            
        prompt += f"Input Query: {query}\nOutput:"
        return prompt

if __name__ == "__main__":
    composer = PromptComposer("You are a translator that outputs translations in JSON formats.")
    composer.add_few_shot_example("Hello", '{"translation": "Hola"}')
    
    full_prompt = composer.compose("Good Morning")
    print(full_prompt)

Interview Questions

  • Beginner: What is the difference between a System prompt and a User prompt?
  • Intermediate: What is Few-Shot Prompting and when should you use it?
  • Advanced: Why does Chain-of-Thought (CoT) prompting improve an LLM's performance on complex logical or mathematical reasoning tasks?

Interview Answers

  • Beginner: A System prompt sets the overall behavior, rules, and persona of the assistant. A User prompt is the specific question or task requested by the user during a conversation.
  • Intermediate: Few-shot prompting involves providing input-output examples inside the prompt. You should use it when the target output format is hard to describe in words, or when the model needs to learn a specific formatting pattern.
  • Advanced: LLMs process text token-by-token. If a model must output an answer immediately, it has only one step of computation to determine the result. Chain-of-Thought forces the model to generate intermediate reasoning tokens first, using these tokens as "working memory" to process calculations sequentially, which improves logical reasoning accuracy.

Common Mistakes

  • Vague instructions: Writing prompts like "Be fast and make sure there are no bugs". LLMs respond better to precise, actionable instructions with clear formatting boundaries.

Assignment

Design a prompt template for an agent that reads email bodies and extracts user names. Include two few-shot examples and a step-by-step extraction instruction.


Discussion

Join the discussion! Sign in to leave comments and ask questions.

Loading discussion...