Skip to main content

State Node

The State Node, which can only be connected to the Start Node, provides a mechanism to set a user-defined or custom State into our workflow from the start of the conversation. This custom State is a JSON object that is shared and can be updated by nodes in the graph, passing from one node to another as the flow progresses.

Understanding the State Node

By default, the State includes a state.messages array, which acts as our conversation history. This array stores all messages exchanged between the user and the agents, or any other actors in the workflow, preserving it throughout the workflow execution.

Since by definition this state.messages cannot be directly manipulated or overwritten, the State Node allows us to augment this default State by adding our own custom attributes to the top level of the State object (alongside state.messages), enabling us to track and manage workflow-specific information.

For example, let's consider a workflow where we want to track the user's language preferences, usage statistics, and product inquiries:

{
"messages": [...], // Conversation history - implicit
"userSettings": { // Custom State - defined by the State Node
"preferredLanguage": "English",
"productInquiries": ["Laptop", "Smartphone"],
"usageMetrics": {
"totalInteractions": 5,
"lastInteractionDate": "2023-11-20"
}
}
}

In addition to the conversation history in the messages array, our workflow now has access to the custom State properties encapsulated under the userSettings object.

Inputs

RequiredDescription
Initial StateYesA JSON object that defines the custom State that will be added to the conversation state.

Outputs

The State Node has no direct outputs. Instead, it connects to the Start Node as its input, making the custom State available to all subsequent nodes in the workflow.

Dynamics of the State

Think of the State as a package of information that flows through the nodes in our Sequential Agent workflow. Each node can inspect the package and either:

  1. Use the information contained in the State to make decisions or generate responses, or
  2. Update the information by modifying existing properties or adding new ones, all while preserving the messages array.

As the State passes from one node to the next, these modifications are carried forward, enabling cumulative and progressive data management throughout the workflow. For example, an Agent Node might add interview responses to the State, an LLM Node can analyze that information, and a Condition Node can use it to determine the next steps.

Best Practices

Use structured data

Design your custom State with well-organized, nested objects rather than flat key-value pairs. This approach promotes clarity and scalability, making it easier to manage and evolve your State as your workflow grows more complex.

Consistent naming conventions

Adopt clear, descriptive naming conventions for your State properties. This enhances readability and helps maintain consistency throughout your workflow, especially when multiple team members are involved.