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

A Chatbot Graph

One model node.

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

A chatbot is the simplest LLM graph. One node calls the model with the conversation so far. To hold a conversation, we send the whole history back in, each turn.

A Chatbot Graph
From the video: A Chatbot Graph
  • The simplest LLM graph
  • One model node
  • Send the history each turn

Two turns

The graph is one chat node on messages state. We keep the messages from each run, add the next user message, and invoke again. Run it. In the second turn, the model remembers the name from the first, because the whole history went in.

chatbot.pypython
from langchain.chat_models import init_chat_model
from langgraph.graph import MessagesState, StateGraph, START

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)

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

b = StateGraph(MessagesState); b.add_node(chat); b.add_edge(START, "chat")
graph = b.compile()

messages = []
for text in ["Hi, I'm Tom and I love sci-fi. Reply in one sentence.", "What's my name? One word."]:
    messages = graph.invoke({"messages": messages + [{"role": "user", "content": text}]})["messages"]
    print("Tom:", text)
    print("Bot:", messages[-1].text.strip())
Output
Tom: Hi, I'm Tom and I love sci-fi. Reply in one sentence.
Bot: Greetings Tom, may your warp drives always be fully charged and your timelines free
of paradoxes!
Tom: What's my name? One word.
Bot: Tom

Carrying history

Here, our code carries the history between turns. It works, but every caller has to manage it. Soon, checkpointers will store the history inside LangGraph, so each turn only sends the new message.

keyvalue
Nowwe carry the history
Each turnsend it all
Sooncheckpointers
Thensend only new messages

Summary

A chatbot graph is one model node. Sending the whole history keeps the conversation going. Next, adding a system prompt inside a node.

Key takeaways
  • One chat node
  • History in, reply out
  • Checkpointers later
Next up: Prompts in Nodes — system messages