• Define a LangGraph task using the task function.

    Tasks can only be called from within an entrypoint or from within a StateGraph. A task can be called like a regular function with the following differences:

    • When a checkpointer is enabled, the function inputs and outputs must be serializable.
    • The wrapped function can only be called from within an entrypoint or StateGraph.
    • Calling the function produces a promise. This makes it easy to parallelize tasks.

    Type Parameters

    • ArgsT extends unknown[]

      The type of arguments the task function accepts

    • OutputT

      The type of value the task function returns

    Parameters

    Returns (...args: ArgsT) => Promise<OutputT>

    A proxy function that accepts the same arguments as the original and always returns the result as a Promise

    const addOne = task("add", async (a: number) => a + 1);

    const workflow = entrypoint("example", async (numbers: number[]) => {
    const promises = numbers.map(n => addOne(n));
    const results = await Promise.all(promises);
    return results;
    });

    // Call the entrypoint
    await workflow.invoke([1, 2, 3]); // Returns [2, 3, 4]
    const addOne = task({
    name: "add",
    retry: { maxAttempts: 3 }
    },
    async (a: number) => a + 1
    );

    const workflow = entrypoint("example", async (numbers: number[]) => {
    const promises = numbers.map(n => addOne(n));
    const results = await Promise.all(promises);
    return results;
    });