Parallel Execution

Run nodes in parallel, combine results with reducers, and distribute tasks dynamically with Send.

🎯 Introduction

Formatting and example checks can run independently when reviewing an article. Neither needs to wait for the other. LangGraph can schedule multiple nodes together and combine their results when they finish. This article covers fixed parallel tasks, shared state merging, and dynamically creating parallel branches with Send.


🧱 Run Independent Tasks in Parallel

Suppose review checks for a level-one heading and an example. Both checks read the same draft, but neither depends on the other, so they can run in parallel.

Same article draft
Heading check
issues: Missing a level-one heading
Example check
issues: Missing an example
Combine review results
Missing a level-one heading and an example
The checks run independently. The reducer merges their issues, and the summary node waits for both checks.

Create 05.parallel.mjs:

import { StateGraph, StateSchema, ReducedValue, START, END } from '@langchain/langgraph';
import { z } from 'zod';

const ArticleState = new StateSchema({
    draft: z.string(),
    issues: new ReducedValue(z.array(z.string()).default(() => []), {
        reducer: (current, update) => current.concat(update),
    }),
    report: z.string().default(''),
});

Both review nodes return issues. Use the reducer introduced earlier to append their issues to a shared list.

Add the two review nodes:

async function checkTitle(state) {
    console.log('Starting heading check');
    await new Promise(resolve => setTimeout(resolve, 300));

    if (!state.draft.startsWith('# ')) {
        return { issues: ['Missing a level-one heading'] };
    }

    return { issues: [] };
}

async function checkExample(state) {
    console.log('Starting example check');
    await new Promise(resolve => setTimeout(resolve, 300));

    if (!state.draft.includes('For example')) {
        return { issues: ['Missing an example'] };
    }

    return { issues: [] };
}

The short delays simulate asynchronous API requests. In a real project, the nodes could call different models or review services.

Next, add a summary node:

function summarizeReview(state) {
    if (state.issues.length === 0) {
        return { report: 'All checks passed' };
    }

    return { report: state.issues.join('; ') };
}

🛠 Wait for Parallel Tasks Before Summarizing

Append the following:

const graph = new StateGraph(ArticleState)
    .addNode('title', checkTitle)
    .addNode('example', checkExample)
    .addNode('summary', summarizeReview)
    .addEdge(START, 'title')
    .addEdge(START, 'example')
    // Wait for both check nodes before running the summary node
    .addEdge(['title', 'example'], 'summary')
    .addEdge('summary', END)
    .compile();

const result = await graph.invoke({ draft: 'LangGraph can orchestrate tasks.' });
console.log('Review report:', result.report);

Run:

node 05.parallel.mjs

One possible output:

Starting heading check
Starting example check
Review report: Missing a level-one heading; Missing an example

The logs show both checks starting before the summary appears. addEdge(['title', 'example'], 'summary') waits for both source nodes, rather than summarizing when either finishes. Business logic should not depend on the order of parallel updates.

Why Use a Reducer?

Multiple nodes may update the same state field within one execution step. If the field only supports a single update, LangGraph cannot choose which result to keep and raises an error.

For example, change issues to a plain z.array(z.string()).default(() => []) and run it again. Expect INVALID_CONCURRENT_GRAPH_UPDATE or a similar concurrent state update error (the exact message depends on the SDK). Restore the reducer afterward.

If nodes update different fields, such as titleIssue and exampleIssue, those fields do not need merge functions. A reducer should safely combine independent results without relying on the order in which parallel nodes finish.


🧱 Distribute Tasks Dynamically with Send

The number of checks above is fixed, so we can define two edges in advance. But if a user submits three sections that each need review, we need to create tasks based on the number of sections.

Send specifies a destination node and the data for that invocation. Multiple Send objects can target the same node to process different sections separately.

Create 05.send.mjs:

import { StateGraph, StateSchema, ReducedValue, Send, START, END } from '@langchain/langgraph';
import { z } from 'zod';

const Section = z.object({
    id: z.number(),
    text: z.string(),
});

const State = new StateSchema({
    sections: z.array(Section),
    issues: new ReducedValue(z.array(z.string()).default(() => []), {
        reducer: (current, update) => current.concat(update),
    }),
    report: z.string().default(''),
});

Each section is an object containing its ID and text, making it easier to identify where an issue occurs.

Add the distribution and review functions:

function assignSections(state) {
    const tasks = [];

    for (const section of state.sections) {
        // Send each section to the same worker node for independent processing
        tasks.push(new Send('worker', { section }));
    }

    if (tasks.length === 0) {
        return 'summary';
    }

    return tasks;
}

function reviewSection(input) {
    const { section } = input;
    console.log('Reviewing section:', section.id);

    if (!section.text.includes('For example')) {
        return { issues: [`Section ${section.id} is missing an example`] };
    }

    return { issues: [] };
}

function summarize(state) {
    if (state.issues.length === 0) {
        return { report: 'No missing examples found' };
    }

    return { report: state.issues.join('; ') };
}

reviewSection() receives the { section } carried by Send, rather than the full parent graph state. Its returned issues are still merged into the parent graph.

Add the graph definition and execution code:

const graph = new StateGraph(State)
    .addNode('worker', reviewSection)
    .addNode('summary', summarize)
    .addConditionalEdges(START, assignSections, ['worker', 'summary'])
    .addEdge('worker', 'summary')
    .addEdge('summary', END)
    .compile();

const result = await graph.invoke({
    sections: [
        { id: 1, text: 'Nodes execute tasks.' },
        { id: 2, text: 'For example: connect nodes with edges.' },
        { id: 3, text: 'State holds shared data.' },
    ],
});

console.log('Review report:', result.report);

Run:

node 05.send.mjs

One possible output:

Reviewing section: 1
Reviewing section: 2
Reviewing section: 3
Review report: Section 1 is missing an example; Section 3 is missing an example

This run creates three worker tasks. Adding or removing sections requires no changes to the graph's node definitions. All workers run in the same parallel step, and summary runs once they finish.

Change sections to [] and run node 05.send.mjs again. Expected output:

Review report: No missing examples found

With no sections to distribute, routing goes directly to summary. Your application can treat empty input as a validation error instead of a successful review if appropriate. Restore the original input afterward.

With real models, more parallel tasks mean more simultaneous requests. Limit input size according to service rate limits and your budget, or set a concurrency cap with maxConcurrency. Do not assume unlimited concurrency.

References: Graph API — Send, Workflows and agents.


❓ Questions and Answers

Q1: Do parallel LangGraph nodes automatically start multiple Node.js threads?

No. Parallelism here mainly means scheduling multiple tasks together, which suits asynchronous operations such as model requests and network access. CPU-intensive work may require Worker Threads or another appropriate mechanism.


🧪 Exercise

Add a fourth section without “For example” to sections in 05.send.mjs. Run it again and confirm that its review log and issue appear. Then use an empty array and verify that no workers execute.


🎉 Congratulations! You can now build fixed and dynamic parallel workflows.