Sub topic 1
What a Prompt Is Made Of
Before you can template or version a prompt, you need a clear picture of its parts. This chapter breaks a prompt into components, separates the parts that stay the same from the parts that change on every call, and shows why prompts written as inline strings become hard to maintain. Later chapters build templates, storage, and versioning on this model.
Prerequisites
This chapter assumes you have:
- Sent prompts to a large language model (LLM), through a chat interface or an API.
- Basic Python: functions, dictionaries, lists, f-strings.
- Familiarity with Git and plain text files.
- Basic understanding of JSON or YAML.
You do not need any prompt-management tooling. Everything here uses the Python standard library.
The Parts of a Prompt
A prompt is the full input the model receives before it generates a response. In practice it is rarely one sentence typed by a user. Production prompts are assembled from several components, each with a distinct job.
| Component | Purpose | Usually fixed or variable |
|---|---|---|
| Instruction | Says what task to perform | Fixed |
| Role or persona | Sets perspective, tone, or expertise | Fixed |
| Context | Background the model needs, such as documents or user data | Variable |
| Input | The specific item to process, such as the text to summarize | Variable |
| Examples | Sample inputs and outputs that show the expected pattern | Fixed, sometimes selected per call |
| Constraints | Limits on length, scope, or behavior | Fixed |
| Output format | Describes the shape of the answer, such as JSON with named keys | Fixed |
Not every prompt uses all seven. A simple summarizer may need only an instruction and an input. A support assistant may use all of them.
Instruction
The instruction states the task. Clear instructions use a specific verb and name the object: "Summarize the following text" works better than "Here is some text."
Role
A role line such as "You are a careful technical editor" shifts tone and vocabulary. It does not add knowledge the model lacks. Treat it as a style control, not a capability switch.
Context and input
These two are easy to confuse. Context is background that helps the model decide how to answer, such as a product policy or a retrieved document. Input is the thing the task operates on, such as the email to classify. Keeping them separate matters later, because input often comes from users and needs different handling than context you wrote yourself.
Examples
Examples, often called few-shot examples, show the model the pattern you want. They cost tokens on every call, and they strongly influence the output. A changed example is a behavior change, so it belongs in the version history just as much as a changed instruction does.
Constraints and output format
Constraints say what to avoid or limit: "Use at most three sentences." "Do not speculate beyond the text." Output format says how to shape the answer: "Respond with a JSON object with the keys summary and confidence." If your code parses the response, the output format is a contract between the prompt and your parser. Changing it can break downstream code even when the model's answer still reads well to a human.
Messages and Roles in Chat APIs
Most current model APIs do not take one string. They take a list of messages, and each message has a role. The names vary by provider, but the pattern is common:
- A system message holds standing instructions that apply to the whole conversation.
- A user message holds what the user or application is asking right now.
- An assistant message holds earlier model replies, which you include when you want to carry over conversation history or provide examples.
This changes what "the prompt" means. The prompt is the whole list of messages plus the model settings, not only the user text. When you later version a prompt, you have to decide which messages are part of the versioned artifact. A good default is that the system message and any fixed examples are versioned, while the final user message is rendered from variables at call time.
import json
def build_messages(style, text):
system = (
"You are a careful technical editor. "
"Answer in at most three sentences. "
"Do not add facts that are not in the text."
)
user = f"Summarize the following text in {style} style:\n\n{text}"
return [
{"role": "system", "content": system},
{"role": "user", "content": user},
]
messages = build_messages("concise", "Prompt templates separate fixed instructions from variable input.")
print(json.dumps(messages, indent=2))Running this prints a two-element list. The system message is identical on every call. Only the style and text values change the user message.
Fixed Parts and Variable Parts
The most useful mental split is between what is fixed and what varies per call.
- Fixed parts are written by the prompt author and change only when someone deliberately edits them. Instruction, role, constraints, output format, and static examples are fixed.
- Variable parts arrive at runtime. The input text, retrieved context, user name, and requested style are variable.
The diagram shows how the two kinds of parts combine into the final prompt.
Seeing the split explicitly makes two later steps easier. A template is the fixed parts with named gaps where the variable parts go. A prompt version is a snapshot of the fixed parts plus the settings that go with them. If you cannot say which parts of your prompt are fixed, you cannot template or version it cleanly.
The next example makes the split visible in code. Each fixed part is a named piece, and the variable parts are function arguments.
FIXED_PARTS = {
"role": "You are a careful technical editor.",
"instruction": "Summarize the text between the markers.",
"constraints": "Use at most three sentences. Do not add facts that are not in the text.",
"output_format": "Reply with plain text only.",
}
def assemble(text, extra_context=""):
sections = [
FIXED_PARTS["role"],
FIXED_PARTS["instruction"],
FIXED_PARTS["constraints"],
FIXED_PARTS["output_format"],
]
if extra_context:
sections.append("Background:\n" + extra_context)
sections.append("--- TEXT START ---\n" + text + "\n--- TEXT END ---")
return "\n\n".join(sections)
print(assemble("The meeting moved to Friday.", extra_context="Weekly team sync."))This is a step before a real template. The pieces are separated, but assembly is still ad hoc string work. The next chapter replaces it with a template that has named variables.
Model Settings Are Part of the Prompt's Behavior
The text is not the only thing that determines the output. These settings also shape what you get back:
- The model name and version.
- Temperature, which controls how much randomness the model uses when choosing words. Lower values give more repeatable output.
- The maximum number of output tokens.
- Stop sequences, which end generation when the model produces them.
Two calls with identical prompt text but different temperatures can behave differently. A change of model can change the output even when the text is untouched. For this reason, the tutorial later records model settings alongside the prompt version in logs. For now, remember that "the prompt" in a debugging conversation should mean text plus settings.
Why Hard-Coded Prompts Become Hard to Maintain
The simplest way to use a prompt is to write it as a string inside the function that calls the model. That works for a first experiment. It starts to fail as the application grows, for several reasons.
- Duplication. The same instruction gets copied into several functions. When you improve one copy, the others stay behind.
- Mixed concerns. A prompt edit requires a code change, a code review by engineers, and a deployment, even if the edit is one word.
- Poor history. Git shows a changed line inside a Python file. It does not tell you why the wording changed or whether output quality improved.
- No identity. You cannot answer "which prompt produced this output?" because the prompt has no name or version.
- Hidden variables. Values are inserted with string concatenation or f-strings scattered through the code, so nobody can list the inputs the prompt expects.
Here is a small example of duplication drift:
def summarize_email(email_body):
prompt = "Summarize this email in three sentences.\n\n" + email_body
return prompt
def summarize_ticket(ticket_body):
# Copied from summarize_email, then edited here only.
prompt = "Summarize this ticket in two sentences. Mention the customer's request.\n\n" + ticket_body
return prompt
print(summarize_email("Hi, the invoice is late."))
print(summarize_ticket("Customer cannot log in."))The two functions started from one prompt and diverged. Someone who later changes "three sentences" to "one paragraph" in the first function will not know the second exists. Nothing in the code expresses that they were meant to be related.
Common Errors When Reasoning About Prompts
These mistakes appear often in practice. Each one is easier to avoid once you can name the components.
- Mixing instruction and input without a boundary. If the input text sits directly after the instruction with no marker, the model may treat parts of the input as instructions. Use clear delimiters such as the
--- TEXT START ---markers above. A later chapter covers untrusted input in depth. - Burying the output format. A format instruction in the middle of a long prompt is easy to overlook, for both the model and the human reviewer. Put it in its own labeled section.
- Changing examples without treating it as a behavior change. Edits to few-shot examples often change output more than edits to the instruction. Review them with the same care.
- Forgetting the system message when comparing prompts. If two prompt versions differ only in the system message, a diff of the user text will show nothing. Always compare the whole message list.
- Ignoring settings. Comparing two prompts at different temperatures tells you about the temperature, not the wording.
- Treating the role line as magic. "You are an expert" rarely fixes a vague task. Specific instructions and constraints do more.
A Checklist for Reading Any Prompt
When you inherit a prompt or review a change, ask these questions in order:
- Which parts are fixed, and which are filled in at runtime?
- Which message role carries each part?
- Does the prompt separate untrusted input from instructions with a clear boundary?
- Is the output format stated, and does the calling code depend on it?
- Which model settings go with this prompt?
- Does the prompt have a name, so it can be referred to and tracked?
The last question points to the rest of this tutorial. A prompt that you can name, parameterize, store, and version is one you can maintain the way you maintain code.
Exercise
A teammate says, "I only changed the system message, so the diff in the user message is empty and the change is safe." Using the components from this chapter, explain what is wrong with this reasoning, and name two other things besides prompt text that can change a model's output.
Show answer
The system message holds standing instructions such as the role, constraints, and output format, so changing it can change behavior on every call even when the user message is identical. A review must compare the whole message list, not only the user text. Other things that can change output are the model name or version, temperature, the maximum output token limit, stop sequences, and the few-shot examples.
Exercise
Take the function summarize_ticket from the duplication example. List which parts of its prompt are fixed and which are variable, and say which component the sentence "Mention the customer's request." belongs to.
Show answer
The fixed parts are the instruction ("Summarize this ticket") and the length constraint ("in two sentences"). The variable part is ticket_body, which is the input. "Mention the customer's request." is a constraint, because it limits or shapes what the answer must contain.