# StateGraph

> **Class** in `@langchain/langgraph`

📖 [View in docs](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph)

A graph whose nodes communicate by reading and writing to a shared state.
Each node takes a defined `State` as input and returns a `Partial<State>`.

Each state key can optionally be annotated with a reducer function that
will be used to aggregate the values of that key received from multiple nodes.
The signature of a reducer function is (left: Value, right: UpdateValue) => Value.

See Annotation for more on defining state.

After adding nodes and edges to your graph, you must call `.compile()` on it before
you can use it.

## Signature

```javascript
class StateGraph
```

## Extends

- `Graph<N, S, U, StateGraphNodeSpec<S, U>, ToStateDefinition<C>>`

## Constructors

- [`constructor()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/constructor)

## Properties

- `branches`
- `channels`
- `compiled`
- `edges`
- `entryPoint`
- `Node`
- `nodes`
- `waitingEdges`
- `allEdges`

## Methods

- [`_addSchema()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/_addSchema)
- [`addConditionalEdges()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/addConditionalEdges)
- [`addEdge()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/addEdge)
- [`addNode()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/addNode)
- [`addSequence()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/addSequence)
- [`compile()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/compile)
- [`setEntryPoint()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/setEntryPoint)
- [`setFinishPoint()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/setFinishPoint)
- [`validate()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/validate)
- [`warnIfCompiled()`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph/warnIfCompiled)

## Examples

```ts
import {
  type BaseMessage,
  AIMessage,
  HumanMessage,
} from "@langchain/core/messages";
import { StateGraph, Annotation } from "@langchain/langgraph";

// Define a state with a single key named "messages" that will
// combine a returned BaseMessage or arrays of BaseMessages
const StateAnnotation = Annotation.Root({
  sentiment: Annotation<string>,
  messages: Annotation<BaseMessage[]>({
    reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
      if (Array.isArray(right)) {
        return left.concat(right);
      }
      return left.concat([right]);
    },
    default: () => [],
  }),
});

const graphBuilder = new StateGraph(StateAnnotation);

// A node in the graph that returns an object with a "messages" key
// will update the state by combining the existing value with the returned one.
const myNode = (state: typeof StateAnnotation.State) => {
  return {
    messages: [new AIMessage("Some new response")],
    sentiment: "positive",
  };
};

const graph = graphBuilder
  .addNode("myNode", myNode)
  .addEdge("__start__", "myNode")
  .addEdge("myNode", "__end__")
  .compile();

await graph.invoke({ messages: [new HumanMessage("how are you?")] });

// {
//   messages: [HumanMessage("how are you?"), AIMessage("Some new response")],
//   sentiment: "positive",
// }
```

---

[View source on GitHub](https://github.com/langchain-ai/langgraphjs/blob/dc37d3cd62bf88272cdc874e37354ae83ac01660/libs/langgraph-core/src/graph/state.ts#L292)