Prompt Template
Prompt parametrizado
Escrito por Darjus Kazakevic · revisado el
A prompt built by joining strings invites injection.
Prompts often start as a string next to the API call. Then they grow a few holes for values, and later they are assembled across three files. Soon nobody can read the prompt that is actually sent, and user input is joined into the sentence that gives the orders. That is the prompt version of building SQL by joining strings.
A template fixes this. It is one named artifact with the fixed instructions, typed parameters for everything that varies, and one function that puts data inside delimited blocks. It is the dull pattern in this category, and the one that makes review, versioning and evals possible.
En esencia — Keep the instructions in one reviewed, named template with typed holes, and put data into its own block, never into the sentence that gives the orders.
Cuándo recurrir a él
Recurre a él cuando
- Any prompt with variable data, which is almost all of them
- User text or retrieved text reaches the prompt
- Several call sites, or several people editing the prompt
Reconsidéralo cuando
- A one-off script with a constant prompt
- A two-line prompt with one hole, where the indirection outweighs it
- Templating so heavy that nobody can read the final prompt
The parts
- The template
- One named, reviewed piece of text with everything fixed: framing, contract, examples, delimiters. It is what you version, diff and test. The call site only supplies values.
- The hole
- A typed parameter, not a free string join. Knowing that ticket is untrusted text and maxWords is a number lets the renderer escape one and inline the other.
- The renderer
- The one function that turns template plus values into the final string. Everything that must never be skipped lives here: escaping, truncation, block order. No call site can forget it.
Reglas de diseño
- Never put data in an instruction sentence
- "Summarize this {style} document" lets a value change your orders. Put the value in its own block and let the instruction refer to the block by name, like binding a SQL parameter.
- Escape what the delimiters use
- If a value can contain </ticket>, it can end the block early, and the rest reads as instructions. The symptom is a strange change in behaviour, not an error, so it is hard to find.
- Type the holes
- Typed parameters turn "someone forgot the category list" into a compile error. Without them you send a prompt with a blank where the categories should be, and the model fills it in.
- Log the rendered prompt
- Log the final string, redacted where needed, not the template and the values separately. It is the only record that explains the answer you got.
Errores habituales
- User input joined into the instruction sentence
- Prompts kept in a database and edited live, with no review or rollback
- Logging the template and the values instead of the rendered prompt