These Java examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.
Java Sequential Flow
Runs a two-step Ax flow against OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- java src/examples/java/flows/SequentialFlowExample.java - Source: src/examples/java/flows/SequentialFlowExample.java
import dev.axllm.ax.*;
import java.nio.file.*;
import java.util.*;
public final class SequentialFlowExample {
static String apiKey() {
String apiKey = System.getenv("OPENAI_API_KEY");
if (apiKey == null || apiKey.isBlank()) apiKey = System.getenv("OPENAI_APIKEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalStateException("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.");
}
return apiKey;
}
static OpenAICompatibleClient client() {
return new OpenAICompatibleClient(
Map.of("api_key", apiKey(), "model", System.getenv().getOrDefault("AX_OPENAI_MODEL", "gpt-5.4-mini"), "model_config", Map.of("temperature", 0.0)));
}
public static void main(String[] args) throws Exception {
AxGen step = Ax.ax("documentText:string -> summaryText:string");
AxFlow program =
Ax.flow(Map.of("id", "examples.sequentialFlow"))
.execute("step", step)
.map("note", state -> Map.of("note", "Mapped flow state after the provider-backed step."))
.returns(Map.of("step", "step", "note", "note"));
Map<String, Object> output = program.forward(client(), Map.of("documentText", "Ax gives developers signatures, provider clients, agents, flows, tracing, and optimization."));
System.out.println(Json.stringify(output));
}
}Java 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 -- java src/examples/java/flows/BranchFlowExample.java - Source: src/examples/java/flows/BranchFlowExample.java
import dev.axllm.ax.*;
import java.nio.file.*;
import java.util.*;
public final class BranchFlowExample {
static String apiKey() {
String apiKey = System.getenv("OPENAI_API_KEY");
if (apiKey == null || apiKey.isBlank()) apiKey = System.getenv("OPENAI_APIKEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalStateException("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.");
}
return apiKey;
}
static OpenAICompatibleClient client() {
return new OpenAICompatibleClient(
Map.of("api_key", apiKey(), "model", System.getenv().getOrDefault("AX_OPENAI_MODEL", "gpt-5.4-mini"), "model_config", Map.of("temperature", 0.0)));
}
public static void main(String[] args) throws Exception {
AxGen classifier = Ax.ax("request:string -> route:class \"support, sales, engineering\"");
AxGen responder = Ax.ax("request:string, route:string -> response:string");
AxFlow program =
Ax.flow(Map.of("id", "examples.branchFlow"))
.execute(
"classifier",
classifier,
Map.of(
"reads", List.of("request"),
"writes", List.of("classifierResult", "route")))
.execute(
"responder",
responder,
Map.of(
"reads", List.of("request", "route"),
"writes", List.of("responderResult", "response")))
.returns(Map.of("route", "route", "response", "response"));
Map<String, Object> output = program.forward(client(), Map.of("request", "A customer says checkout is down for their enterprise account."));
System.out.println(Json.stringify(output));
}
}Java 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 -- java src/examples/java/flows/ParallelFlowExample.java - Source: src/examples/java/flows/ParallelFlowExample.java
import dev.axllm.ax.*;
import java.util.*;
public final class ParallelFlowExample {
static String apiKey() {
String apiKey = System.getenv("OPENAI_API_KEY");
if (apiKey == null || apiKey.isBlank()) apiKey = System.getenv("OPENAI_APIKEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalStateException("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.");
}
return apiKey;
}
static OpenAICompatibleClient client() {
return new OpenAICompatibleClient(
Map.of(
"api_key", apiKey(),
"model", System.getenv().getOrDefault("AX_OPENAI_MODEL", "gpt-5.4-mini"),
"model_config", Map.of("temperature", 0.0)));
}
public static void main(String[] args) throws Exception {
AxGen research = Ax.ax("topicText:string -> factList:string[]");
AxGen audience = Ax.ax("topicText:string -> audienceAngle:string");
AxGen join = Ax.ax("factList:string[], audienceAngle:string -> briefText:string");
AxFlow program =
Ax.flow(Map.of("id", "examples.parallelFlow"))
.execute(
"research",
research,
Map.of(
"reads", List.of("topicText"),
"writes", List.of("researchResult", "factList")))
.execute(
"audience",
audience,
Map.of(
"reads", List.of("topicText"),
"writes", List.of("audienceResult", "audienceAngle")))
.execute(
"join",
join,
Map.of(
"reads", List.of("factList", "audienceAngle"),
"writes", List.of("joinResult", "briefText")))
.returns(Map.of("briefText", "briefText"));
Map<String, Object> output =
program.forward(
client(),
Map.of(
"topicText",
"Why typed contracts make multi-step LLM systems easier to maintain"));
System.out.println(Json.stringify(output));
}
}Java 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 -- java src/examples/java/flows/ComposedFlowExample.java - Source: src/examples/java/flows/ComposedFlowExample.java
import dev.axllm.ax.*;
import java.nio.file.*;
import java.util.*;
public final class ComposedFlowExample {
static String apiKey() {
String apiKey = System.getenv("OPENAI_API_KEY");
if (apiKey == null || apiKey.isBlank()) apiKey = System.getenv("OPENAI_APIKEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalStateException("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.");
}
return apiKey;
}
static OpenAICompatibleClient client() {
return new OpenAICompatibleClient(
Map.of("api_key", apiKey(), "model", System.getenv().getOrDefault("AX_OPENAI_MODEL", "gpt-5.4-mini"), "model_config", Map.of("temperature", 0.0)));
}
public static void main(String[] args) throws Exception {
AxGen step = Ax.ax("topic:string -> outline:string[]");
AxFlow program =
Ax.flow(Map.of("id", "examples.composedFlow"))
.execute("step", step)
.map("note", state -> Map.of("note", "Mapped flow state after the provider-backed step."))
.returns(Map.of("step", "step", "note", "note"));
Map<String, Object> output = program.forward(client(), Map.of("topic", "How Ax moves from typed generation to agents, flows, and optimization"));
System.out.println(Json.stringify(output));
}
}Java 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 -- java src/examples/java/flows/RefineFlowExample.java - Source: src/examples/java/flows/RefineFlowExample.java
import dev.axllm.ax.*;
import java.util.*;
public final class RefineFlowExample {
static String apiKey() {
String apiKey = System.getenv("OPENAI_API_KEY");
if (apiKey == null || apiKey.isBlank()) apiKey = System.getenv("OPENAI_APIKEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalStateException("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.");
}
return apiKey;
}
static OpenAICompatibleClient client() {
return new OpenAICompatibleClient(
Map.of(
"api_key", apiKey(),
"model", System.getenv().getOrDefault("AX_OPENAI_MODEL", "gpt-5.4-mini"),
"model_config", Map.of("temperature", 0.0)));
}
public static void main(String[] args) throws Exception {
AxGen draft = Ax.ax("topicText:string -> draftText:string");
AxGen critique = Ax.ax("draftText:string -> critiqueText:string");
AxGen revise = Ax.ax("draftText:string, critiqueText:string -> revisedText:string");
AxFlow program =
Ax.flow(Map.of("id", "examples.refineFlow"))
.execute(
"draft",
draft,
Map.of(
"reads", List.of("topicText"),
"writes", List.of("draftResult", "draftText")))
.execute(
"critique",
critique,
Map.of(
"reads", List.of("draftText"),
"writes", List.of("critiqueResult", "critiqueText")))
.execute(
"revise",
revise,
Map.of(
"reads", List.of("draftText", "critiqueText"),
"writes", List.of("reviseResult", "revisedText")))
.returns(Map.of("revisedText", "revisedText"));
Map<String, Object> output =
program.forward(
client(),
Map.of("topicText", "Explain automatic flow parallelism to a backend engineer."));
System.out.println(Json.stringify(output));
}
}