Prompt
How do I use prompt templates with an LLM API?
Latest observation
Using prompt templates with an LLM API is mostly about creating reusable prompt strings with variables that you fill in at runtime before sending the request.
Basic idea
Instead of writing a full prompt every time:
Summarize the following article in 3 bullet points:
[article text here]
you make a template:
Summarize the following article in 3 bullet points:
{{article}}
Then in code, you replace {{article}} with the real text.
Why use templates?
- Consistency: same structure every time
- Reuse: one prompt pattern for many inputs
- Maintainability: easy to update
- Safety/clarity: keeps instructions separate from user data
Common ways to template prompts
1. Simple string replacement
template = "Translate the following text to French:\n\n{{text}}"
prompt = template.replace("{{text}}", user_text)
2. Python str.format()
template = "Translate the following text to French:\n\n{text}"
prompt = template.format(text=user_text)
Be careful: if user_text contains { or }, you may need to escape them.
3. Jinja2 templates
Good for more complex prompts with conditionals and loops.
from jinja2 import Template
template = Template("""
You are a helpful assistant.
{% if tone %}
Write in a {{ tone }} tone.
{% endif %}
Text:
{{ text }}
""")
prompt = template.render(tone="formal", text=user_text)
Using templates with an LLM API
Most LLM APIs just want the final rendered prompt, or a list of messages.
Example: text completion style
import requests
template = "Classify this review as positive, negative, or neutral:\n\n{{review}}"
prompt = template.replace("{{review}}", review_text)
payload = {
"model": "your-model",
"prompt": prompt
}
response = requests.post("https://api.example.com/v1/completions", json=payload)
print(response.json())
Example: chat-style API
If your API uses system + user messages, you template the user message or both.
system_template = "You are a concise assistant."
user_template = "Summarize this in one sentence:\n\n{{text}}"
messages = [
{"role": "system", "content": system_template},
{"role": "user", "content": user_template.replace("{{text}}", article_text)}
]
Best practices
1. Separate instructions from user input
Keep your instructions in the template and user content as a variable. This helps reduce prompt injection risks.
2. Be explicit
Specify:
- format
- length
- tone
- constraints
- examples if needed
Example:
Answer in JSON with keys: summary, sentiment, topics.
Text: {{text}}
3. Escape user content
If you embed user text directly, make sure it can’t accidentally break your template syntax.
4. Version your templates
Treat prompts like code:
- store them in files
- review changes
- test them
- version them
5. Test with multiple inputs
Try short, long, weird, and adversarial inputs to make sure the template behaves well.
A practical pattern
A common approach is:
- Define a template
- Fill in variables
- Send rendered prompt to the API
- Parse the output
template = """
You are a helpful assistant.
Task: Extract the main idea from the text below in 1 sentence.
Text:
{{text}}
"""
prompt = template.replace("{{text}}", article_text)
# send prompt to LLM API here
If you want, I can also show you:
- a Python example with OpenAI-style chat messages
- a Jinja2-based template system
- or a production-ready prompt template structure