Glossary

Prompt template

A reusable prompt with fixed instructions and named placeholders that an application fills with variables, such as user input or retrieved context, before each model call.

A prompt template is a reusable prompt in which the fixed parts (instructions, format rules, examples) are written once and the changing parts are left as named placeholders, such as {question}, {context} or {user_name}. At runtime the application fills each placeholder with a value, producing the finished prompt that is sent to the model. Templates are how most production applications talk to language models: instead of writing a prompt by hand for every request, a developer writes one template per task and lets code supply the variables. That separation makes prompts testable, versionable and consistent across thousands of calls, and it gives a clear place to control what context each request carries. The template itself is simple text with gaps. Most of the engineering effort goes into deciding what fills those gaps and how much of it to include.

How a prompt template works

A template has two kinds of content. The static part is the same on every call: the role the model plays, the task, the rules it must follow, the output format and sometimes a handful of examples. The dynamic part is a set of variables the application resolves at request time. Rendering the template means substituting each variable with its value and producing a string, or, for chat models, a list of messages with roles such as system, user and assistant.

A support assistant might use a template like this: a system prompt section that says the assistant answers questions about a billing product, a rule that it must cite the help article it used, a slot called {customer_facts}, a slot called {retrieved_articles}, and finally the user’s {question}. The template never changes between customers. What changes is the content of the three slots, which the application gathers from its own systems just before calling the LLM.

Template syntax varies by tool. Python developers often use f-strings or Jinja, which adds loops and conditionals so a template can include a section only when its data exists. Frameworks such as LangChain ship their own template classes for text and chat prompts, which is one of the abstractions teams weigh when they look at LangChain alternatives. TypeScript applications commonly use template literals or a small rendering function. The syntax matters less than the discipline: every variable is named, every slot has a known source, and the finished prompt can be logged and inspected.

What goes into the slots

The placeholders in a template are where context engineering happens. A well-designed template usually has slots for some mix of the following.

Slot

Typical source

What to watch

User input

The current message or form field

Untrusted text; keep it clearly separated from instructions

Retrieved documents

A search index or retriever

Relevance, length and citations

User facts and preferences

A memory store or profile

Freshness; facts change over time

Conversation history

The session log

Grows with every turn and inflates cost

Examples

A curated set, sometimes chosen per request

Must match the current task

Tool results

Function or API calls made earlier

Size and format of the payload

Each slot has a budget, whether or not anyone wrote it down. The context window is finite, and every token in a slot is paid for on every call. A template that drops the whole conversation history and the top ten search results into its slots will work in a demo and become slow and expensive in production. Research on long inputs also shows that models use information less reliably as inputs grow, a pattern described as context rot, and that facts placed in the middle of a long prompt are the most likely to be missed, known as lost in the middle.

So the useful question for each slot is not only where its content comes from but how little of it the model needs. Precise retrieval, such as sub-document retrieval that returns the passage containing the answer rather than a whole chunk, keeps the {retrieved_articles} slot small. Structured memory keeps the {customer_facts} slot to a few relevant facts instead of the full chat log. The breakdown in where your tokens go shows how quickly unmanaged slots add up, and a context budget template gives a way to assign limits per slot.

Prompt templates and related techniques

A template is a container, and most prompting techniques live inside one. Few-shot prompting puts worked examples in a fixed or variable slot; zero-shot prompting leaves them out and relies on instructions alone. Both depend on in-context learning, the model’s ability to pick up a task from what is in the prompt. A chain of thought instruction is just another line in the static part.

Templates also sit at the joints of multi-step systems. In prompt chaining, the output of one template becomes a variable in the next: a classification step fills a {category} slot that decides which downstream template runs. When the output needs to be machine-readable, the template carries the format rules and pairs with structured output or a JSON schema, so the response can be parsed without guesswork.

The broader discipline is prompt engineering, which covers how the static instructions are worded. Context engineering covers what fills the variables. The two meet in the template, and the difference is set out in context engineering vs prompt engineering. In agent systems the template is often assembled from parts at runtime, with instructions, tool definitions for function calling and reusable agent skills added only when the current step needs them.

Designing templates that hold up in production

Put stable content first. Providers that support prompt caching can reuse the processed form of an identical prefix across calls. Placing the instructions, format rules and fixed examples at the top, and the variables after them, makes the most of that discount. Caching only helps the unchanging part, though, which is why prompt caching won’t save you if the variable slots are the bulk of the prompt.

Delimit every variable. Wrap each slot in clear markers, such as XML-style tags or labelled sections, so the model can tell instructions from data. Formats matter here too: Markdown headings are easy for models to follow, while JSON suits structured records. The trade-offs are covered in JSON vs Markdown for LLMs.

