Prompt Engineering

From zero-shot to ReAct, guardrails, and enterprise prompt lifecycle.

Companion learning site

Craft-N-Vibe

Interactive prompting guide from beginner to advanced—examples, playground, and best practices.

Continue on the dedicated site below—this portal keeps enterprise reference content alongside that curriculum.

Open Craft-N-Vibe →

00Introduction

Overview

Prompt engineering is the practice of designing and optimizing language model inputs to achieve reliable, safe, and measurable outputs in production systems.

Technical Deep Dive

Unlike traditional programming, behavior is emergent from natural language specifications plus model weights. Engineers treat prompts as versioned configuration coupled to eval harnesses—similar to infrastructure-as-code for non-deterministic compute.

Disciplines include: instruction design, in-context learning, structured output enforcement, adversarial robustness, and cost/latency optimization through prompt compression.

Practical Use Case

AI platform team establishes a Prompt Center of Excellence: style guide, template library, mandatory eval CI on merge, quarterly red-team. Reduces production incidents from prompt drift by 60% year-over-year.

01Zero-Shot Prompting

Overview

Zero-shot prompting instructs the model to perform a task without in-context examples—relying entirely on pretraining and alignment (SFT/RLHF) to infer the task schema.

Technical Deep Dive

Mechanism: instruction I and input x form prompt P = I ⊕ x; model computes P(y|P). No demonstration set D. Effectiveness highest when task matches pretraining distribution (classification, summarization, translation).

Failure modes: ambiguous label space, format non-compliance, domain jargon absent from training. Mitigate with explicit output schema (JSON mode), role prompting, and temperature 0 for determinism.

Practical Use Case

News aggregator classifies 50k articles/day into 12 categories via zero-shot with JSON schema {category, confidence}. No labeled training data; accuracy 91% vs. 94% fine-tuned BERT—saved 6 weeks labeling.

02Few-Shot Prompting

Overview

Few-shot prompting includes k exemplar (input, output) pairs in-context, enabling the model to infer task format and edge-case handling without weight updates.

Technical Deep Dive

Token cost scales with k × example length. Selection strategies: random k, semantic similarity to query (dynamic few-shot), diversity sampling. Optimal k often 3–7; diminishing returns beyond due to context dilution.

Research: in-context learning behaves like implicit Bayesian inference over latent task variable. Ordering bias exists—shuffle examples or use balanced label order per class.

Practical Use Case

Invoice extraction uses 5 examples per vendor layout stored in prompt registry; dynamic retrieval picks closest layout by embedding similarity. Field accuracy 96% across 40 vendor templates.

03Chain of Thought (CoT)

Overview

Chain-of-Thought elicits intermediate reasoning tokens before the final answer, improving multi-step math, logic, and planning tasks on capable models.

Technical Deep Dive

Variants: zero-shot CoT (add 'think step by step'), few-shot CoT (exemplars with rationales), self-consistency (sample N chains, majority vote). Costs extra output tokens and latency.

Do not use CoT for simple extraction where reasoning increases hallucination surface. Validate final answer independently when possible (calculator tool, code execution).

Practical Use Case

Loan underwriting agent uses CoT for debt-ratio explanation paragraphs required by regulation; final numbers verified by deterministic spreadsheet tool—not LLM arithmetic.

04ReAct

Overview

ReAct interleaves natural language reasoning (Thought) with tool actions (Action) and environment feedback (Observation)—grounding decisions in external state rather than pure hallucination.

Technical Deep Dive

Loop: Thought → Action (tool + args) → Observation → … → Final Answer. Cap max iterations. Log full trace for debugging. Modern implementations use native function calling instead of parsing Action: lines.

Combines with retrieval (search tool), code execution (Python REPL), APIs. Failure: repetitive tool calls—detect via action hash repetition and break.

Practical Use Case

Internal research assistant ReAct loop: search Confluence → read pages → synthesize memo with citations. Avg 4.2 tool calls per query; 78% user satisfaction vs. 52% single-shot RAG.

05Prompt Security

Overview

Prompt security defends against instruction injection—untrusted content (emails, web pages, PDFs) embedded in prompts that hijack model behavior to exfiltrate data or bypass policies.

Technical Deep Dive

Defenses: (1) structural separation—system vs user channels; (2) input sanitization and length limits; (3) output filters (PII regex, topic classifiers); (4) tool least-privilege; (5) canary tokens detecting leakage.

Indirect injection: malicious text in retrieved RAG chunk attempts policy override—mitigate with retrieval source trust tiers and post-retrieval content scanning.

Practical Use Case

Customer support bot sandbox: tools cannot access other users' tickets; system prompt immutable server-side; user uploads scanned for injection patterns before entering RAG index.

Role Prompting

Overview

Role prompting assigns a persona in the system message—shaping vocabulary, risk tolerance, depth, and refusal boundaries without changing model weights.

Technical Deep Dive

Effective roles specify: expertise domain, audience, output format, and explicit constraints (e.g. never provide medical diagnosis). Weak roles (you are helpful) add little signal.

Combine with user-role separation: system = immutable policy; user = untrusted. Role drift occurs in long conversations—re-inject role summary every N turns.

Practical Use Case

Cybersecurity advisor role for SOC analysts: uses MITRE ATT&CK terminology, severity scoring template, refuses offensive hacking instructions. Reduces alert triage time 25%.

Context Engineering

Overview

Context engineering curates what enters the finite context window: which history, documents, tool results, and metadata—often more impactful than tweaking wording.

Technical Deep Dive

Techniques: summarization of old turns, RAG with reranking, lost-in-the-middle mitigation (place critical info at start/end), dynamic tool result truncation, structured metadata headers per chunk.

Measure context efficiency: answer quality vs. tokens used. Prompt compression (LLMLingua) for bulk context.

Practical Use Case

Legal discovery loads only clauses matching matter_id metadata filter + top-8 reranked chunks—not entire 500-page contracts—cutting hallucinated clause references 70%.