These Go examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.
Go Sequential Flow
Runs a two-step Ax flow against OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- go src/examples/go/flows/sequential_flow.go - Source: src/examples/go/flows/sequential_flow.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" { apiKey = os.Getenv("OPENAI_APIKEY") }
if apiKey == "" { panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.") }
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" { model = "gpt-5.4-mini" }
return ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": apiKey, "model": model, "model_config": ax.Object("temperature", 0)})
}
func printJSON(value ax.Value) {
data, err := json.MarshalIndent(value, "", " ")
if err != nil { panic(err) }
fmt.Println(string(data))
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client := openAIClient()
step := ax.NewAx("documentText:string -> summaryText:string", nil)
program := ax.NewFlow(map[string]ax.Value{"id": "examples.sequentialFlow"}).
Execute("step", step, nil).
Returns(map[string]ax.Value{"step": "step"})
output, err := program.Forward(ctx, client, map[string]ax.Value{"documentText": "Ax gives developers signatures, provider clients, agents, flows, tracing, and optimization."}, nil)
if err != nil { panic(err) }
printJSON(output)
}Go Branching Flow
Routes a classification through follow-up flow logic backed by OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- go src/examples/go/flows/branch_flow.go - Source: src/examples/go/flows/branch_flow.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
apiKey = os.Getenv("OPENAI_APIKEY")
}
if apiKey == "" {
panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.")
}
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" {
model = "gpt-5.4-mini"
}
return ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": apiKey, "model": model, "model_config": ax.Object("temperature", 0)})
}
func printJSON(value ax.Value) {
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client := openAIClient()
classifier := ax.NewAx("request:string -> route:class \"support, sales, engineering\"", nil)
responder := ax.NewAx("request:string, route:string -> response:string", nil)
program := ax.NewFlow(map[string]ax.Value{"id": "examples.branchFlow"}).
Execute("classifier", classifier, map[string]ax.Value{
"reads": ax.Array("request"), "writes": ax.Array("classifierResult", "route"),
}).
Execute("responder", responder, map[string]ax.Value{
"reads": ax.Array("request", "route"), "writes": ax.Array("responderResult", "response"),
}).
Returns(map[string]ax.Value{"route": "route", "response": "response"})
output, err := program.Forward(ctx, client, map[string]ax.Value{"request": "A customer says checkout is down for their enterprise account."}, nil)
if err != nil {
panic(err)
}
printJSON(output)
}Go Parallel Flow
Runs two independent OpenAI-backed steps in parallel before joining their results.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- go src/examples/go/flows/parallel-flow.go - Source: src/examples/go/flows/parallel-flow.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
apiKey = os.Getenv("OPENAI_APIKEY")
}
if apiKey == "" {
panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.")
}
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" {
model = "gpt-5.4-mini"
}
return ax.NewOpenAICompatibleClient(map[string]ax.Value{
"api_key": apiKey,
"model": model,
"model_config": ax.Object("temperature", 0),
})
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
research := ax.NewAx("topicText:string -> factList:string[]", nil)
audience := ax.NewAx("topicText:string -> audienceAngle:string", nil)
join := ax.NewAx("factList:string[], audienceAngle:string -> briefText:string", nil)
program := ax.NewFlow(map[string]ax.Value{"id": "examples.parallelFlow"}).
Execute("research", research, map[string]ax.Value{
"reads": ax.Array("topicText"), "writes": ax.Array("researchResult", "factList"),
}).
Execute("audience", audience, map[string]ax.Value{
"reads": ax.Array("topicText"), "writes": ax.Array("audienceResult", "audienceAngle"),
}).
Execute("join", join, map[string]ax.Value{
"reads": ax.Array("factList", "audienceAngle"), "writes": ax.Array("joinResult", "briefText"),
}).
Returns(map[string]ax.Value{"briefText": "briefText"})
output, err := program.Forward(
ctx,
openAIClient(),
map[string]ax.Value{"topicText": "Why typed contracts make multi-step LLM systems easier to maintain"},
nil,
)
if err != nil {
panic(err)
}
data, err := json.MarshalIndent(output, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}Go Composed Flow
Composes multiple typed programs into one OpenAI-backed flow.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- go src/examples/go/flows/composed_flow.go - Source: src/examples/go/flows/composed_flow.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" { apiKey = os.Getenv("OPENAI_APIKEY") }
if apiKey == "" { panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.") }
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" { model = "gpt-5.4-mini" }
return ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": apiKey, "model": model, "model_config": ax.Object("temperature", 0)})
}
func printJSON(value ax.Value) {
data, err := json.MarshalIndent(value, "", " ")
if err != nil { panic(err) }
fmt.Println(string(data))
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client := openAIClient()
step := ax.NewAx("topic:string -> outline:string[]", nil)
program := ax.NewFlow(map[string]ax.Value{"id": "examples.composedFlow"}).
Execute("step", step, nil).
Returns(map[string]ax.Value{"step": "step"})
output, err := program.Forward(ctx, client, map[string]ax.Value{"topic": "How Ax moves from typed generation to agents, flows, and optimization"}, nil)
if err != nil { panic(err) }
printJSON(output)
}Go Refinement Flow
Drafts, critiques, and revises an answer through three OpenAI-backed steps.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- go src/examples/go/flows/refine-flow.go - Source: src/examples/go/flows/refine-flow.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
apiKey = os.Getenv("OPENAI_APIKEY")
}
if apiKey == "" {
panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.")
}
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" {
model = "gpt-5.4-mini"
}
return ax.NewOpenAICompatibleClient(map[string]ax.Value{
"api_key": apiKey,
"model": model,
"model_config": ax.Object("temperature", 0),
})
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
draft := ax.NewAx("topicText:string -> draftText:string", nil)
critique := ax.NewAx("draftText:string -> critiqueText:string", nil)
revise := ax.NewAx("draftText:string, critiqueText:string -> revisedText:string", nil)
program := ax.NewFlow(map[string]ax.Value{"id": "examples.refineFlow"}).
Execute("draft", draft, map[string]ax.Value{
"reads": ax.Array("topicText"), "writes": ax.Array("draftResult", "draftText"),
}).
Execute("critique", critique, map[string]ax.Value{
"reads": ax.Array("draftText"), "writes": ax.Array("critiqueResult", "critiqueText"),
}).
Execute("revise", revise, map[string]ax.Value{
"reads": ax.Array("draftText", "critiqueText"), "writes": ax.Array("reviseResult", "revisedText"),
}).
Returns(map[string]ax.Value{"revisedText": "revisedText"})
output, err := program.Forward(
ctx,
openAIClient(),
map[string]ax.Value{"topicText": "Explain automatic flow parallelism to a backend engineer."},
nil,
)
if err != nil {
panic(err)
}
data, err := json.MarshalIndent(output, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}