Skip to main content
PromptPalPromptPal
Pricing
Sign inGet started free
  1. Home
  2. /
  3. Blog
  4. /
  5. General

LangChain Prompt Templates: How to Write and Reuse Them

LangChain prompt templates turn a prompt with variables into a reusable object. Here's how to write them, with copy-ready examples and a way to keep them versioned.

Author: PromptPal TeamNovember 20, 2026Updated November 21, 20266 min read
blog-4
On this page
  1. The two templates you'll actually use
  2. Write the template before you write the code
  3. A copy-ready template pack
  4. Add few-shot examples without breaking the template
  5. Things that go wrong with templates
  6. Stop leaving templates inside your code
  7. A checklist before you ship a template
  8. Using the same templates outside LangChain
  9. 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.

Prompt template
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.

Prompt template
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:

Prompt template
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:

Prompt template
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.

Prompt template
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.

Prompt template
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:

  1. Draft and test the template in the prompt playground against your sample inputs.

  2. 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.

  3. Edit it through prompt version control so every change is recorded and you can roll back when a tweak makes things worse.

  4. 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, not input2).

  • 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.

Tags:langchainprompt templatesprompt managementdevelopersprompt versioning

Read this article as Markdown

Stay Updated

Get the latest blog posts and AI prompt engineering tips delivered to your inbox.

No spam, unsubscribe at any time. We respect your privacy.

On this page

  1. The two templates you'll actually use
  2. Write the template before you write the code
  3. A copy-ready template pack
  4. Add few-shot examples without breaking the template
  5. Things that go wrong with templates
  6. Stop leaving templates inside your code
  7. A checklist before you ship a template
  8. Using the same templates outside LangChain
  9. Next step

Turn ideas into prompts that work

Describe what you need and PromptPal writes a reusable prompt in your style for ChatGPT, Claude and Gemini.

Try PromptPal free

Related Posts

Continue reading

General · November 21, 2026

Codex System Prompt: What It Is and How to Customize It

The Codex system prompt is built in, but you control the instructions layered on top. Here is where each one goes, with copy-ready AGENTS.md and task prompts.

General · November 21, 2026

Claude Code Prompt Library: 5 Prompts to Save and Reuse

Build a Claude Code prompt library you actually reuse: five copy-ready prompts for planning, bug fixes, refactors and reviews, plus how to store and version them.

General · November 21, 2026

Prompt Engineering Job: What It Is and How to Get One

What a prompt engineering job involves day to day, which skills get you hired, and how to build a portfolio of tested, versioned prompts that proves you can do it.

Ready to get started?

Join thousands of developers and teams using PromptPal to build better AI prompts.

Start for freeContact Sales

Footer

Get better at AI, one email a week

One great prompt, one quick tip, and what’s new in ChatGPT, Claude and Midjourney. Five minutes, every Thursday.

No spam. Unsubscribe in one click.

PromptPalPromptPal

Great prompts for any AI, in seconds. Free to start, no account needed.

Create a prompt
Browser extensionConnect to Claude, ChatGPT, Cursor
Featured on SaaSearch

Product

  • Prompt builder
  • Browser extensionNew
  • Connect to your AI
  • Pricing

Prompt generators

  • ChatGPT
  • Claude
  • Gemini
  • Midjourney
  • Perplexity
  • See all

Use cases

  • Students
  • Writers
  • Solo builders
  • Everyday users
  • Developers
  • AI teams

Resources

  • Guides
  • Blog
  • Prompt Store
  • Docs
  • Compare
  • Help center

Company

  • About
  • Contact
  • Enterprise
  • Press

© 2026 Securestack Technologies Inc.

PrivacyTermsRefund policyChangelog

PromptPal