Prompt Engineering for AI

Undergraduate module · Self-paced & instructor-led
Undergraduate No ML required ~3 hours Pass: 75%
OpenAI · Anthropic · DeepMind research
Learning Objectives
  • Understand what AI language models are and why how you phrase a request matters enormously
  • Learn the vocabulary — prompt, context, token, temperature, inference — with zero assumed ML background
  • Apply 6 core prompting techniques (zero-shot, few-shot, chain-of-thought, role, structured output, iterative refinement)
  • Write prompts step-by-step using a repeatable 5-element framework (Role → Task → Context → Constraints → Output format)
  • Diagnose and fix weak prompts — understand why a model gave a poor answer and how to revise
  • Understand the ethical and practical limits of prompt-based AI use
6
Prompting techniques covered
5
Elements in the prompt framework
~3h
Total study time
0
ML prerequisites needed
Module At A Glance
#SectionFormatTime
1Foundations — AI & ML background vocabularySelf-paced reading25 min
2Anatomy of a Prompt — the 5-element frameworkLecture + worked examples30 min
3Prompting Types — zero-shot to chain-of-thoughtInteractive examples40 min
4Stepwise Practice — build & refine promptsLive sandbox45 min
5Ethics & LimitationsDiscussion + case studies20 min
6Knowledge Check — 10 questionsIndividual assessment15 min
What You Will Need
  • A web browser — Chrome, Firefox, Safari, or Edge
  • Access to an AI assistant — Claude, ChatGPT, or Gemini (free tier is fine)
  • A notebook or text editor — for saving your prompt drafts
  • No coding required — though example code is shown for context

New to AI? Start with the Foundations tab. It explains every technical term — from "neural network" to "temperature" — in plain language with historical context. You can revisit it any time.

Foundation Keywords

Everything you need to know before writing your first prompt — from what "AI" actually means, to the history of language models, to the vocabulary used throughout this module. No prior knowledge assumed.

Artificial Intelligence & Machine Learning
AI
Artificial Intelligence
Machines that simulate human-like reasoning

Artificial Intelligence (AI) is the broad field of building computer systems that can perform tasks that would normally require human intelligence — understanding language, recognising images, making decisions, solving problems.

1950
Alan Turing proposes the "imitation game" — can a machine think? The Turing Test is born.
1956
The Dartmouth Conference coins the term "Artificial Intelligence". Early AI uses hand-written rules.
1980s–90s
Expert systems and rule-based AI dominate — but they fail on tasks too complex to write rules for.
2010s
Deep learning revolution. AI beats humans at image recognition, Go, and then language tasks.
2022–now
ChatGPT, Claude, Gemini. Conversational AI becomes mainstream — the era of prompt engineering.
Why it matters: Prompt engineering is the primary interface between humans and modern AI systems. Understanding AI's history helps you understand its capabilities and limitations.
ML
Machine Learning
Learning from data rather than explicit rules

Machine Learning (ML) is a subset of AI where systems learn patterns from data rather than being programmed with hand-written rules. Given enough examples, the model learns to generalise — to handle new inputs it has never seen before.

Analogy
Traditional programming: IF email contains "win a prize" THEN → spam IF email contains "meeting at 3pm" THEN → not spam Machine Learning: Show model 10 million labelled emails. Model learns its OWN rules — much more nuanced.
Connection to prompting: Language models are trained by ML on billions of text examples. Prompts work because the model has "seen" similar text and learned how to respond — your prompt is activating learned patterns.
ML
Neural Network
Loosely inspired by the structure of the brain

A neural network is a type of ML model made of layers of mathematical units ("neurons") that transform an input into an output. Each connection has a weight — a number — that is adjusted during training to improve the model's accuracy.

Simplified structure
Input layer: "What is 2 + 2?" (text → numbers) ↓ hidden layers (billions of weights) Output layer: "4" (numbers → text)
Key insight: You don't need to understand the maths inside a neural network to use it effectively — just as you don't need to understand a car engine to drive. But knowing the basics helps you understand why some prompts work better than others.
ML
Training
The process of learning from data

Training is the process where a model adjusts its internal weights by seeing millions or billions of examples and learning to predict correct outputs. Modern language models are trained on text from the internet, books, and code over weeks on thousands of specialised chips.

