Your First Workflow
🎯 Introduction
The previous article introduced nodes, edges, and state. Here, we will build a workflow that generates a draft and passes it to a review node. You will learn to define shared state, register and connect nodes, run the workflow, and stream each node's results.
🛠 Create the Project
Create a standalone langgraph-demo project. This course uses the same dependency versions as the preceding LangChain course: Node.js 24 LTS, LangGraph 1.7.5, LangChain Core 1.5.0, and Zod 4.2.1.
mkdir langgraph-demo
cd langgraph-demo
npm init -y
npm install --save-exact @langchain/langgraph@1.7.5 @langchain/core@1.5.0 zod@4.2.1
These dependencies serve the following purposes:
| Dependency | Purpose |
|---|---|
@langchain/langgraph | Build and run workflows |
@langchain/core | Provide the core runtime capabilities LangGraph depends on |
zod | Define state field types and default values |
No model API key or database is needed. We will simulate generation and review with ordinary functions to focus on workflow execution.
🧱 Define State and Nodes
We need two tasks: generate a draft from a topic, then check whether it contains an example. We will store the topic, draft, and review result to observe how data moves through the workflow.
Create 02.first-workflow.mjs:
import { StateGraph, StateSchema, START, END } from '@langchain/langgraph';
import { z } from 'zod';
const ArticleState = new StateSchema({
topic: z.string(),
draft: z.string().default(''),
review: z.string().default(''),
});
StateSchema defines the shared data structure that all nodes read and update. Only topic is supplied when starting the task, so it has no default. Later nodes produce draft and review, which start as empty strings.
Append two node functions:
function writeArticle(state) {
// Simulate article generation
return {
draft: `${state.topic} can organize tasks.`,
};
}
function reviewArticle(state) {
// Simulate a review rule: check for an example
const hasExample = state.draft.includes('For example');
if (!hasExample) {
return { review: 'Missing an example' };
}
return { review: 'Review passed' };
}
Each node receives the current state and returns the fields to update. The writing node updates draft, the review node updates review, and LangGraph preserves the other fields.
These strings and keyword checks only verify execution flow. They do not perform real AI writing or quality review.
🧱 Connect the Nodes
Next, tell LangGraph where execution starts and what should run after each node.
Append the following:
// Create a workflow with the state structure defined above
const builder = new StateGraph(ArticleState);
// Register nodes; their names are used to connect them
builder.addNode('write', writeArticle);
builder.addNode('review', reviewArticle);
// Define the execution order
builder.addEdge(START, 'write');
builder.addEdge('write', 'review');
builder.addEdge('review', END);
const graph = builder.compile();
StateSchema defines the data a workflow uses. StateGraph builds the workflow by organizing the nodes that operate on that data and their execution relationships.
addNode()registers an ordinary function as a node. For example,writeis the node name andwriteArticleis its function. When execution reaches that node, LangGraph calls the function with the current state.addEdge()connects nodes by name. Its first argument is the source and its second is the destination.STARTandENDare framework-provided entry and exit points; they need no business functions.compile()checks the graph's basic structure and produces a runnable workflow object.
We have now defined how the tasks should execute, but have not run any business nodes yet.
🛠 Run the Workflow
Append the following:
const result = await graph.invoke({
topic: 'LangGraph',
});
console.log(result);
Run:
node 02.first-workflow.mjs
Expected output:
{
topic: 'LangGraph',
draft: 'LangGraph can organize tasks.',
review: 'Missing an example'
}
invoke() accepts initial workflow data and returns the final state after execution. The data changes as follows:
| Stage | State change |
|---|---|
| Initialization | topic is LangGraph; draft and review are empty strings |
write | Generate a draft from topic and update draft |
review | Read the updated draft and set review to Missing an example |
Each node updates only its own fields, and subsequent nodes read the updated state. At END, the result contains all three state fields, rather than just the last node's return value.
🧱 Inspect Each Node's Results
invoke() waits for the entire workflow to finish. If generation and review take time, the frontend may need to display results as each node completes. Use stream() to execute the workflow again and receive updates during execution. We will use the updates mode introduced in the agent course.
Keep the state, nodes, and graph definitions. Replace the previous graph.invoke() call and console.log(result) with:
const stream = await graph.stream(
{
topic: 'LangGraph',
},
{
streamMode: 'updates',
}
);
for await (const update of stream) {
console.log(update);
}
Run it again:
node 02.first-workflow.mjs
Expected output (object formatting may differ slightly):
{ write: { draft: 'LangGraph can organize tasks.' } }
{ review: { review: 'Missing an example' } }
updates returns node updates keyed by node name. The first entry shows that write updated draft; the second shows that review updated review. The application can refresh frontend progress whenever it receives an update.
To receive the full state after each step, use values mode. This course mainly uses updates to inspect node results.
❓ Questions and Answers
Q1: Why does the workflow end even when the review fails?
At present, review has only one outgoing edge, leading to END. The result is stored in state but does not yet affect routing. We will address this when we cover conditional branches.
🧪 Exercise
Add a report node to the workflow to summarize the topic and review result.
- Add a string field named
reporttoArticleState, with an empty string as its default. - Write a node function that reads
topicandreviewand stores a summary inreport. - Register
report. Replace the edge fromreviewtoENDwith edges fromreviewtoreportand fromreporttoEND. - Run the streaming example again. Confirm that the three nodes run in order and inspect the result returned by
report.
This exercise only summarizes the review result; it does not decide whether to deliver the article.
🎉 Congratulations! You have created and run your first LangGraph workflow.
