On this page
- The two templates you'll actually use
- Write the template before you write the code
- A copy-ready template pack
- Add few-shot examples without breaking the template
- Things that go wrong with templates
- Stop leaving templates inside your code
- A checklist before you ship a template
- Using the same templates outside LangChain
- Next step
A LangChain prompt template is a prompt with named variables that you fill in at runtime. You write the text once, mark the parts that change with {curly_braces}, and call the template with different inputs. The two classes you'll use most are PromptTemplate for a single string and ChatPromptTemplate for a list of system, human, and AI messages.
The syntax takes five minutes to learn. The harder part is writing templates that hold up, and keeping them from turning into strings scattered across your codebase. This post covers both.
The two templates you'll actually use
PromptTemplate for a single string
Use this when you want one block of text going to a model. It's the simplest form and fine for summaries, classification and extraction.
from langchain_core.prompts import PromptTemplate
template = PromptTemplate.from_template(
"Summarise the following [document_type] in {word_limit} words or fewer. "
"Audience: {audience}.\n\nText:\n{text}"
)
prompt = template.invoke({
"word_limit": 80,
"audience": "a busy operations manager",
"text": meeting_notes,
})LangChain reads the variable names out of the string. If you forget to pass one, it raises an error instead of sending a half-filled prompt to the model. That alone is a good reason to use a template over an f-string.
ChatPromptTemplate for system and user messages
Most real work goes through chat models, so you'll want separate roles. Put the stable instructions in the system message and the changing input in the human message.
from langchain_core.prompts import ChatPromptTemplate
chat_template = ChatPromptTemplate.from_messages([
("system", "You are a support analyst for {product_name}. "
"Answer only from the provided context. "
"If the context doesn't contain the answer, say so."),
("human", "Context:\n{context}\n\nCustomer question: {question}"),
])If you're not sure what belongs in which message, our post on system prompt vs user prompt covers the split. The short version: rules and role go in system, the task and data go in the human turn.
Write the template before you write the code
The template syntax is the easy bit. A weak prompt with tidy variables is still a weak prompt. Draft the text in a chat window first, run it against three or four real inputs, and only then turn the changing parts into variables.
Here's a before and after for a bug triage template.
Before:
Look at this bug report and tell me what to do: {report}The output was a rambling paragraph that was different every time. Nothing downstream could parse it.
After:
You are triaging bug reports for {product_name}.
Read the report below and return exactly these fields:
- severity: one of low, medium, high, critical
- component: the most likely area of the product
- missing_info: what you'd need to reproduce it (or "none")
- next_step: one sentence
If the report is not a bug, set severity to "none" and explain in next_step.
Report:
{report}Same variable, but now the answer has a fixed shape. You can pass it into a parser, and a reviewer can scan it in seconds.
A copy-ready template pack
These three cover most of what people build first. Each one is written as a LangChain-style template {variables}. The same text works pasted into ChatGPT or Claude if you replace the braces with your own content.
TEMPLATE 1: Grounded Q&A
System:
You answer questions about {product_name} using only the context provided.
- Quote or cite the passage you used, labelled by its source id.
- If the context does not answer the question, reply: "I don't have that information."
- Do not use outside knowledge.
Human:
Context:
{context}
Question: {question}
---
TEMPLATE 2: Structured extraction
System:
You extract data from {document_type} documents. Return valid JSON only, matching this shape: {json_shape}. Use null for any field not present in the text. Never guess.
Human:
{document_text}
---
TEMPLATE 3: Rewrite in a house style
System:
You rewrite text to match the style guide below. Keep every fact unchanged. Do not add claims.
Style guide:
{style_guide}
Human:
Rewrite this for {channel}. Target length: {length}.
{draft}Notice the "don't guess" and "keep every fact" lines. Those are the guardrails that stop the model from filling gaps with confident nonsense. They cost you one line each.
Add few-shot examples without breaking the template
When the output format matters, examples beat description. LangChain has FewShotChatMessagePromptTemplate for this, and it keeps the examples as data instead of pasted text.
from langchain_core.prompts import (
ChatPromptTemplate,
FewShotChatMessagePromptTemplate,
)
examples = [
{"input": "App crashes when I upload a PDF", "output": "component: uploads"},
{"input": "Can't reset my password", "output": "component: auth"},
]
example_prompt = ChatPromptTemplate.from_messages([
("human", "{input}"),
("ai", "{output}"),
])
few_shot = FewShotChatMessagePromptTemplate(
examples=examples,
example_prompt=example_prompt,
)
final = ChatPromptTemplate.from_messages([
("system", "Label the component for each bug report."),
few_shot,
("human", "{input}"),
])Two to four good examples are usually enough. More than that and you're spending tokens without much gain, and you risk the model copying surface details from your examples. For the thinking behind this, see few-shot prompting.
Things that go wrong with templates
Literal braces. If your prompt contains JSON, the curly braces collide with variable syntax. Double them (
{{and}}) to print a literal brace. This catches everyone once.Variables doing too much. A
{instructions}variable that holds a whole paragraph written by a user is not a template anymore. It's an open door. Keep variables for data, and keep instructions fixed.Untrusted input in the system message. Retrieved documents and user text belong in the human message or clearly fenced as data. If you drop them into the system role, injected instructions get more authority than they deserve.
No test inputs. A template with no saved examples can't be checked after an edit. Keep three to five representative inputs next to it.
Stop leaving templates inside your code
Once you have more than a handful of templates, the string-in-a-Python-file approach starts to hurt. A product manager wants to tweak the tone and has to open a pull request. Someone changes the extraction prompt, and nobody can say which version produced last week's output. Two services quietly drift into using different copies of the same prompt.
The fix is to treat prompts as versioned assets with an owner, not as code constants. A workable setup:
Draft and test the template in the prompt playground against your sample inputs.
Save it to your prompt library with the variables clearly named in
[square_brackets]or braces, and a one-line note on what it's for.Edit it through prompt version control so every change is recorded and you can roll back when a tweak makes things worse.
Pull the current version into your chain at runtime instead of hard-coding it.
The last step is where the agent platforms integration comes in. PromptPal can deliver versioned prompts and context to LangChain, CrewAI, n8n, or your own code through MCP and the SDK, so a prompt edit doesn't need a redeploy. You fetch the text, then hand it to ChatPromptTemplate.from_messages like any other string.
If you want to see how that fits into a wider setup, the prompt library guide for teams covers naming, ownership, and review habits that stop a library from going stale.
A checklist before you ship a template
Run through this before a template goes into a chain:
Every variable has a clear name (
customer_question, notinput2).The output format is stated explicitly, with a fallback for "can't answer".
Stable rules are in the system message. Untrusted data is in the human message.
There's a "do not guess" or "use only the context" line where facts matter.
Literal JSON braces are escaped.
You've run it on at least three real inputs, including one awkward one.
It's saved somewhere with a version history and a named owner.
Using the same templates outside LangChain
A good template isn't tied to a framework. The grounded Q&A pattern works as a ChatGPT custom instruction, a Claude project prompt or a Gemini system instruction. Model behaviour does differ, so test each one rather than assuming identical output. If you're moving between models, consistent results across Claude, ChatGPT, and Gemini show what tends to shift.
This is also why keeping the prompt text separate from the chain code pays off. When you switch models, you adjust the prompt in one place, compare versions, and leave the pipeline alone.
Next step
Save your first three templates to a shared library so they stop living in scattered files. Start with the PromptPal prompt library, add the variables and a note on each, and version them from the first edit.
Stay Updated
Get the latest blog posts and AI prompt engineering tips delivered to your inbox.




