To turn an LLM call into a chatbot workflow, use `StateGraph` to build a graph whose nodes read and update shared state. Start by defining the state: a `TypedDict` can specify a `messages` list, though it is not the only supported way to represent state. section 10.2.3
A one-node graph follows a short sequence:
- Create a builder with `StateGraph(MyGraphState)`. section 10.2.3
- Add a Python function as the assistant node. It receives the current state, calls the LLM with `state["messages"]`, and returns the state keys it wants to update. section 10.2.3
- Connect `START` to the assistant and the assistant to `END`; compile the builder before invoking the graph with an initial state. section 10.2.3
Listing 10.1 An example of a graph
from langgraph.graph import START, END, StateGraph
from langchain_core.messages import AnyMessage, HumanMessage
from langchain_openai import ChatOpenAI
from typing import TypedDict
llm = ChatOpenAI(model_name="gpt-5.1", openai_api_key="sk-proj-...")
class MyGraphState(TypedDict):
messages: list[AnyMessage]
builder = StateGraph(MyGraphState)
def assistant_node(state):
messages = state["messages"]
ai_response_message = llm.invoke(messages)
return {"messages": messages + [ai_response_message]}
builder.add_node("assistant", assistant_node)
builder.add_edge(START, "assistant")
builder.add_edge("assistant", END)
graph = builder.compile()
input_message = input("Talk to the bot: ")
initial_state = {"messages": [HumanMessage(content=input_message)]}
final_state = graph.invoke(initial_state)
print("Bot:\n" + final_state["messages"][-1].content)
The complete listing puts the state definition, node, edges, compilation, and a single chatbot exchange together. section 10.2.3
A node returns only the keys it wants to modify, even when a one-key state makes its return value look like the whole state. The chatbot version also uses LangGraph’s built-in `MessagesState` when wrapping the graph for a Streamlit interface. section 10.2.3

