Module 8 cheat sheet: Prompt Engineering

Clear prompts, reasoning techniques and structured output make LLM behaviour predictable; LangChain turns them into reusable chains.

All modules📖 Glossary

Six elements of a good prompt

ElementExample
Role“You are a support agent for a bank.”
Task“Classify this complaint.”
ContextCustomer type, product, policy text
Format“Reply as JSON with keys …”
Constraints“Max 50 words. Never share account numbers.”
Examples2–3 input → output pairs

S26 →

Key ideas

  • Lasting rules go in the system message; the request goes in the user message. S26 →
  • Start zero-shot; add few-shot examples for custom labels and formats. S26 →
  • Chain-of-thought for multi-step problems; self-consistency = ask several times and vote. S27 →
  • Reasoning models already think step by step: give clear goals, not step lists. S27 →
  • Always validate structured output (Pydantic) and retry on failure. S28 →
  • LLMs are stateless: memory = re-sending chosen history. S30 →

Getting structured output

MethodGuarantees
JSON modeValid JSON, but not your fields
Schema / structured outputsYour exact fields and types
Tool (function) callingArguments that match the tool schema
Pydantic + retryYour code checks and asks again if wrong

Working with many documents

ChainHowGood for
StuffAll text in one promptFew short documents
Map-reduceSummarise each in parallel, then combineMany documents
RefineUpdate the answer document by documentOrder matters
Map-rerankAnswer from each, keep the best scoredOne best source

S30 →

Code: an LCEL chain

# pip install langchain-core langchain-openai   (needs OPENAI_API_KEY)
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful banking tutor."),
    ("user", "Explain {topic} in one line.")])
chain = prompt | ChatOpenAI(model="gpt-4o-mini", temperature=0) | StrOutputParser()
print(chain.invoke({"topic": "UPI"}))          # also: .batch([...]) and .stream(...)

Common mistakes

  • Vague prompts (“make it better”) instead of a clear task, format and limit.
  • Putting permanent rules only in the user message, where users can override them.
  • Sending model JSON straight into your system without validation.
  • Re-sending the whole chat history forever: cost and latency keep growing.

Created by Ajeetkumar · EasyLearnSpace