Skip to main content
Master prompt engineering using LangChain to get consistent, useful, and controllable outputs from LLMs. This guide demonstrates practical prompting techniques — Zero-Shot, One-Shot, Few-Shot, and Chain-of-Thought — with runnable examples and recommended best practices. Why this matters: LLMs often produce vague, inconsistent, or incomplete responses when prompts lack structure. The techniques below help you control format, tone, length, and reasoning so outputs match your requirements.
A screenshot of a tutorial titled "Master Prompt Engineering with LangChain" that outlines prompting techniques like zero-shot, one-shot, few-shot, and chain-of-thought. The right sidebar shows a file list of Python task scripts.
High-level tip: choose the prompting technique that matches your goal — speed, format consistency, tone, or complex reasoning — and provide explicit constraints (format, length, audience) to get predictable outputs.
Environment verification Before starting the exercises, confirm your development environment is ready (virtualenv activated, LangChain installed, OpenAI credentials configured, and an LLM connection working). This prevents runtime errors and allows you to focus on prompt quality during experiments. Run this one-line environment check:
Expected verification output (example):
Do not commit API keys or secret files to version control. Keep credentials in environment variables or a secure secret store and verify access only from trusted machines.
Once verification passes and prompt utilities are available, proceed to the tasks below. Each task includes the concept, an example prompt or script, expected behavior, and best practices to help you reproduce consistent results.

Task 1 — Zero-Shot Prompting

Zero-shot prompting asks the model to perform a task with no examples. The quality of the output depends heavily on how explicit and constrained the instruction is.
A screenshot of a presentation slide titled "Task 1: Zero-Shot Prompting (2 minutes)" that defines zero-shot prompting, contrasts vague vs. specific prompts, and gives example prompts. A dark sidebar on the right lists code filenames (e.g., task_1_zero_shot.py).
Key idea: prefer a specific instruction that includes audience, jurisdiction, required sections, and constraints (word count, tone, or format). Illustrative Python comparison (vague vs. specific zero-shot prompts):
Example console output (trimmed):
Zero-shot best practices:
  • State the exact task and desired length.
  • Define context (jurisdiction, audience, domain).
  • List required sections or bullet points the output must include.
  • Constrain format where necessary (e.g., JSON, Markdown, or numbered sections).

Task 2 — One-Shot Prompting

One-shot prompting supplies a single example that demonstrates the desired format, tone, or structure. It’s useful when you want the model to reproduce a template or layout across many inputs. Example: Provide a refund policy template as the one-shot example and ask the model to produce a remote work policy using the same structure. One-shot example (refund policy template):
Then ask the model:
Generated result (example):
One-shot benefits:
  • Enforces a single-template formatting.
  • Fast to set up for repetitive, structured documents.
  • Good when you want to preserve a strict layout without many examples.

Task 3 — Few-Shot Prompting

Few-shot prompting gives the model several diverse examples so it can learn format, tone, and response patterns. This technique is ideal for customer support, marketing copy, or any content that requires consistent voice across variations.
A dark-themed screen showing a presentation card titled "Task 3: Few-Shot Prompting (3 minutes)" with a definition and bullet points explaining why multiple examples matter. On the right is a file/sidebar list with Python files like task_2_one_shot.py and task_3_few_shot.py.
Example few-shot training set (customer support style examples):
New prompt and model output:
Quick response analysis example:
Few-shot advantages:
  • Learns subtleties of tone and phrasing across examples.
  • Keeps responses consistent across agents or channels.
  • Reduces need for labeled fine-tuning for many use-cases.

Task 4 — Chain-of-Thought (CoT) Prompting

Chain-of-Thought prompting encourages the model to expose intermediate reasoning steps. This produces more accurate and defensible answers for complex or multi-step tasks. Use CoT when you need the model to enumerate assumptions, weigh options, or provide a stepwise troubleshooting path. Example LangChain prompt templates (fixed and syntactically correct):
Simple CoT instruction pattern:
CoT best practices:
  • Provide worked examples that demonstrate intermediate steps.
  • Ask the model to enumerate assumptions and order steps by likelihood.
  • Use models/configurations that support longer contexts for full reasoning chains.
  • Prefer CoT when correctness and traceability matter.

Task 5 — Technique Showdown (Comparison)

Compare the techniques by running the same task through each style and evaluating differences in structure, tone, and completeness.
A dark-themed slide titled "Task 5: Technique Showdown" listing and briefly describing four prompting techniques (Zero-Shot, One-Shot, Few-Shot, Chain-of-Thought) with a highlighted key insight box; a colorful cursor points at "Chain-of-Thought." A file sidebar with task filenames is visible along the right edge of the screen.
Example Python script comparing all four techniques:
Typical comparative observations:
  • Zero-Shot: fastest but may miss company specifics or required sections.
  • One-Shot: enforces a strict format from a single template.
  • Few-Shot: matches tone and variations across multiple examples.
  • Chain-of-Thought: produces longer, structured reasoning and more comprehensive policies.
Comparison table (quick reference):
A screenshot of a coding tutorial popup titled "Congratulations!" listing mastered prompting techniques (zero-shot, one-shot, few-shot, chain-of-thought) and a key takeaway. To the right is a code editor/file explorer showing Python files such as task_5_comparison.py.

Wrap-up and Practical Tips

By completing these exercises you should now be able to:
  • Choose the right prompting technique based on goals (speed, format, tone, or reasoning).
  • Design explicit constraints (format, length, audience) to get predictable outputs.
  • Use One-Shot and Few-Shot prompts to enforce structure and brand voice.
  • Use Chain-of-Thought when you need traceable, stepwise reasoning.
Quick checklist before running experiments:
  • Provide role/context to the model (e.g., “You are an expert HR advisor”).
  • Include required sections and constraints.
  • Use examples to teach format or tone when needed.
  • Keep a simple evaluation rubric (empathy, actionability, timeline) to compare outputs.
Links and References Recommended next steps:
  • Run the provided tasks in your environment and compare outputs across multiple model sizes.
  • Iterate on prompts and evaluate with a small rubric (correctness, format, tone).
  • Automate comparisons with simple scripts (as shown in task_5_comparison.py) to measure improvements over prompt versions.

Watch Video

Practice Lab