~15T
Tokens LLaMA-3 trained on
weeks
Training time (GPUs)
fixed
Knowledge after training
Critical limitation: Once trained, a model's knowledge is frozen at the training cutoff date. It cannot learn from your conversation — each prompt starts fresh. This is why prompts must provide all needed context.
Language Models & How They Work
LLM
Large Language Model
Abbreviated LLM — the engine behind ChatGPT, Claude, Gemini

A Large Language Model (LLM) is a neural network trained on vast amounts of text that can generate, summarise, translate, reason about, and converse in natural language. "Large" refers to billions of learned parameters (internal numbers).

Scale comparison
GPT-2 (2019): 1.5 billion parameters GPT-3 (2020): 175 billion parameters GPT-4 (2023): ~1 trillion (estimated) LLaMA-3 (2024): 8B / 70B / 405B variants
2017
The Transformer architecture invented (Google "Attention Is All You Need" paper). Foundation of all modern LLMs.
2018
BERT (Google) and GPT-1 (OpenAI). First large pre-trained language models.
2022
ChatGPT launches. 1 million users in 5 days. Prompt engineering becomes a mainstream skill.
2024
GPT-4o, Claude 3, Gemini 1.5. Multimodal (text + images + audio) LLMs. 200K+ token context windows.
LLM
Token
The basic unit an LLM reads and writes

A token is a chunk of text — roughly a word or word fragment — that an LLM processes one at a time. Models don't see letters; they see tokens. Each token is converted to a number, processed, and the model predicts the next most likely token.

How "Prompt Engineering" tokenizes
"Prompt" → 1 token "Engineering" → 1–2 tokens "Unbelievable"→ ["Un", "believ", "able"] = 3 tokens "😀" → 2–4 tokens (emoji cost more!)
~0.75
words per token (English)
$0.003
per 1K tokens (approx)
Why it matters: LLMs have a maximum number of tokens per conversation (the context window). Long prompts use up tokens — leaving less room for the response. Concise prompts are more efficient.
LLM
Temperature
The creativity / randomness dial

Temperature is a setting (usually 0–2) that controls how predictable or creative a model's output is. At temperature 0, the model always picks the most likely next token. At high temperature, it picks more surprising words — more creative, but also less accurate.

Temperature effects on the same prompt
Prompt: "Give me a word that means 'happy'" Temp 0.0 → "joyful" (always the same, safest) Temp 0.7 → "elated" (varied, still sensible) Temp 1.5 → "sunburst" (creative, unexpected) Temp 2.0 → "flurfl" (may become nonsense)
Practical guide: Use low temperature (0–0.3) for factual tasks like summarising or coding. Use higher temperature (0.7–1.0) for creative writing, brainstorming, or idea generation.
LLM
Context Window
The model's working memory per conversation

The context window is the maximum number of tokens a model can consider at once — including your prompt, the conversation history, and the model's reply. Content outside this window is forgotten.

Context limits (2024)
GPT-3.5: 4,096 tokens ≈ 3,000 words GPT-4: 128,000 tokens ≈ 90,000 words Claude 3: 200,000 tokens ≈ 150,000 words Gemini 1.5: 1,000,000 tokens (1M!)
Key insight: Unlike a human colleague, the model has no memory between separate conversations. Every new conversation is a blank slate — your prompt must supply all relevant background each time.
LLM
Inference
The moment the model generates a response

Inference is when a trained model runs on an input to produce an output. Unlike training (which takes weeks), inference is fast — seconds. LLMs generate text one token at a time, each token informed by everything before it.

Token-by-token generation
Prompt: "The capital of France is" Token 1: "Paris" ← model stops (high confidence) Prompt: "Write a haiku about rain" Token 1: "Silver" Token 2: " drops" Token 3: " fall" ... (continues until complete)
Why this matters for prompts: Because generation is one token at a time, asking the model to "think step by step" before answering genuinely improves accuracy — each token becomes context for the next.
History
Hallucination
When a model confidently says something false

A hallucination occurs when an LLM generates text that is grammatically fluent and confident-sounding but factually wrong or entirely fabricated. The model predicts plausible text, not verified truth — it has no internal fact-checker.

