Home/ Part XIII — Expert Mode: Systems, Agents, and Automation/39. Prompt Engineering for Experts (The Real Stuff)

39. Prompt Engineering for Experts (The Real Stuff)

Overview and links for this section of the guide.

Beyond "You are a helpful assistant"

Expert prompt engineering treats prompts as programmable interfaces. You version them, test them, and structure them hierarchically.

// Beginner prompt
"You are a helpful assistant. Answer the user's question."

// Expert prompt
const prompt = composePrompt({
  global: GLOBAL_RULES,
  module: CODE_REVIEW_RULES,
  task: buildReviewTaskPrompt(diff, context),
  examples: selectRelevantExamples(diff, exampleDb),
  output: STRUCTURED_OUTPUT_SCHEMA
});

Expert Principles

Principle Beginner Expert
Structure Free-form text Hierarchical sections with XML/Markdown
Examples Fixed few-shot Dynamic selection based on query
Output "Return JSON" Enforced schema with validation
Testing Manual spot checks Automated eval suites
Versioning None Git-tracked with regression tests

Advanced Techniques

Chain of Thought (CoT)

"Think step by step" is the basic version. Expert CoT uses forced structure:

Before giving your answer, you MUST output your reasoning in this format:


1. First, I observe that...
2. This implies...
3. Therefore...


Then provide your final answer in JSON.

Few-Shot Selection

Instead of fixed examples, dynamically select the most relevant ones:

// Dynamic example selection
const relevantExamples = await vectorDb.search({
  query: userQuery,
  collection: 'prompt_examples',
  limit: 3
});

const prompt = `${SYSTEM_INSTRUCTION}

Here are relevant examples:
${relevantExamples.map(formatExample).join('\n\n')}

Now handle this query: ${userQuery}`;

Self-Consistency

Ask the model the same question multiple times and take the majority vote:

async function selfConsistency(prompt: string, n = 5): Promise {
  const responses = await Promise.all(
    Array(n).fill(null).map(() => 
      model.generate({ 
        prompt, 
        temperature: 0.7  // Higher temp for diversity
      })
    )
  );
  
  // Count answers
  const counts = new Map();
  for (const r of responses) {
    const answer = extractAnswer(r);
    counts.set(answer, (counts.get(answer) || 0) + 1);
  }
  
  // Return most common
  return [...counts.entries()].sort((a, b) => b[1] - a[1])[0][0];
}

Where to go next