Parallel work
Run independent asynchronous operations at the same time and collect their results.
AI Agents · Class
task wraps an asynchronous operation for use inside a LangGraph entrypoint or StateGraph. Its returned proxy produces promises, enabling checkpoint-aware execution and parallel task calls.
The task function wraps an asynchronous operation and returns a promise-producing proxy. The proxy can be called only from an entrypoint or StateGraph; with checkpointing enabled, its inputs and outputs must be serializable, and concurrent calls can be awaited together.
task<ArgsT extends unknown[], OutputT>(optionsOrName: string | TaskOptions, func: TaskFunc<ArgsT, OutputT>): (...args: ArgsT) => Promise<OutputT>| Name | Type | Description |
|---|---|---|
optionsOrName | string | TaskOptions | A task name string or TaskOptions configuration object. |
func | TaskFunc<ArgsT, OutputT> | The function that performs the task. |
A proxy accepting the original arguments and returning Promise<OutputT>.
task to wrap an operation as a LangGraph task.import { entrypoint, task } from "@langchain/langgraph";
const addOne = task("add", async (value: number) => value + 1);
const workflow = entrypoint("example", async (numbers: number[]) => {
const promises = numbers.map((number) => addOne(number));
return await Promise.all(promises);
});
const result = await workflow.invoke([1, 2, 3]);
console.log(result); // [2, 3, 4]Defines a task and runs several task calls concurrently inside an entrypoint.
Run independent asynchronous operations at the same time and collect their results.
Give a unit of work a stable name or configure behavior such as retries.
Use task calls within a checkpointed workflow where serializable inputs and outputs can be persisted.
task function return?task accepts either a name or a TaskOptions object plus a task function. It returns a proxy with the same argument list that always resolves to a Promise of the task result.
Follow-up: How would you run independent tasks concurrently? Map the inputs to task calls, then await the resulting promises with Promise.all.
Tasks must run inside an entrypoint or StateGraph. This restriction lets LangGraph apply its runtime behavior, including checkpoint-aware execution.
Follow-up: What additional constraint applies with a checkpointer? Task inputs and outputs must be serializable.
task support parallel execution?A task can be invoked like a normal function, but each invocation produces a promise. Independent invocations can therefore be started together and awaited collectively.
Follow-up: Why is this useful for I/O-bound work? Concurrent waiting can reduce total elapsed time when operations do not depend on one another.
entrypointStateGraphTaskOptionsTaskFuncMemorySaver