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

AI Agents · Function

add_node

Adds a named node to a LangGraph.js StateGraph. The node implementation runs when graph execution reaches it and can return updates to the graph state.

How it works

In the JavaScript graph builder, addNode registers a named node implementation in a StateGraph. The node can be a function that returns state updates; after adding edges, compile the builder and invoke the resulting graph.

Syntax

graph.addNode(name, node)

Parameters

NameTypeDescription
namestringThe node name used to identify the node and reference it from edges.
nodeNodeActionThe node implementation that runs when the graph reaches this node.

Returns

The StateGraph builder, enabling chained graph construction.

Key points

  • The first argument identifies the node, while the second supplies the function or runnable that performs its work.
  • Create the graph with a state schema before adding nodes.
  • Connect START to the registered node so execution can enter it.

Example: Register a node in a StateGraph

import { Annotation, StateGraph, START, END } from "@langchain/langgraph";

const State = Annotation.Root({
  result: Annotation<string>(),
});

const node = () => ({ result: "done" });

const graph = new StateGraph(State)
  .addNode("node", node)
  .addEdge(START, "node")
  .addEdge("node", END)
  .compile();

console.log(await graph.invoke({}));

Defines a state graph, registers a node by name, connects it between START and END, compiles the graph, and invokes it.

When to use it

Build a workflow

Registering application logic as a step in a stateful workflow.

Add agent steps

Connecting an agent or tool operation to graph control flow.

Compose subgraphs

Embedding a compiled subgraph when state keys are shared.

  • StateGraph
  • addEdge
  • START
  • compile
  • invoke

Source: Use the graph API. Checked 2026-10-15.