What is prompt engineering?

Prompt engineering is the practice of crafting and structuring inputs to an LLM to reliably get the quality, format, and accuracy of output you need.

AILLMPrompt Engineering

Overview

LLMs are sensitive to how you phrase your request. The same question written differently can produce vastly different results - in quality, format, accuracy, and tone. Prompt engineering is the skill of crafting inputs that reliably produce the desired output. It involves techniques like role assignment, few-shot examples, chain-of-thought reasoning, and output format constraints.

Key Points

  • •**System prompt**: sets the model's persona, constraints, and context before the user speaks.
  • •**Few-shot examples**: include 2–5 input/output examples in the prompt - the model follows the pattern.
  • •**Chain-of-thought**: asking the model to "think step by step" before answering dramatically improves accuracy on reasoning tasks.
  • •**Output format constraints**: specify JSON, markdown, a specific schema - models follow format instructions well.
  • •**Temperature**: lower values (0–0.3) for factual/consistent tasks; higher (0.7–1.0) for creative tasks.
  • •**Role assignment**: "You are an expert TypeScript developer…" anchors the model's style and vocabulary.

Examples

System prompt + structured output

typescript
const response = await client.chat.completions.create({
  model: 'gpt-4o',
  response_format: { type: 'json_object' },  // force JSON output
  messages: [
    {
      role: 'system',
      content: `You are a code review assistant.
Respond ONLY with valid JSON in this shape:
{ "issues": [{ "line": number, "severity": "error"|"warn", "message": string }] }
If there are no issues, return { "issues": [] }.`,
    },
    {
      role: 'user',
      content: `Review this code: 

${codeSnippet}`,
    },
  ],
});

const review = JSON.parse(response.choices[0].message.content);

Few-shot prompting - teach by example

typescript
const prompt = `Classify the sentiment of each review as: positive, negative, or neutral.

Review: "The product arrived on time and works perfectly."
Sentiment: positive

Review: "Completely broken, waste of money."
Sentiment: negative

Review: "It arrived. Does what it says."
Sentiment: neutral

Review: "${userReview}"
Sentiment:`;

// The model continues the pattern you established

Chain-of-thought - force step-by-step reasoning

typescript
// Without chain-of-thought - model often guesses
const bad  = 'Is 9677 a prime number? Answer yes or no.';

// With chain-of-thought - dramatically more accurate
const good = `Is 9677 a prime number?
Think step by step: check divisibility starting from 2.
Show your reasoning, then give a final answer.`;

// Result: model correctly walks through trial division
// before concluding (9677 = 89 × 109, so: not prime)

Prompt template with variable injection

typescript
function buildPrompt(language: string, code: string): string {
  return `You are an expert ${language} developer.
Analyse the following code and:
1. Identify any bugs or logic errors
2. Suggest performance improvements
3. Flag any security issues

Code:
\`\`\`${language}
${code}
\`\`\`

Respond in markdown with clear headings for each section.`;
}

Pro Tip

Treat your system prompt like a specification document - the more precise, the better. Include: what role the model plays, what format the output should be in, what it should NOT do, and 2–3 examples of ideal input/output pairs. Vague prompts produce vague results; specific prompts produce reliable ones.