Classic hallucination example
Prompt: "List 3 papers by Dr. Sarah Chen on NLP" AI: "1. Chen, S. (2021). Transformer efficiency... 2. Chen, S. (2019). Cross-lingual... 3. Chen, S. (2023). Prompt robustness..." Reality: None of these papers exist. The citations are entirely fabricated.
Practical rule: Never trust an LLM's citations, legal claims, medical advice, or specific statistics without verification. Prompt strategies like "only use information I provide" or "say 'I don't know' if unsure" can reduce (not eliminate) hallucination.
Prompt Engineering Vocabulary
Prompting
Prompt
The input you give the model

A prompt is the text input you send to an AI model. It is your instruction, question, or context. The quality, specificity, and structure of your prompt is the single biggest factor in the quality of the output you receive.

Weak vs strong prompt
Weak: "Write about climate change." Strong: "Write a 3-paragraph explainer on the causes of climate change for a 16-year-old audience. Use simple language, one real-world analogy, and end with a hopeful note."
Core principle: The model cannot read your mind. Every piece of context you don't provide, it will fill with its best guess. Explicit instructions beat vague ones every time.
Prompting
System Prompt
The hidden instruction layer set by the developer

A system prompt is a special set of instructions sent to the model before the user's message, usually set by the application developer. It defines the model's persona, constraints, and behaviour rules. Users typically cannot see it.

Typical system prompt structure
SYSTEM: You are a helpful customer support assistant for AcmeCorp. Only answer questions about AcmeCorp products. If asked about competitors, politely decline. Respond in British English. USER: "What's the return policy?"
Practical use: When using APIs (e.g. for a student project), the system prompt is where you set up the model's role, personality, and rules. Think of it as the "briefing" before the conversation starts.
Prompting
Role Prompting
Giving the model a persona or expert identity

Role prompting (also called persona prompting) tells the model to behave as a specific expert, character, or persona. This frames the model's response style, vocabulary, and perspective — often producing more focused, domain-appropriate answers.

Effect of role on the same question
No role: "Explain recursion" → Generic textbook answer With role: "You are a patient high-school computer science teacher. Explain recursion." → Analogies, simpler vocabulary, step-by-step scaffolding
When to use it: When you need domain-specific depth (e.g. "act as a lawyer", "act as a data scientist"), a specific tone (e.g. "act as a friendly mentor"), or a calibrated reading level (e.g. "explain as if to a 10-year-old").
Prompting
Output Format
Specifying how the answer should be structured

Output format instructions tell the model how to structure its response — as a bullet list, JSON, table, numbered steps, a poem, markdown, etc. Without format instructions, the model chooses whatever seems natural, which may not suit your use case.

Format specification examples
"Return your answer as a JSON object with keys: 'title', 'summary', 'tags'" "Respond in a numbered list of exactly 5 steps" "Format as a markdown table with columns: Pros | Cons | Verdict" "Reply in under 50 words. No bullet points."
Pro tip: For technical use (e.g. feeding output into code), always specify JSON and provide an example schema. For human readers, bullet lists and numbered steps are easier to scan than dense paragraphs.
Prompting
Iteration
Refining your prompt based on the output

Iteration is the practice of refining and improving your prompt based on what the model returns. Rarely is a first prompt perfect. Effective prompt engineers treat prompting as a dialogue — they read the output critically and adjust constraints, examples, or framing.

Iteration loop
v1: "Summarise this article." → Too long, too formal v2: "Summarise in 3 bullet points, casual tone." → Good length, but misses main point v3: "Summarise the 3 key arguments in the article below in bullet points. Casual tone. Lead with the most surprising finding." → ✓ Perfect
Mindset shift: Don't expect the first prompt to be perfect. The fastest path to a great output is a fast feedback loop — try, read, adjust, repeat.
Prompting
Guardrails & Refusal
Safety limits built into modern models

Guardrails are safety constraints trained into LLMs that prevent them from producing harmful, illegal, or misleading content. When a model refuses a request, it is because the request triggered one of these constraints — not because it "can't" do it technically.

