Module 8 cheat sheet: Prompt Engineering
Clear prompts, reasoning techniques and structured output make LLM behaviour predictable; LangChain turns them into reusable chains.
Six elements of a good prompt
| Element | Example |
|---|
| Role | “You are a support agent for a bank.” |
| Task | “Classify this complaint.” |
| Context | Customer type, product, policy text |
| Format | “Reply as JSON with keys …” |
| Constraints | “Max 50 words. Never share account numbers.” |
| Examples | 2–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
| Method | Guarantees |
|---|
| JSON mode | Valid JSON, but not your fields |
| Schema / structured outputs | Your exact fields and types |
| Tool (function) calling | Arguments that match the tool schema |
| Pydantic + retry | Your code checks and asks again if wrong |
Working with many documents
| Chain | How | Good for |
|---|
| Stuff | All text in one prompt | Few short documents |
| Map-reduce | Summarise each in parallel, then combine | Many documents |
| Refine | Update the answer document by document | Order matters |
| Map-rerank | Answer from each, keep the best scored | One 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