0
LLM Engineering & APIs
Phase 2Module 6 of 12

Prompt Templates

Hardcoding prompts as strings scattered across code is unmaintainable. Templates let you version, test, and swap prompts independently — like SQL parameterized queries but for LLM instructions.

A mail merge template: 'Dear {name}, your order {order_id} ships on {date}.' The structure stays fixed; variables change per request. Prompt templates work the same way for LLM inputs.

Visual Workflows

Start here — study each diagram, then use + / to zoom if needed.

100%
Loading diagram...

Scroll inside the frame · use + / − to zoom

Key Takeaways

  • 1.Prompt templates are reusable, parameterized structures for LLM inputs — separating prompt engineering from application code and enabling consistent, maintainable AI interactions. Template structure: system message (persona/rules) + user message (task with {variables}).
  • 2.Tools: LangChain PromptTemplate, Jinja2 strings, LangSmith prompt hub. Best practices: separate system from user prompts, use few-shot examples as template sections, version prompts in git, A/B test variants.
  • 3.Chat templates: models expect specific formats (Llama [INST], ChatML, etc.) — use tokenizer.apply_chat_template(). Variables: {context}, {question}, {format_instructions}.
  • 4.Composition: chain templates for multi-step pipelines.

Real Example

Scenario

RAG template: system='Answer based only on context. Cite sources.' user='Context: {context}\n\nQuestion: {question}'. Code fills context from vector DB and question from user input. Swap template to change behavior without code changes.

What you would do

In LLM Engineering & APIs, apply Prompt Templates to this scenario: RAG template: system='Answer based only on context. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Open the Code Walkthrough below and run it locally. Change one parameter related to Prompt Templates (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Prompt Templates happens in the code.

Prompt Templates
1from langchain_core.prompts import ChatPromptTemplate  # import dependencies2
3# Define reusable template4rag_template = ChatPromptTemplate.from_messages([  # key line for Prompt Templates5    ("system", "Answer using ONLY the provided context. If unsure, say 'I don't know'."),6    ("human", "Context:\n{context}\n\nQuestion: {question}"),7])8
9# Fill variables at runtime10prompt = rag_template.invoke({  # key line for Prompt Templates11    "context": "Paris is the capital of France. Population: 2.1M.",12    "question": "What is the population of Paris?",13})14
15# Use with any LLM16# response = llm.invoke(prompt)17
18# Jinja2 alternative19from jinja2 import Template  # import dependencies20tpl = Template("Summarize in {{ style }} style:\n\n{{ text }}")21rendered = tpl.render(style="bullet-point", text="Long article here...")

Cheat Sheet

Quick recap

quick ref
  • ChatPromptTemplate.from_messages()
  • {variable} injection at runtime
  • System + human message pairs
  • apply_chat_template() for formatting
  • Version prompts in git
  • A/B test with eval metrics

Common Mistakes

  • Hardcoding prompts in business logic — hard to iterate
  • Not using chat templates — wrong format for model
  • No versioning — can't roll back bad prompt changes
  • Overly complex templates — hard to debug filled output