Low-level orchestration framework for building stateful, multi-actor applications with LLMs
Examples with persistence and memory.
import { StateGraph, MessagesAnnotation, START, END } from "@langchain/langgraph";
import { MemorySaver } from "@langchain/langgraph-checkpoint";
import { HumanMessage, AIMessage } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI();
const chatNode = async (state: typeof MessagesAnnotation.State) => {
const response = await model.invoke(state.messages);
return { messages: [response] };
};
const chatbot = new StateGraph(MessagesAnnotation)
.addNode("chat", chatNode)
.addEdge(START, "chat")
.addEdge("chat", END)
.compile({ checkpointer: new MemorySaver() });
// First message
await chatbot.invoke(
{ messages: [new HumanMessage("Hi! My name is Alice.")] },
{ configurable: { thread_id: "user-123" } }
);
// Follow-up (remembers context)
const result = await chatbot.invoke(
{ messages: [new HumanMessage("What's my name?")] },
{ configurable: { thread_id: "user-123" } }
);
// Response: "Your name is Alice."import { entrypoint, getPreviousState } from "@langchain/langgraph";
import { MemorySaver } from "@langchain/langgraph-checkpoint";
interface WorkflowState {
stepsDone: string[];
data: any;
}
const workflow = entrypoint({
name: "multi-step",
checkpointer: new MemorySaver()
}, async (input: { action: string; data?: any }) => {
const prev = getPreviousState<WorkflowState>();
const stepsDone = [...(prev?.stepsDone || []), input.action];
const data = input.data || prev?.data;
// Process based on action
if (input.action === "start") {
return entrypoint.final({
value: "Started. Call with action 'process' next.",
save: { stepsDone, data: input.data }
});
}
if (input.action === "process") {
const processed = processData(data);
return entrypoint.final({
value: "Processed. Call with action 'finish' next.",
save: { stepsDone, data: processed }
});
}
if (input.action === "finish") {
return entrypoint.final({
value: `Completed ${stepsDone.length} steps: ${stepsDone.join(", ")}`,
save: { stepsDone, data }
});
}
});
const config = { configurable: { thread_id: "workflow-1" } };
await workflow.invoke({ action: "start", data: initialData }, config);
await workflow.invoke({ action: "process" }, config);
const result = await workflow.invoke({ action: "finish" }, config);See: Checkpointing Guide for persistence patterns.