How do you build a prompt template, and what can go wrong when you inject variables into it?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
A template is a prompt with typed slots plus rendering logic. The naive version is an f-string over user input, and it fails in ways that are worth naming.
Full worked answer
A template is a prompt with typed slots plus rendering logic. The naive version is an f-string over user input, and it fails in ways that are worth naming.
Delimiter collision. If your template wraps retrieved text in <document> tags and the retrieved text contains </document>, the model now sees your structure break mid-content. Strip or escape your own delimiters out of slot values before rendering. This is the prompt-layer equivalent of SQL injection, and it is the one that gets missed.
Empty and missing values. A slot that renders to nothing leaves Documents: followed by blank space, and the model invents documents. Handle the empty case explicitly with different copy: "No documents were retrieved. Tell the user you could not find anything." Fail loudly on missing keys rather than rendering None.
Unbounded values. A slot with no length cap is how a 3k-token prompt becomes 180k. Cap every slot and decide the degradation strategy per slot.
Cache boundaries. Interpolating a timestamp or request id near the top invalidates the whole cached prefix. Keep static text contiguous and put volatile slots as late as possible.
def render(tmpl: str, **slots: str) -> str:
safe = {}
for k, v in slots.items():
if v is None:
raise ValueError(f"missing slot: {k}")
v = v.replace("<document>", "<document>")
v = v.replace("</document>", "</document>")
safe[k] = v[:MAX_CHARS[k]]
return tmpl.format(**safe)
Beyond mechanics: keep templates in version control as files, not string literals scattered across handlers, so they are diffable and reviewable. Assert on the rendered prompt in tests, not just on the template, because most "the model ignored my instruction" bugs turn out to be an instruction that never made it into the rendered string.
What a good answer covers
- A template is a prompt with typed slots plus rendering logic
- The naive version is an f-string over user input, and it fails in ways that are worth naming
- If your template wraps retrieved text in <document> tags and the retrieved text contains </document>,…
- Strip or escape your own delimiters out of slot values before rendering
- This is the prompt-layer equivalent of SQL injection, and it is the one that gets missed
Key terms: Delimiter collision · Empty and missing values · Unbounded values · Cache boundaries
Interviewer follow-ups to expect
How would you unit test a prompt template?
Where would you put a per-user personalisation string so it does not destroy your prompt cache?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep