AI Hints
Home›LangGraph Tutorial›Chatbot Graphs
LangGraph · Lesson 30 of 120

Prompts in Nodes

System messages.

⏱ 2 min read▶ Video at 31:44🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Where should the system prompt live? Usually not in the state. If it's in the state, it's stored with every conversation. Instead, the node adds it, just before calling the model.

Prompts in Nodes
From the video: Prompts in Nodes
  • Where does the prompt go?
  • Not in the state
  • Added by the node

Adding the system prompt

The system prompt is a constant. The node puts it in front of the messages, only for the model call. The state stays clean. Run it. The answer follows the shop's rules, and the final state holds only the user message and the reply, with no system message.

system_prompt.pypython
from langchain.chat_models import init_chat_model
from langchain.messages import SystemMessage
from langgraph.graph import MessagesState, StateGraph, START

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
SYSTEM = SystemMessage("You are the Riverside Books assistant. One short sentence. "
                       "Always suggest visiting our shop in Bath.")

def chat(s: MessagesState):
    return {"messages": [model.invoke([SYSTEM] + s["messages"])]}

b = StateGraph(MessagesState); b.add_node(chat); b.add_edge(START, "chat")
out = b.compile().invoke({"messages": [{"role": "user", "content": "Any tips for a rainy day?"}]})
print(out["messages"][-1].text.strip())
print("stored:", [m.type for m in out["messages"]])
Output
Cozy up with a good mystery novel from our shop in Bath.
stored: ['human', 'ai']

Prompt placement

Adding the prompt in the node keeps state small, and lets you change the prompt without touching stored conversations. Different nodes can have different prompts, which is how one graph can hold several specialists.

keyvalue
In the nodenot the state
Change freelyold chats unaffected
Per nodedifferent prompts
Specialistsone graph, many roles

Summary

Add the system prompt inside the node, just before the model call. The state stays clean. Next, giving models tools inside a graph.

Key takeaways
  • [SYSTEM] + messages
  • State stays clean
  • Prompt per node
Next up: Tools in a Graph — bind_tools