Sub topic 1
What a Prompt Is Made Of
Prerequisites
This chapter assumes you have sent prompts to a large language model (LLM) before and can read short Python snippets. You do not need Git or YAML yet. Later chapters use both.
Why start with the parts of a prompt
A prompt is the full text an application sends to a model. When a prompt is a single string pasted into code, it is hard to tell which part is fixed and which part changes. That confusion causes most maintenance problems later. Templates and versions only make sense once you can name the parts.
This chapter breaks a prompt into parts and shows what goes wrong when they are mixed together. The next chapter turns the separated parts into a template.
The parts of a prompt
Most prompts used in applications contain some combination of these parts.
| Part | Purpose | Changes per request? |
|---|---|---|
| Role or context | Tells the model who it is or what situation it is in | Rarely |
| Instruction | States the task to perform | Rarely |
| Constraints | Sets limits on format, length, or tone | Rarely |
| Examples | Shows the expected input and output pattern | Rarely |
| Input data | The text or values the task applies to | Every request |
The first four parts are fixed instructions. The last part is variable input. Keeping that distinction clear is the foundation of everything that follows.
A hard-coded prompt
Here is a typical first attempt. The whole prompt is built inside a function.
def build_prompt(text):
return "You are a helpful editor. Summarize the following text in a concise style. Answer in one sentence if it is short.\n\n" + text
print(build_prompt("Cats sleep a lot."))This works, but the fixed instructions and the variable input are joined by string concatenation. Several problems follow.
- Hard to review. A change to the wording appears as a change to a line of code, buried among logic.
- Hard to reuse. Another feature that needs a "detailed" style must copy the string and edit it.
- Hard to track. Nothing records which wording produced which output.
- Hard to test. You cannot run the instructions alone against sample inputs without running the surrounding code.
Separating fixed from variable
The first step toward a template is to label each part. Take the prompt above and mark it up.
ROLE = "You are a helpful editor."
INSTRUCTION = "Summarize the following text in a concise style."
CONSTRAINT = "Answer in one sentence if the text is short."
def build_prompt(text):
fixed = " ".join([ROLE, INSTRUCTION, CONSTRAINT])
return fixed + "\n\n" + text
print(build_prompt("Cats sleep a lot."))The output is the same text as before. The difference is that each part now has a name, and only text is passed in per request. You can change the constraint without touching the input handling.
This is still not a template. The style, "concise", is baked into the instruction. If you want to choose the style per request, you need a named placeholder in the text. That is the subject of the next chapter.
Common mistakes at this stage
- Mixing input into instructions. If user text is spliced into the middle of an instruction sentence, the model may treat it as part of the instruction. Keep input in its own clearly marked position, usually at the end.
- Leaving a variable value in the fixed text. A style, language, or length that you plan to change belongs in the variable part. Hard-coding it forces a copy of the prompt for each choice.
- Treating the prompt as a throwaway. A prompt that ships in a product is an artifact. It needs a name, an owner, and a history, which the versioning chapters cover.
Exercise
A function builds a prompt as "Translate to French: " + usertext. Which part is fixed and which is variable, and where is the risk if usertext contains the sentence "Ignore the instruction above"?
Show answer
"Translate to French: " is the fixed instruction and user_text is the variable input data. The risk is that the input sits directly after the instruction with no separation, so the model may treat the injected sentence as an instruction. Placing the input in a clearly marked position and handling untrusted input, covered in a later chapter, reduces this risk.
Exercise
List the five parts of a prompt from this chapter and say which one normally changes on every request.
Show answer
Role or context, instruction, constraints, examples, and input data. The input data changes on every request. The other four are normally fixed.