📘 Introduction

When you build LLM applications, you usually do not want to write a completely new prompt every time. You want a reusable structure where only the inputs change. LangChain prompt templates help you do exactly that.

In this tutorial, you will learn what prompt templates are, why they are useful, and how to build reusable AI prompts with Python and LangChain. We will start with a simple template, then connect it to a chat model, and finally build a more flexible template with multiple variables.

💡
Prompt templates are one of the most useful building blocks in LangChain because they turn hardcoded prompt text into reusable application logic.

💡 What are LangChain prompt templates?

A prompt template is a reusable prompt with placeholders. Instead of writing one fixed prompt, you define a template once and fill it with values at runtime.

For chat models, LangChain commonly uses ChatPromptTemplate. It lets you create message-based prompts, such as a system message for the assistant behavior and a human message for the user request.

Without a templateWith a prompt template
You rewrite prompt text manually.You reuse one prompt structure with variables.
Prompts become harder to maintain.Prompts stay consistent across your app.
Changing behavior means editing many strings.Changing behavior means updating one template.

🧠 Why prompt templates matter

Prompt templates are especially useful when your application asks similar questions with different inputs. For example, you may want to summarize different topics, generate learning plans for different audiences, or create product descriptions in different tones.

In a real LLM app, this helps keep your prompts clean, testable, and easier to improve over time.

A good prompt template separates the fixed instruction from the changing user input. That makes your app easier to debug and extend.

✅ Prerequisites

Before getting started, make sure you have:

☑️ Python installed
☑️ Basic Python knowledge
☑️ An OpenAI API key or another supported chat model provider
☑️ A terminal or command prompt

⚙️1️⃣ Set up the project folder

Create a new local project folder for this tutorial:

mkdir langchain-prompt-templates
cd langchain-prompt-templates

🐍2️⃣ Create a virtual environment

Create and activate a virtual environment:

python -m venv .venv
source .venv/bin/activate

On Windows, activate it with:

.venv\Scripts\activate

📦3️⃣ Install libraries

Install the LangChain packages we need for this tutorial:

pip install langchain-core langchain-openai

🔐4️⃣ Set your API key

If you use OpenAI, set your API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.

export OPENAI_API_KEY="your_api_key_here"

On Windows PowerShell, use:

$env:OPENAI_API_KEY="your_api_key_here"
🔐
Do not paste private API keys into your code, screenshots, Git commits, or blog content. Use placeholders in examples.

🧩5️⃣ Create your first prompt template

Create a file named prompt_templates_demo.py and add the following code. This first example creates a reusable template and fills the topic variable.

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI teacher."),
    ("human", "Explain {topic} in simple beginner-friendly language."),
])

formatted_prompt = prompt.invoke({"topic": "prompt templates"})

print(formatted_prompt)

prompt_templates_demo.py

Run the script:

python prompt_templates_demo.py

You should see a formatted chat prompt. The exact output may look different depending on your package version, but the important part is that LangChain turned the template into chat messages.

🎓
Want to continue with the full implementation? In the Academy section, we connect this prompt template to a chat model, build a multi-variable template, and create a reusable mini workflow.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In