Skip to main content
This example builds a two-stage article pipeline using Vibes’ Graph API: an outline node followed by a writing node. The Graph class manages state transitions between BaseNode instances - each node receives the current state, does its work, and either transitions to the next node with next() or terminates with output().

What you’ll learn

  • Defining graph nodes with BaseNode
  • State transitions using the next() free function
  • Terminal output using the output() free function
  • Composing a Graph from nodes and running it

Prerequisites

  • Vibes installed (jsr:@vibesjs/sdk)
  • ANTHROPIC_API_KEY environment variable set
Always import next and output as free functions from @vibesjs/sdk. Do not call them as instance methods (e.g., prefixed with this.) - they are standalone functions, not methods on BaseNode, and calling them as such will throw at runtime.

Complete example

Source: examples/graph-workflow.ts

Run it

How it works

State type

PipelineState carries all data through the graph. Each node receives the full state and returns a new state (immutable - always spread with { ...state, field: value }).

next(nodeId, newState)

Transitions to the named node. The nodeId must match the id of another node in the graph. The newState becomes the input state for that node. You can call it with or without explicit type parameters:

output(value)

Terminates the graph and returns value as the final result. The type parameter string (second generic on BaseNode<TState, TOutput>) must match.

new Graph([nodes])

Pass all nodes as an array. The second argument to graph.run() is the ID of the starting node.

Next steps