Why models refuse
✗ "Write malware that steals passwords" → Refused: harmful intent ✗ "Write a fake news article pretending to be from BBC" → Refused: misinformation / impersonation ✓ "Write a fictional news-style story for a creative writing class" → Usually allowed: clearly fictional context
Ethical principle: Guardrails exist for good reasons. Attempting to bypass them ("jailbreaking") is unethical and may violate terms of service. Understanding what models won't do is as important as knowing what they will do.
How These Concepts Connect
Your prompt travels through these layers. Understanding each one makes you a more effective prompt engineer.
You write a Prompt
Tokenized into IDs
Passed through context window
LLM runs inference
Tokens generated one-by-one
Response returned to you
The 5-Element Prompt Framework
Every great prompt can be built from these five components — use as many as your task needs

How to use this framework: You don't always need all five elements. Simple questions may only need Task. Complex tasks benefit from all five. Work through them in order and omit what isn't relevant.

Step-By-Step Walkthrough
1
Role — Who should the AI be?

Assign the model an expert identity. This shapes vocabulary, depth, and perspective. The more specific the role, the more calibrated the answer.

Example
"You are an experienced data analyst with expertise in Python and business reporting."
Why it works

The model shifts from generic knowledge to domain-specific framing — it will use pandas/matplotlib terminology, assume business context, and avoid over-explaining basics.

2
Task — What exactly do you need?

State the action verb clearly. Be specific about the deliverable. Avoid vague requests like "help me with" or "tell me about".

Weak vs Strong
❌ "Help me with my essay about climate change"
✅ "Write a 400-word introduction for an undergraduate essay arguing that carbon pricing is the most effective policy tool to reduce emissions."
3
Context — What background does the model need?

Paste in relevant documents, data, or background. The model cannot browse the internet (unless given a tool) — anything it needs to know must be in the prompt.

Example
"Here is the sales data for Q3 [paste data here]. The company sells three product lines: hardware, software, and services. Our main concern is the 15% drop in software revenue."
Tip: Put context before the task. Models read top-to-bottom — context they haven't seen yet can't influence their interpretation of the task.
4
Constraints — What should the model avoid or respect?

Add boundaries: word limits, audience level, tone, things to exclude, factual scope. Constraints prevent the model from making assumptions that don't match your needs.

Constraint examples
"Do not use technical jargon. Write for a non-specialist audience. Keep each point to one sentence. Do not speculate beyond the provided data. Do not use bullet points."
5
Output Format — How should the response be structured?

Tell the model exactly how to format its answer. This is especially important when the output will be read by humans in a document, or processed by code.

Format instructions
"Return your answer as a markdown report with the following sections: Executive Summary (2 sentences), Key Findings (3 bullet points), Recommended Actions (numbered list). End with a one-line conclusion."
Full assembled prompt (all 5 elements)
You are an experienced business analyst. [ROLE] Analyse the Q3 sales data below and identify the root cause of the 15% software revenue decline. [TASK] [Context: paste data here] [CONTEXT] Do not speculate. Use only the data provided. Avoid jargon. [CONSTRAINTS] Return: Executive Summary (2 sentences), Key Findings (3 bullets), Recommended Actions (numbered). [OUTPUT FORMAT]
Before & After: Prompt Transformation
❌ Before (Weak Prompt)
"Explain machine learning to me"

Problems: No audience level. No format. No length. No purpose. Model will produce generic textbook content.

✅ After (Strong Prompt)
"You are a friendly university lecturer. Explain machine learning to a first-year undergraduate student who has no statistics background. Use one everyday analogy, keep it under 200 words, and end with one question to check understanding."

Result: Calibrated to audience, constrained length, includes analogy, ends interactively.

6
Core Prompting Techniques
All 6 techniques explained in full — definition, when to use, step-by-step, worked example, strengths & limitations

How to use this section: Read all six techniques in order — each one builds on the previous. Use the jump-nav below to revisit any technique quickly.

Stepwise Prompt Builder
Build a prompt element by element — then see the assembled result
Step 1 — Role
Step 2 — Task
Step 3 — Context (paste any relevant info)
Step 4 — Constraints
Step 5 — Output Format
Prompt Diagnosis — What Went Wrong?

Identify the problem with each weak prompt below. Click to reveal the diagnosis and improved version.

