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 Typed Generation
Runs a small typed generation program against OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- go src/examples/go/generation/basic_generation.go - Source: src/examples/go/generation/basic_generation.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()
program := ax.NewAx("question:string -> answer:string", nil)
output, err := program.Forward(ctx, client, map[string]ax.Value{"question": "In one sentence, explain Ax as a language-agnostic LLM programming library."}, nil)
if err != nil { panic(err) }
printJSON(output)
}Go Structured Extraction
Extracts structured fields and labels from support text with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- go src/examples/go/generation/structured_generation.go - Source: src/examples/go/generation/structured_generation.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()
program := ax.NewAx("ticket:string -> priority:class \"high, normal, low\", summary:string, labels:string[]", nil)
output, err := program.Forward(ctx, client, map[string]ax.Value{"ticket": "Checkout has failed for enterprise customers since 09:00. Support wants a concise summary and tags."}, nil)
if err != nil { panic(err) }
printJSON(output)
}Go Signature Constraints
Builds native constrained fields and runs the signature with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- go src/examples/go/generation/signature-constraints.go - Source: src/examples/go/generation/signature-constraints.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()
signature := ax.AxSignature{
Description: "Extract a constrained restaurant booking",
Inputs: []ax.Field{
{
Name: "requestText", Title: "Request Text", Description: "Booking request",
Type: ax.FieldType{Name: "string", MinLength: 10, MaxLength: 500},
},
{
Name: "contactEmail", Title: "Contact Email", Description: "Contact email",
Type: ax.FieldType{Name: "string", Format: "email"},
},
},
Outputs: []ax.Field{
{
Name: "partySize", Title: "Party Size", Description: "Guests",
Type: ax.FieldType{Name: "number", Minimum: 1, Maximum: 12},
},
{
Name: "bookingCode", Title: "Booking Code", Description: "Must look like ABC-1234",
Type: ax.FieldType{
Name: "string", Pattern: `^[A-Z]{3}-\d{4}$`,
PatternDescription: "Three letters, a dash, and four digits",
},
},
},
}
program := ax.NewAx("requestText:string -> partySize:number, bookingCode:string", nil)
program.Signature = signature
output, err := program.Forward(
ctx,
openAIClient(),
map[string]ax.Value{
"requestText": "Book dinner for four people under the name Ada Lovelace.",
"contactEmail": "ada@example.com",
},
nil,
)
if err != nil {
panic(err)
}
data, err := json.MarshalIndent(output, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}Centralized Usage Observer
Attributes every completed model call to a tenant, user, and request from one global observer.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- go src/examples/go/generation/usage_observer.go - Source: src/examples/go/generation/usage_observer.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
ax "github.com/ax-llm/ax/packages/go"
)
func main() {
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"
}
events := []ax.AxUsageEvent{}
ax.SetUsageObserver(func(event ax.AxUsageEvent) {
events = append(events, event)
})
defer ax.SetUsageObserver(nil)
client := ax.NewOpenAICompatibleClient(map[string]ax.Value{
"api_key": apiKey,
"model": model,
"usageContext": ax.Object(
"tenantId", "tenant-42",
"feature", "support-chat",
"attributes", ax.Object("environment", "example"),
),
})
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
_, err := client.Chat(
ctx,
map[string]ax.Value{
"chat_prompt": ax.Array(
ax.Object("role", "user", "content", "Reply with one short greeting."),
),
},
ax.Object(
"usageContext",
ax.Object("userId", "user-7", "requestId", fmt.Sprintf("request-%d", time.Now().UnixNano())),
),
)
if err != nil {
panic(err)
}
output, err := json.MarshalIndent(events, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(output))
}Go Contextual Generation
Answers from supplied context and returns compact citations with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- go src/examples/go/generation/context_generation.go - Source: src/examples/go/generation/context_generation.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()
program := ax.NewAx("context:string, question:string -> answer:string, citations:string[]", nil)
output, err := program.Forward(ctx, client, map[string]ax.Value{"context": "Ax uses signatures, ai(), ax(), agent(), flow(), and optimize().", "question": "How should a new developer think about Ax?"}, nil)
if err != nil { panic(err) }
printJSON(output)
}Go Adaptive Provider Balancing
Routes equivalent chat traffic using shared reliability, latency, and cost statistics.
- Provider:
openai-compatible - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- go src/examples/go/generation/adaptive_balancer.go - Source: src/examples/go/generation/adaptive_balancer.go
package main
import (
"context"
"fmt"
"os"
ax "github.com/ax-llm/ax/packages/go"
)
func main() {
key := os.Getenv("OPENAI_API_KEY")
if key == "" {
key = os.Getenv("OPENAI_APIKEY")
}
if key == "" {
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"
}
services := []ax.AxAIService{
ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": key, "model": model}),
ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": key, "model": model}),
}
store := ax.NewAxInMemoryBalancerStatsStore()
routeKeys := []string{"openai-primary", "openai-backup"}
events := []string{}
strategy := &ax.AxBalancerAdaptiveStrategy{
DeadlineMs: 6_000, BadOutcomeCost: 0.02,
ExpectedTokens: map[string]ax.Value{"promptTokens": 1_200.0, "completionTokens": 300.0},
Namespace: "support-summary-v1", StatsStore: store,
RouteKey: func(_ ax.AxAIService, index int) string { return routeKeys[index] },
Slice: func(context map[string]ax.Value) string {
if options, ok := context["options"].(map[string]ax.Value); ok && options["stream"] == true {
return "streaming"
}
return "interactive"
},
OnRoutingEvent: func(event ax.AxBalancerRoutingEvent) { events = append(events, fmt.Sprint(event["type"])) },
}
balancer, err := ax.NewAxBalancerWithOptions(services, ax.AxBalancerOptions{Strategy: strategy})
if err != nil {
panic(err)
}
response, err := balancer.Chat(context.Background(), map[string]ax.Value{"model": model, "chat_prompt": ax.Array(ax.Object("role", "user", "content", "Summarize why shared routing state matters."))}, nil)
if err != nil {
panic(err)
}
fmt.Println(response)
fmt.Println(events)
}