langchain

02: System prompt

02: System prompt

What this does

Separates a fixed behavioral rule (SystemMessage) from the user's actual question (HumanMessage), and sends both as a conversation list instead of a single string.

from langchain_ollama import ChatOllama
from langchain_core.messages import SystemMessage, HumanMessage

llm = ChatOllama(model="llama3.2:3b")

messages = [
    SystemMessage("You are a senior backend engineer. Always answer in 2 sentences or less, no fluff."),
    HumanMessage("What is LangChain?"),
]

response = llm.invoke(messages)
print(response.content)

Why a list, not a string

Part 1 passed a plain string to .invoke(). LangChain accepted it as shorthand and silently converted it into a single HumanMessage behind the scenes. Here, passing an explicit list is what makes a multi-role conversation possible at all — a plain string has no way to express "this part is a rule, this part is the user."

The list is also the mechanism conversation memory is built on later: a real chat app appends each new HumanMessage and the model's AIMessage reply to the same list and resends the whole thing every call, because these models are stateless between requests — nothing is remembered server-side unless you resend it.

The three message roles

TypeWho writes itSent to the model as
SystemMessageThe application codeA "system" role message
HumanMessageThe end userA "user" role message
AIMessageThe model's own prior replyAn "assistant" role message, when replayed back in history

Under the hood, LangChain converts these into the role-tagged JSON format every chat API actually expects ({"role": "system", "content": "..."}, etc.). The SystemMessage/HumanMessage classes exist so your code reads clearly and stays provider-agnostic — you're not hand-building that JSON or memorizing each provider's exact field names.

Order and position matter

The system message is conventionally placed first in the list. Most providers treat position as meaningful — a system instruction appearing after user turns is weaker or ignored by some models, because it reads more like "the user said this" than "this is a standing rule." Put SystemMessage first unless you have a specific reason not to.

Production considerations (not visible in this toy example)

One-line interview answer

"A system prompt is a SystemMessage placed first in the message list — it sets a standing rule the model should follow for the whole conversation, separate from what the user typed. It's not a security boundary; it's a strong instruction that a determined adversarial prompt can still override."