Langchain System Prompt
System Prompt (System Prompt) is the core method to control Agent behavior.
This section introduces the usage of static prompts and dynamic prompts.
* * *
## system_prompt Parameter
The system_prompt parameter of create_agent() accepts two forms:
## Example
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.messages import SystemMessage
model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)
# Method 1: String (simplest)
agent = create_agent(
model=model,
system_prompt="You are TUTORIAL 'sLearning Advisor, responses should be concise and professional.",
)
# Method 2: SystemMessage object (reusable)
system_msg = SystemMessage(
content="You are TUTORIAL 'sLearning Advisor, responses should be concise and professional."
)
agent = create_agent(model=model, system_prompt=system_msg)
* * *
## Designing Effective System Prompts
A good system_prompt should include the following elements:
## Example
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage
from langchain.tools import tool
@tool
def search_course(keyword: str) ->str:
"""Searching for courses"""
courses ={
"python": "Python3 Basic Tutorial (Free)",
"html": "HTML Basic Tutorial (Free)",
}
return courses.get(keyword.lower(),"No relevant courses found")
# A well-designed system_prompt
GOOD_PROMPT ="""You are TUTORIAL 'sLearning Advisorγ
## you'sResponsibilities
- Help users find suitable'sProgramming courses
- Answer programming learning related queries'sQuestions
- Recommend learning paths based on user level
## Behavioral Guidelines
- Keep answers concise, no more than 3 sentences each time
- Prioritize using the search_course tool to query course information
- If the user is a complete beginner, prioritize recommending Getting Started courses
- Do not use emoji
- Don't know'sJust say you don't know, don't fabricate answers"""
model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)
agent = create_agent(
model=model,
tools=,
system_prompt=GOOD_PROMPT,
)
result = agent.invoke({
"messages": [HumanMessage(content="I'm a complete beginner and want to learn programming, what do you recommend?")]
})
print(result.content)
Output:
It is recommended to start with the Python3 Basic Tutorial. Python syntax is concise and suitable for beginners Getting Started. This course is available for free on TUTORIAL, with systematic and comprehensive content.
* * *
## @dynamic_prompt β Dynamically Generating Prompts
Static system_prompt treats all users equally. However, in practical applications, you may need to dynamically adjust prompts based on user information, conversation context, time, etc.
The @dynamic_prompt decorator allows you to dynamically generate system_prompt before each model call.
## Example
from langchain.agents import create_agent
from langchain.agents.middleware import dynamic_prompt
from langchain.agents.middleware.types import ModelRequest
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage
from langchain.tools import tool
@tool
def search_course(keyword: str) ->str:
"""Searching for courses"""
courses ={
"python": "Python3 Basic Tutorial (Free, 20 hours)",
"html": "HTML Basic Tutorial (Free, 15 hours)",
"java": "Java Basic Tutorial (Free, 25 hours)",
}
return courses.get(keyword.lower(),"No relevant courses found")
# @dynamic_prompt decorator: receives ModelRequest, returns new system_prompt
@dynamic_prompt
def personalized_prompt(request: ModelRequest) ->str:
"""Dynamically generate personalized prompts based on conversation context"""
messages = request.state.get("messages",[])
message_count =len(messages)
# Can dynamically adjust prompts based on different conditions
base_prompt ="You are TUTORIAL 'sLearning Advisorγ"
if message_count 10:
# Long conversation, remind to keep it concise
return base_prompt + (
"The conversation is already quite long, keep answers as concise as possible,"
"No more than 2 sentences per response."
)
else:
# Normal conversation phase
return base_prompt + (
"Based on the user's previous'sRecommend suitable questions'scourses,"
"Use the search_course tool to query course information."
)
model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)
agent = create_agent(
model=model,
tools=,
middleware=,# Inject via middleware
)
# First conversation (message_count str:
"""Dynamically generate prompts based on user information, time, and conversation stage"""
# Get user info from runtime.context
context = request.runtime.context
user_name = context.get("user_name","Student")if context else"Student"
user_level = context.get("user_level","Getting Started")if context else"Getting Started"
# Get current time
now =datetime.now()
greeting ="Good morning"if now.hour<12 else"Good afternoon"if now.hour20:
prompt +="n- The conversation is very long, keep answers as brief as possible"
return prompt
> @dynamic_prompt executes before each model call, so prompts can change as the conversation progresses. However, avoid heavy computations in it, as it will affect response speed.
* * *
## Manual Control of System Prompt Priority
If both create_agent()'s system_prompt and @dynamic_prompt middleware are set, the middleware has higher priority β it will override the static prompt.
## Example
from langchain.agents import create_agent
from langchain.agents.middleware import dynamic_prompt
from langchain.agents.middleware.types import ModelRequest
from langchain.chat_models import init_chat_model
@dynamic_prompt
def override_prompt(request: ModelRequest) ->str:
"""this middleware 's system_prompt will override in create_agent'sSettings"""
return"You are a cat, and all replies must use'Meow'End."
model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0.7)
agent = create_agent(
model=model,
system_prompt="You are TUTORIAL 'sProfessional Learning Advisor.",# Will be overridden
middleware=,# Higher priority
)
result = agent.invoke({
"messages": [{"role": "user","content": "Introduce yourself"}]
})
print(result.content)
Output:
I am a cute'sCat Assistant Meow~Specialized in helping everyone solve various problems, meow!
> If you want the middleware prompt to merge with the static prompt instead of overriding, you can manually concatenate them in @dynamic_prompt. The original system_prompt is not directly exposed in request, so if you need to keep the original content, it is recommended to reference the static prompt as a variable in the function.
* * *
## System Prompt Design Checklist
| Element | Description | Example |
| --- | --- | --- |
| Role Definition | Define AI's identity and responsibilities | You are TUTORIAL 'sLearning Advisor |
| Behavior Guidelines | Constrain response style and boundaries | Keep answers concise, no more than 3 sentences each time |
| Tool Usage Guide | Tell the model when to use which tools | Prioritize using the search_course tool when querying courses |
| Boundary Constraints | Clarify what should not be done | Don't know'sJust say you don't know, don't fabricate answers |
| Format Requirements | Specify response format
YouTip