Ethics Checklist — Before You Submit
  • Accuracy risk: Could the model hallucinate important facts? → Verify outputs independently, especially for medical, legal, or financial information.
  • Privacy: Does your prompt contain personal data (names, emails, health info)? → Remove or anonymise before sending to a third-party AI.
  • Attribution: Are you passing off AI-generated content as fully your own work? → Follow your institution's academic integrity policy.
  • Bias: Could the model's output reflect stereotypes or unfair assumptions? → Review outputs critically, especially in hiring, assessment, or research contexts.
  • Environmental cost: LLM inference uses significant compute energy. → Batch your requests and avoid unnecessary calls.
Practical Examples — Kaggle & Hugging Face
Real-world code patterns used in competitions and production pipelines · Click a technique to explore

How to use this section: Each tab shows production-grade code patterns. Copy the snippets into your own environment and adapt them. Techniques map 1-to-1 with the Prompting Types tab.

Zero-Shot — Hugging Face Pipeline
facebook/bart-large-mnli · HuggingFace

No examples given — model uses its pre-trained knowledge. The BART MNLI model lets you pass any candidate labels without fine-tuning. Great for prototyping on Kaggle datasets where labels aren't fixed.

❌ Vague zero-shot
"Classify this review."
✅ Clear zero-shot with format lock
"Classify the sentiment of this product review as Positive, Negative, or Neutral. Return only the label. Review: 'Battery life is disappointing but the screen is gorgeous.'"
HuggingFace — zero-shot-classification
from transformers import pipeline classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") result = classifier( "This course teaches neural networks from scratch.", candidate_labels=["education", "sports", "politics"], ) # result scores: education=0.91, sports=0.04, politics=0.04

Kaggle tip: BART-MNLI is a zero-shot powerhouse — no fine-tuning needed. Swap candidate_labels freely between datasets without retraining.

Few-Shot — Kaggle NLP Competition Pattern
LegalBench · Kaggle Competition Pattern

Provide 2–5 labeled examples directly in the prompt. Teams on the LegalBench leaderboard found 3–5 shots outperform zero-shot by ~8 F1 points on edge cases. Consistent formatting (Clause / Label pairs) is the key.

Few-shot prompt construction
SYSTEM = """You are a legal text classifier. Classify each clause as: Obligation | Right | Prohibition""" EXAMPLES = """ Clause: "The vendor must deliver goods within 14 days." Label: Obligation Clause: "The user may cancel at any time." Label: Right Clause: "Sublicensing is strictly forbidden." Label: Prohibition """ USER = f"""{EXAMPLES} Clause: "The licensee shall not reverse engineer the software." Label:""" # Model outputs: "Prohibition"

Why it works: Consistent Clause / Label formatting teaches the model the exact pattern. The final Label: with no value is the prompt — the model completes it.

Quick Reference — Technique Selection Guide
ScenarioBest TechniqueReal-World Source
Classify text with custom labels, no training dataZero-Shot (BART-MNLI)HuggingFace zero-shot-classification
Custom output format, consistent across many inputsFew-Shot (2–5 examples)Kaggle LegalBench (+8 F1)
Multi-step maths or logic, need auditable reasoningChain-of-Thought + self-consistencyKaggle MATH / GSM8K (74%→89%)
Domain-specific depth, calibrated reading levelRole Prompting + format lockKaggle Science Exam (+4.2%)
Output fed into code or a databaseStructured Output (JSON schema / Outlines)HuggingFace Outlines library
Question answering over private or recent documentsRAG (retrieve then generate)HuggingFace FAISS + MiniLM
Long-form writing or complex analysis needing polishIterative Refinement (2–3 rounds)Kaggle LMSYS Arena (+6 Elo)
Knowledge Check — 10 Questions · Pass Mark 75%
Comprehensive Reference · 2025

Prompt Engineering
Strategies × LangChain

Industry practices, trends, and agentic AI applicability — mapped to current LangChain / LangSmith documentation and LangGraph patterns.