Handle empty slots. A slot with nothing in it should disappear cleanly or say so explicitly (“No relevant documents were found”). Leaving a heading with no content invites the model to invent some, which is one route to hallucination.

Version templates like code. Keep them in source control, give each one an identifier and log which version produced each response. When a model update changes behaviour, you need to know whether the template or the model moved. LLM observability tools record the rendered prompt alongside the response, and an LLM gateway gives one place to attach template versions to traffic.

Test changes against fixed cases. A small golden dataset of inputs with expected outputs, scored by LLM evaluation methods, catches regressions before a template change ships. Fix the temperature low during tests so differences come from the template rather than sampling.

Security: templates and untrusted input

Filling a template is string concatenation, and that is where prompt injection gets in. If a user types “ignore the instructions above” into the {question} slot, the model sees that text in the same prompt as your rules. Retrieved documents and web pages can carry the same kind of instruction, which is indirect prompt injection. Delimiters and an instruction to treat slot content as data reduce the risk but do not remove it. Real controls belong in code: what tools the model may call, what data each request can reach, and what actions need confirmation. A template is a formatting layer and cannot enforce policy.

A worked example

A team runs a support agent for an accounting product. Its first template stuffs the last 30 messages of the conversation and the top eight help-centre chunks into the prompt, around 14,000 input tokens per request. Answers are mostly right, but the agent sometimes quotes the free-plan limits to customers who upgraded weeks ago, because the upgrade was mentioned once in an old conversation and the stale plan details appear in several retrieved chunks.

The team rewrites the template with four slots: {customer_facts}, {relevant_passages}, {recent_turns} and {question}. Customer facts now come from a memory store that keeps the current plan as one fact, superseding the old one. Relevant passages come from a search that returns the specific paragraph, each with its page reference for source attribution. Recent turns are limited to the last four messages. The static instructions move to the top so they can be cached. Input drops to under 3,000 tokens, and the wrong-plan answers stop because the template no longer contains contradictory facts.

Nothing about the model or the instructions changed. The improvement came from what the template’s variables were filled with, which is the same pattern described in your LLM bill is a retrieval problem and in the customer support agents use case.

Where Exabase fits

Exabase does not store or render prompt templates. Your templates live in your code, in whatever syntax you prefer. Exabase supplies the content for the context slots, which is usually where a template’s size and accuracy are decided.

Exabase Memory fills slots such as {customer_facts}. Send conversations in, and the M-1 engine extracts facts, preferences and events, resolving contradictions as they change, so a user who upgrades has one current plan. Before each call, POST /v2/memories/search returns the memories relevant to the current query, ranked and ready to drop into the template, with optional query expansion and reranking. If you would rather skip assembly for a simple lookup, ask memories returns an answer directly. The guide to retrieving memories covers the parameters.

Deep Search fills slots such as {relevant_passages}. It searches inside stored documents, audio and video at the sub-document level and returns matched chunks with page numbers or timestamps, and its precision parameter controls how many results come back. See searching resources for the request format. Together, these replace full history and broad top-K retrieval with a few hundred precise tokens. The reduce token costs solution describes an illustrative request falling from about 15,100 input tokens to about 2,800, and the token cost calculator estimates savings for your own numbers. For answer quality, the RAG accuracy solution explains why retrieving the passage that contains the answer matters more than tuning the template’s wording.

Frequently asked questions

What is the difference between a prompt and a prompt template?

A prompt is the finished text sent to the model. A prompt template is the reusable pattern with placeholders that produces prompts once its variables are filled.

Is a system prompt a prompt template?

Often it is part of one. The system prompt is the instruction section, and in many applications it contains variables such as the user’s name or today’s date that are filled at runtime.

What syntax do prompt templates use?

Any string templating works. Common choices are Python f-strings, Jinja, TypeScript template literals and the template classes in agent frameworks.

How long should a prompt template be?

As short as the task allows. The static part is usually small; the cost comes from the variable slots, so set a budget for each and fill it with precise context. Input vs output tokens explains how each side is billed.

Do prompt templates prevent prompt injection?

No. Delimiters help the model separate instructions from data, but user input and retrieved content can still contain instructions. Enforce permissions and limits in code.

Should prompt templates be versioned?

Yes. Store them in source control, log the version used for each request and run a fixed test set before shipping a change.

Can Exabase fill prompt template variables?

Yes, for context slots. Memory returns relevant user facts and Deep Search returns relevant passages with citations; your code inserts them into the template.

Related pages: prompt engineering, system prompt, prompt chaining, few-shot prompting, context engineering, prompt caching, prompt injection, reduce token costs, context budget template

Cut your token spend and give your agent precise context.

Get started in minutes.

Cut your token spend and give your agent precise context.

Get started in minutes.