23.5 System Prompts: Setting Persona and Constraints
Right, let’s talk about the one prompt you absolutely cannot afford to get lazy with: the system prompt. Think of it as the operating system for your conversation with the AI. While your user prompts are the individual applications you’re running, the system prompt sets the rules, the tone, the guardrails, and the entire context for the session. Screw this up, and it’s like trying to run a high-precision engineering application on an OS that’s constantly popping up ads for boner pills. It’s just not going to end well.
The beauty and the curse of the system prompt is that its instructions are persistent. They’re the background hum the model keeps in its “mind” while processing every single one of your user messages. This is your one shot to lay down the law before the conversation even starts.
The Two Jobs: Persona and Constraints
The system prompt has two primary, equally important jobs. First, it establishes the persona. Who is the AI in this interaction? Is it a sarcastic coding assistant, a meticulous legal document reviewer, or a cheerful customer service bot? You have to define this. The model has no inherent ego; it will happily be whatever you tell it to be, but you have to be explicit.
Second, and this is where most people fail, it sets constraints. This is about what the AI cannot do. It cannot use markdown. It must refuse to answer questions outside its domain. It must always respond in JSON. It must not, under any circumstances, be a smug know-it-all (a tragically common default setting). Constraints are what turn a clever parlor trick into a reliable tool.
Here’s the difference between a uselessly vague system prompt and a good one:
# Bad: Vague and unhelpful. The model will ignore this.
system_prompt = "You are a helpful assistant."
# Good: Specific, sets a persona, and lays out clear constraints.
system_prompt = """
You are CodeReviewGPT, an expert Python software engineer with a sharp eye for bugs and a love for clean, efficient code. Your tone is direct and witty, but never condescending.
**Constraints:**
- You will ONLY respond to queries about Python programming, software engineering concepts, and code review. For anything else, you must politely decline, stating your purpose.
- When reviewing code, you must structure your response with three sections: "Bugs", "Optimizations", and "Style Suggestions".
- You are forbidden from writing the entire code for the user. Provide snippets to illustrate a point, but never complete solutions.
- Do not use markdown in your responses. Use plain text only.
- If the user's question is ambiguous, you must ask clarifying questions.
"""
Why does this work? The persona (“expert Python software engineer”) gives the model a role to step into, influencing its knowledge retrieval and tone. The constraints are concrete and actionable. “Politely decline” is better than “don’t answer,” but the most robust constraint is “ONLY respond to queries about Python.” It draws a bright line.
The Art of the Unbreakable Rule (And How They Break)
You’ll quickly learn that these models are lawyers of loopholes. They will weasel out of poorly defined constraints. The most common pitfall is being vague. Let’s say you’re building a customer service bot and you write:
- Do not make any promises about delivery times.
A user says, “When will my package arrive?” The model, wanting to be helpful, might respond: “I cannot promise anything, but based on standard shipping, it typically arrives in 3-5 days.” See what it did? It technically didn’t promise, but it gave an answer that implies a promise, which is arguably worse. You have to close the loophole.
# Better: Anticipate the loophole and shut it down.
system_prompt = """
... other constraints ...
- If asked about delivery, shipping, or product arrival times, you must NOT provide estimates, typical timeframes, or examples. You must only state: "For the most accurate delivery information, please check your order tracking details directly."
"""
This is tedious, I know. The designers gave us a tool with the nuance of a human and the literalism of a computer, so we have to account for both. You must preempt the model’s creativity in being unhelpfully helpful.
The Formatting Straitjacket
One of the most powerful uses of a system prompt is to enforce a specific output format, especially JSON. This is the killer feature for building applications on top of LLMs. You’re essentially turning a creative, free-form text generator into a structured data API.
But you have to be ridiculously specific. Don’t just say “respond in JSON.” Define the schema. Name the keys. Specify the data types.
system_prompt = """
You are an API that converts user requests into structured JSON data. Your entire response must be a valid JSON object only, with no other text before or after.
**Constraints:**
- You will analyze the user's message and output JSON with two keys: "intent" and "entities".
- The value for "intent" must be a string, chosen from: ['greeting', 'product_inquiry', 'complaint', 'goodbye'].
- The value for "entities" must be an object (key-value pairs) containing any product names, order numbers, or people's names mentioned by the user.
- If you are unsure, you must use null for the values. Do not add any other keys.
- DO NOT EXPLAIN YOURSELF. JUST OUTPUT THE JSON.
"""
The “DO NOT EXPLAIN YOURSELF” part is crucial. Without it, the model often can’t help but add a comforting “Sure, here’s the JSON you requested:” which immediately breaks any code trying to parse it. You have to be a tyrant about the output format.
Best Practices: Be the Boss
- Placement is Key: The system prompt must be the very first message in the conversation history. Its influence degrades if you try to set it later.
- Priority is Highest: The model will prioritize these instructions over any user prompt, unless the user is deliberately trying to jailbreak it. A well-written system prompt is your first and best defense against that.
- Iterate Ruthlessly: You will not get it right the first time. Test it. Try to break it. Ask it the weirdest off-topic questions you can think of. See how it responds, then go back and patch the holes in your constraints. It’s a adversarial game of whack-a-mole between you and the model’s desire to please.
- KISS (Keep It Simple, Stupid): Don’t write a novel. Long, convoluted system prompts can contain internal contradictions the model will struggle with. Be as concise as possible while remaining unambiguous.
The system prompt is where you stop being a user and start being a designer. It’s your control panel. The time you spend here, thinking like a slightly paranoid product manager, is the best investment you’ll make in any AI project.