Zero-shot · Few-shot · CoT ReAct · Structured Output LangGraph · LangSmith PE
Section 01
Core prompt engineering strategies
Zero-shot
Zero-shot prompting
Direct instruction with no examples. Relies purely on the model's pretrained knowledge. Simplest to craft but least reliable for complex tasks.
Simplicity
95
Reliability
58
Agent fit
40
Few-shot
Few-shot prompting
Provides 2–8 examples to guide format, tone, and reasoning pattern. Significantly improves consistency for classification and formatting tasks.
Simplicity
70
Reliability
78
Agent fit
65
Role / Persona
Role / persona prompts
Assigns a role ("You are a senior data engineer…") to anchor tone and expertise domain. Delivered via system message in ChatPromptTemplate. Used for agent specialization.
Simplicity
88
Reliability
65
Agent fit
72
ℹ️ Production agentic systems (LangGraph, CrewAI, agenta.ai) typically combine 3–4 prompt strategies per agent node. No single strategy dominates; the most capable agents layer Role + CoT + ReAct + Structured Output.
Section 02 · LangChain Docs 2025
LangChain prompt classes → strategy mapping
Zero-shot
PromptTemplate
Single-variable string template. Zero-shot by default. Retained for backward compatibility — LC docs recommend ChatPromptTemplate for all new projects.
from langchain_core.prompts import PromptTemplate
PromptTemplate( input_variables=["topic"], template="Explain {topic} briefly." )
Agent fit
40
Production use
55
Few-shot
FewShotChatMessagePromptTemplate
Injects structured human/AI example pairs into a ChatPromptTemplate. Recommended over the legacy FewShotPromptTemplate for all chat models.
from langchain_core.prompts import FewShotChatMessagePromptTemplate
FewShotChatMessagePromptTemplate( example_prompt=ex_prompt, examples=examples )
Agent fit
65
Production use
72
ℹ️ LC docs 2025: All new projects should use chat-style prompts via ChatPromptTemplate. Completion-style PromptTemplate is for backward compatibility only. LangGraph replaces AgentExecutor for all production agents.
Section 03
Strategy × LangChain class × agentic applicability
Strategy LangChain class / API LangGraph role Token cost Structured out Multi-turn Agent fit LC doc status
Zero-shot PromptTemplate Simple node prompts Low 4/10 Legacy
Few-shot FewShotChatMessagePromptTemplate Example injection in system prompt Medium Partial 6.5/10 Active
Chain-of-thought ChatPromptTemplate + system msg Planner / reasoner node High Partial 9/10 Active
ReAct create_react_agent (LangGraph) Core agent loop Think→Act→Observe Very high 10/10 Recommended
Role / Persona ChatPromptTemplate system msg Agent identity / specialization Low Partial 7/10 Active
Structured output with_structured_output() / bind_tools() Output node, tool-call parsing Medium 9.5/10 Recommended
Memory / history MessagesPlaceholder + MemorySaver Stateful graph across turns High 9/10 Active
Prompt versioning LangSmith commits + tags Prod/staging lifecycle Recommended
10
ReAct agent fit
9.5
Structured output fit
9
CoT agent fit
6.5
Few-shot agent fit
4
Zero-shot agent fit
Section 05
Where each prompt pattern lives in a LangGraph agent
Planning node — ChatPromptTemplate + CoT system message
The system message instructs step-by-step reasoning. In LangGraph, this is the first node that decomposes the user goal into subtasks. LC docs confirm ChatPromptTemplate with "Think step by step" in the system role is the standard CoT delivery mechanism.
ChatPromptTemplateCoT
Tool execution — create_react_agent + bind_tools()
LangGraph's create_react_agent is the canonical 2025 ReAct implementation. It binds tools to the model via bind_tools(), auto-generates the Thought→Action→Observation loop, and replaces the old AgentExecutor. state_modifier is the new way to inject system-level CoT instructions.
create_react_agentbind_tools()LangGraph
Memory layer — MessagesPlaceholder + MemorySaver
MessagesPlaceholder("history") in ChatPromptTemplate holds the conversation buffer. LangGraph's MemorySaver checkpointing persists state across turns. For production: LC docs recommend SqliteSaver or PostgresSaver.
MessagesPlaceholderMemorySaver
{ }
Structured output node — with_structured_output() / response_format
Aug 2025 (LangGraph GitHub #5872): create_react_agent was refactored to enforce structured output by binding the response schema as a tool, eliminating extra LLM calls. with_structured_output(PydanticModel) is the recommended pattern for all final-answer nodes.
with_structured_output()Pydantic
Multi-agent routing — ChatPromptTemplate role specialization
In LangGraph multi-agent graphs, each subagent node has its own ChatPromptTemplate with a distinct system message (researcher, coder, critic). The orchestrator uses zero-shot + role prompt to route tasks. Few-shot examples in the orchestrator guide delegation decisions.
ChatPromptTemplateFew-shotRole
Self-critique / reflection — LCEL chain + FewShotChatMessagePromptTemplate
Reflexion-style agents add a reflection node. LangChain Expression Language (LCEL) pipes ChatPromptTemplate | llm | output_parser for the critique step. Few-shot examples of "good vs bad" answers calibrate self-evaluation quality.
LCEL pipeFewShotCoT
ℹ️ agenta.ai relevance: The most critical patterns for prompt management platforms are structured output (reliable pipeline integration), CoT for complex reasoning nodes, and versioned prompt templates with automated evaluation — mapping directly to LangSmith's commit/tag/playground workflow.
Section 06
LangChain prompt API evolution — legacy vs current 2025
⚠ Legacy (pre-2024)
  • from langchain.prompts import PromptTemplate (root import)
  • Completion-style prompts as default
  • LLMChain + SequentialChain for chaining steps
  • AgentExecutor for ReAct agents
  • FewShotPromptTemplate for string-based few-shot
  • Memory via ConversationBufferMemory
  • No native structured output enforcement
  • Prompts hardcoded inside application code
✓ Current 2025 — LangSmith + LangGraph
  • from langchain_core.prompts import ChatPromptTemplate
  • Chat-style prompts as default (system/human/ai roles)
  • LCEL | pipe operator replaces all chain classes
  • create_react_agent (LangGraph prebuilt) replaces AgentExecutor
  • FewShotChatMessagePromptTemplate for chat-native few-shot
  • MessagesPlaceholder + MemorySaver / SqliteSaver
  • with_structured_output() / bind_tools() as first-class API
  • Prompts versioned in LangSmith Hub (commits, tags, environments)
⚠ LC docs explicitly state: "Unless you have a specific reason to use completion prompts, use chat prompts for new projects." Completion-style PromptTemplate is "maintained primarily for backward compatibility."
Section 07 · Official LangSmith Docs
LangSmith prompt engineering workflow
Prompt templates with dynamic variables
LangSmith treats prompts as templates with {variable} placeholders. F-string format is the default; mustache format is available for conditionals and loops. The template + variable values = final prompt sent to the model. Templates versioned separately from application code.
Commits + tags = prompt version control
Every saved prompt change creates a commit with a unique hash. Tags like staging and production are reserved for environment promotion. Pull specific versions in code: client.pull_prompt("name:commit_hash"). Diff view shows what changed between commits.
client.pull_prompt("my-agent-system:a3f2b1c")
Playground + Polly AI for prompt optimization
The LangSmith Playground lets you test prompts against custom model configs and endpoints. Polly (the AI assistant inside Playground) optimizes prompts, generates tool definitions, and creates output schemas — automating prompt engineering iteration without code changes.
{ }
Tools + structured output as first-class prompt concepts
In LangSmith, tools (name + description + JSON schema of args) and structured output schemas are stored alongside prompt templates. Key distinction: tools = model chooses which to call; structured output = model always responds in that schema — one response, always.
Cross-functional prompt engineering
LangChain docs explicitly state prompt engineering "is often a multi-disciplinary effort — the most effective prompt engineer may be a product manager, domain expert, or other non-technical team member." LangSmith's UI and Hub are designed so non-technical contributors can iterate on prompts without touching code.
Evaluation pipeline integration
LangSmith connects prompt versioning to evaluation datasets and automated test runs. Teams A/B test prompt commits against golden datasets using LLM-as-judge scoring. Failed evals block promotion from staging to production — treating prompts with the same rigor as application code.
A/B testingLLM-as-judgeGate to prod
ℹ️ LangSmith prompt engineering page: docs.langchain.com/langsmith/prompt-engineering — covers creating, versioning, testing, and deploying prompts via UI, SDK, and the Polly AI assistant.