> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Prompt Templates

> Explains LangChain prompt templates for system human and AI messages, showing usage patterns, Python example, best practices, and use cases for building reusable chat prompts.

Static messages work, but they’re not flexible or reusable across an application. Prompt templates let you parameterize system, human, or AI messages with placeholders that are filled in at runtime. This makes prompts easier to manage, share, and maintain—especially as your LLM-based application grows.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Xqjckn2TzkOV2Gz2/images/LangChain/Interacting-with-LLMs/Prompt-Templates/prompt-templates-and-messages-diagram.jpg?fit=max&auto=format&n=Xqjckn2TzkOV2Gz2&q=85&s=540e2844f646106c4b2ff1fb17a5f034" alt="The image shows a diagram labeled &#x22;Prompt Templates and Prompts&#x22; with rounded rectangles representing different prompts and a legend indicating message types: System Message, Human Message, and AI Message. It notes the attributes &#x22;Helpful&#x22; and &#x22;Flexible.&#x22;" width="1920" height="1080" data-path="images/LangChain/Interacting-with-LLMs/Prompt-Templates/prompt-templates-and-messages-diagram.jpg" />
</Frame>

In practice, you define templates for each role (system, human, AI) and then combine them to build the final messages sent to your model. A common pattern is to use a system template to set behavior and constraints, and a human template to include the dynamic content or user input.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Xqjckn2TzkOV2Gz2/images/LangChain/Interacting-with-LLMs/Prompt-Templates/system-human-prompt-templates-diagram.jpg?fit=max&auto=format&n=Xqjckn2TzkOV2Gz2&q=85&s=27c6835dec0f16ee647f197e6c3447eb" alt="The image shows two sections titled &#x22;System Message Prompt Template&#x22; and &#x22;Human Message Prompt Template,&#x22; each with colored rectangular prompts. The system template is in blue, and the human template is in green." width="1920" height="1080" data-path="images/LangChain/Interacting-with-LLMs/Prompt-Templates/system-human-prompt-templates-diagram.jpg" />
</Frame>

The model’s reply is returned as an AI message. While you can’t control the exact content the model generates, using an AI prompt template or output-parsing logic can help standardize expected formatting and extract structured results.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Xqjckn2TzkOV2Gz2/images/LangChain/Interacting-with-LLMs/Prompt-Templates/application-ai-message-chat-model-flow.jpg?fit=max&auto=format&n=Xqjckn2TzkOV2Gz2&q=85&s=891d36d6ce85fa8d221fdfdcc3147a62" alt="The image illustrates a process flow from an &#x22;Application&#x22; to an &#x22;AI Message&#x22; and finally to a &#x22;Chat Model,&#x22; associated with an &#x22;AI Message Prompt Template&#x22; concept." width="1920" height="1080" data-path="images/LangChain/Interacting-with-LLMs/Prompt-Templates/application-ai-message-chat-model-flow.jpg" />
</Frame>

## Quick Python example

Below is a concise example showing how to compose chat-style prompt templates using LangChain. This demonstrates a `SystemMessagePromptTemplate` and a `HumanMessagePromptTemplate` with a placeholder called `text`. When formatted, the placeholder is filled and converted to messages that you can pass to a chat model.

```python theme={null}
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

system = SystemMessagePromptTemplate.from_template(
    "You are a helpful assistant that summarizes text concisely."
)

human = HumanMessagePromptTemplate.from_template(
    "Summarize the following text in one paragraph:\n\n{text}"
)

chat_prompt = ChatPromptTemplate.from_messages([system, human])

# Populate the placeholder when invoking:
prompt = chat_prompt.format_prompt(text="LangChain connects components to build LLM apps.")
messages = prompt.to_messages()  # messages can be sent to a ChatModel/LLM
```

<Callout icon="lightbulb" color="#1CB2FE">
  Use prompt templates to keep prompts consistent, enforce style or constraints, and share reusable patterns across your application or organization.
</Callout>

## Prompt template types and common uses

| Template Type  | Purpose                                                   | Example / Notes                                                                   |
| -------------- | --------------------------------------------------------- | --------------------------------------------------------------------------------- |
| System message | Set assistant role, tone, and constraints                 | Use `SystemMessagePromptTemplate.from_template("You are a helpful assistant...")` |
| Human message  | Contains user-facing prompt text and dynamic placeholders | Use `HumanMessagePromptTemplate.from_template("Summarize:\n\n{text}")`            |
| AI message     | Template or parsing expectations for model outputs        | Use for expected format or to attach output parsing logic                         |

## Best practices

* Keep system templates focused on role, constraints, and safety guardrails.
* Use human templates for dynamic content and user data; validate or sanitize inputs before formatting.
* Create small, composable templates for reuse (e.g., short/concise vs. long/detailed personas).
* Manage organization-wide templates in a central repo or configuration to maintain consistent model behavior.
* Combine templates with output-parsing tools or schema validation when you need structured data from responses.

## Example use cases

* Summarization: system sets tone, human provides target text.
* QA over documents: system enforces source citation, human supplies query and document context.
* Multi-persona assistants: swap system templates to change assistant behavior without editing application logic.

## Links and references

* [LangChain Documentation](https://langchain.readthedocs.io/)
* [Kubernetes Basics](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/) (general reference)
* [Docker Hub](https://hub.docker.com/) (general reference)

Next, we’ll walk through demos showing how to construct these prompt templates and use them inside chains.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/ae260750-791b-496c-991f-0d0333f61e40/lesson/4c4472e9-fd82-4691-860a-55d9fad2f7f5" />
</CardGroup>
