Run your first graph
Build a graph that takes a name and returns a greeting. It runs entirely on your machine and needs only Go 1.26.10 or newer.
Create a Go module
Section titled “Create a Go module”In a terminal, create an empty project and add Loom’s graph package.
mkdir hello-loomcd hello-loomgo mod init example.com/hello-loomgo get github.com/masterkeysrd/loom/graphAdd a greeting node
Section titled “Add a greeting node”Save this program as main.go. The two edges tell Loom to start at greet and finish when that node returns its update.
package main
import ( "context" "fmt" "log"
"github.com/masterkeysrd/loom/graph")
type Greeting struct { Name string Text string}
func (s Greeting) Copy() Greeting { return s }
func greet(_ context.Context, _ Greeting) (graph.Command[Greeting], error) { return graph.Update[Greeting](func(s Greeting) Greeting { s.Text = "Hello, " + s.Name + "!" return s }), nil}
func main() { builder := graph.New[Greeting]() builder.AddNode("greet", graph.NodeFunc(greet)) builder.AddEdge(graph.START, "greet") builder.AddEdge("greet", graph.END) g, err := builder.Build() if err != nil { log.Fatal(err) }
input := graph.Update[Greeting](func(s Greeting) Greeting { s.Name = "Ada" return s }) result, err := g.Execute(context.Background(), input, nil) if err != nil { log.Fatal(err) } fmt.Println(result.State.Text)}Run the graph
Section titled “Run the graph”go run .You should see this greeting.
Hello, Ada!Change s.Name = "Ada" to s.Name = "Sam" and run the program again. The input update sets the starting state, the node fills in Text, and result.State contains the final greeting.
Try another step
Section titled “Try another step”- Choose a provider to call a model from your graph.
- Connect nodes and branch to send different inputs down different paths.