Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 3 · 2. LCEL & Prompt Architecture
Lesson 15 of 68

System, Human, AI Messages

The cast of characters in an LLM conversation. Controlling behavior effectively.

Derpx0610 Jan · 1 min read
LangChain Mastery
Your First Chatbot: Talking to an LLMLCEL: Why LangChain Is a Composition EngineThe Runnable Protocol (invoke, batch, stream)Pipe Operator & Data FlowRunnableLambda & Custom LogicRunnableParallel & BranchingPromptTemplate vs ChatPromptTemplateSystem, Human, AI MessagesPrompt Architecture & ReusabilityStructured Output with JSON ModePydanticOutputParserOutputFixingParserStreaming Tokens & Async Chains
Contents
1. The Cast of CharactersA. SystemMessageB. HumanMessageC. AIMessage2. Why Separation Matters3. Dynamic Personalities4. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
Your First Chatbot: Talking to an LLMLCEL: Why LangChain Is a Composition EngineThe Runnable Protocol (invoke, batch, stream)Pipe Operator & Data FlowRunnableLambda & Custom LogicRunnableParallel & BranchingPromptTemplate vs ChatPromptTemplateSystem, Human, AI MessagesPrompt Architecture & ReusabilityStructured Output with JSON ModePydanticOutputParserOutputFixingParserStreaming Tokens & Async Chains
Contents
1. The Cast of CharactersA. SystemMessageB. HumanMessageC. AIMessage2. Why Separation Matters3. Dynamic Personalities4. Summary

1. The Cast of Characters

A conversation isn't just a list of strings. It's a script with roles. Chat Models distinguish between Who is speaking.

A. SystemMessage

Role: The Director / God Mode. Function: Sets the rules, tone, and constraints. Visibility: The user usually never sees this. Weight: Models pay extra attention to this (usually).

B. HumanMessage

Role: The User. Function: The input query or command.

C. AIMessage

Role: The Model. Function: The response.

2. Why Separation Matters

Beginners often put everything in the HumanMessage: "You are a helpful assistant. Tell me a joke."

This is weak. The model treats it as a suggestion from the user, not a law of the universe. If a user later says "Ignore previous instructions", the model is likely to obey because "User overrides User."

If you put constraints in SystemMessage, they are harder to override (though not impossible). "System: You are a helpful assistant." "User: Ignore instructions." "System Rules > User Rules."

3. Dynamic Personalities

Using ChatPromptTemplate, we can swap personalities easily.

personalities.py
from langchain_core.messages import SystemMessage, HumanMessage

def get_personality_chain(role):
  template = ChatPromptTemplate.from_messages([
      ("system", "You are a {role}. Answer briefly."),
      ("human", "{question}")
  ])
  return template | model

# Role: Pirate
get_personality_chain("Pirate").invoke({"question": "Hi"}) 
# "Ahoy matey!"

# Role: Butler
get_personality_chain("English Butler").invoke({"question": "Hi"})
# "Good afternoon, sir."

4. Summary

Don't just be a user. Be a Director. Use SystemMessage to establish the laws of your world. Use HumanMessage only for the immediate input.

Key Intuition: "Behavior lives in system messages."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonPromptTemplate vs ChatPromptTemplateNext lesson →Prompt Architecture & Reusability