Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Agents · Class

Task

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.

How it works

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.

Syntax

task<ArgsT extends unknown[], OutputT>(optionsOrName: string | TaskOptions, func: TaskFunc<ArgsT, OutputT>): (...args: ArgsT) => Promise<OutputT>

Parameters

NameTypeDescription
optionsOrNamestring | TaskOptionsA task name string or TaskOptions configuration object.
funcTaskFunc<ArgsT, OutputT>The function that performs the task.

Returns

A proxy accepting the original arguments and returning Promise<OutputT>.

Key points

  • Use task to wrap an operation as a LangGraph task.
  • The returned task proxy is callable with the wrapped function's arguments.
  • Every task invocation returns a promise rather than the direct value.
  • Start independent task calls together and await them collectively for parallel execution.
  • The first argument can name the task or configure it with TaskOptions.
  • The second argument supplies the asynchronous implementation.

Common mistakes

  • Calling a task outside an entrypoint or StateGraph is invalid.
  • Use serializable task arguments and results when checkpointing is enabled.
  • The original wrapped function is subject to the same execution-context restriction as the task proxy.

Example: Parallel task execution

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.

When to use it

Parallel work

Run independent asynchronous operations at the same time and collect their results.

Configured tasks

Give a unit of work a stable name or configure behavior such as retries.

Checkpointed workflows

Use task calls within a checkpointed workflow where serializable inputs and outputs can be persisted.

Interview questions

QWhat does the 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.

QWhere may a LangGraph task be called?

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.

QHow does 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.

  • entrypoint
  • StateGraph
  • TaskOptions
  • TaskFunc
  • MemorySaver

Source: Function task. Checked 2026-10-15.