Sub topic 5
Designing system prompts and message structure
This chapter covers how to structure the messages list you send to a served chat model. It assumes you can already call a serving endpoint with the chat completions client from the previous chapter, and that you know what system, user, and assistant roles are.
The working model
A chat completions request sends an ordered list of messages. Each message has a role and content. The model reads the whole list on every call and keeps no memory between calls. Anything it needs to know must be in the list.
| Role | Purpose | Typical content |
|---|---|---|
| `system` | Sets behavior, tone, and rules | Persona, output format, limits |
| `user` | The request from the person or caller | A question or task |
| `assistant` | A prior model reply | Earlier answers, or example answers |
The system message is the place for instructions that stay the same across requests. The user message carries what changes. Keeping the two apart makes prompts easier to test and reuse.
Writing a useful system prompt
A strong system prompt is specific and short. Cover these points:
- Role: who the model is acting as.
- Task boundaries: what it should and should not answer.
- Output format: length, structure, or JSON shape.
- Fallback behavior: what to do when it lacks information.
The RAG example later in this tutorial uses a fallback rule: "Answer using only the context below. If the answer is not there, say so." That one sentence limits the model to supplied evidence and gives it a safe way out.
Few-shot examples and history
You can show the model the format you want by adding example turns before the real question. These are called few-shot examples. Use user and assistant pairs.
from databricks.sdk import WorkspaceClient
ENDPOINT_NAME = "<your-chat-endpoint-name>"
client = WorkspaceClient().serving_endpoints.get_open_ai_client()
SYSTEM_PROMPT = (
"You classify support tickets as 'billing', 'technical', or 'other'. "
"Reply with the label only, in lowercase."
)
def classify(ticket: str, history: list[dict] | None = None) -> str:
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": "I was charged twice this month."},
{"role": "assistant", "content": "billing"},
{"role": "user", "content": "The dashboard shows a 500 error."},
{"role": "assistant", "content": "technical"},
]
messages += history or []
messages.append({"role": "user", "content": ticket})
response = client.chat.completions.create(
model=ENDPOINT_NAME,
messages=messages,
max_tokens=10,
temperature=0,
)
return response.choices[0].message.content.strip()
print(classify("How do I reset my password?"))Setting temperature=0 makes the output more repeatable, which suits classification. For multi-turn chat, append each new user message and each model reply to the list, then send the full list again.
Common errors
- Putting changing data in the system prompt. Keep it stable. Pass retrieved context or user input as part of the request, and test the system prompt on its own.
- Vague instructions. "Be helpful" gives the model nothing to follow. State the format and limits.
- Unbounded history. Every message counts toward the context window and your token cost. Trim or summarize old turns.
- Trusting the prompt as a security control. A user can try to override instructions. Treat retrieved text and user text as untrusted, and enforce real limits with guardrails (covered in the AI Gateway chapter).
- Changing prompts without recording them. A small wording change can alter results. Keep prompts in version control and use tracing to see what was actually sent.
Exercise
Exercise
Your RAG assistant sometimes invents answers when the retrieved context does not contain the information. Which part of the message structure should you change first, and what should you add?
Show answer
Change the system message. Add an explicit fallback rule, such as "Answer using only the context below. If the answer is not there, say you don't know." Keep the retrieved context separate from the fixed instructions, and test with questions the context cannot answer.