Prompt

How do I use prompt templates with an LLM API?

Artificial Intelligence / AI Platforms2 observationsLast seen Nov 26, 2026

Latest observation

Nov 26, 2026OpenAI APIWeb search: off

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:

  1. Define a template
  2. Fill in variables
  3. Send rendered prompt to the API
  4. 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

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (2 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.