Advanced Start
Advanced Start is built from runnable Java examples. The story below follows the same source files that appear under Examples, so code changes start in src/examples/java/.
Java Typed Generation
Start with a typed contract: the model receives named inputs and Ax parses named outputs.
Runs a small typed generation program against OpenAI.
- Level:
beginner - Run:
npm run example -- java src/examples/java/generation/BasicGenerationExample.java - Source: src/examples/java/generation/BasicGenerationExample.java
- More in this group: Generation examples
import dev.axllm.ax.*;
import java.nio.file.*;
import java.util.*;
public final class BasicGenerationExample {
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 program = Ax.ax("question:string -> answer:string");
Map<String, Object> output = program.forward(client(), Map.of("question", "In one sentence, explain Ax as a language-agnostic LLM programming library."));
System.out.println(Json.stringify(output));
}
}Java Grounded Support Agent
Move to an agent when the model needs a runtime loop and a final typed answer.
Answers a support question grounded in a handbook that is kept out of the model prompt via contextFields.
- Level:
beginner - Run:
npm run example -- java src/examples/java/short-agents/BasicAgentExample.java - Source: src/examples/java/short-agents/BasicAgentExample.java
- More in this group: Agents examples
import dev.axllm.ax.*;
import dev.axllm.ax.runtime.quickjs.*;
import java.util.*;
public final class BasicAgentExample {
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)));
}
// The handbook can be arbitrarily large. Listing it in `contextFields` keeps it
// in the agent's runtime so it never inflates the model prompt -- the agent reads
// it through code, not through tokens. That is the whole point of an Ax agent
// over a plain gen() call: the source material stays out of the context window.
static final String HANDBOOK = String.join("\n",
"# Acme Cloud -- Support Handbook",
"",
"## Billing",
"- Invoices are issued on the 1st of each month and are due net-15.",
"- Plan downgrades take effect at the END of the current billing cycle, not immediately.",
"- Refunds are issued to the original payment method within 5 business days.",
"",
"## Access",
"- Seats can be added by any workspace Owner under Settings -> Members.",
"- SSO (SAML) is available on Enterprise; SCIM provisioning is Owner-only.",
"",
"## Incidents",
"- Status and uptime are published at status.acme.example.",
"- Sev-1 incidents page the on-call within 5 minutes; updates post every 30 minutes.",
"",
"## Data",
"- Exports are available in CSV and JSON from Settings -> Data.",
"- Deleted workspaces are recoverable for 30 days, then permanently purged.");
public static void main(String[] args) throws Exception {
AxAgent assistant = Ax.agent(
"question:string, handbook:string -> answer:string, citations:string[] \"Handbook sections the answer relies on\"",
// Keep the handbook in the runtime, out of the prompt.
Map.of("contextFields", List.of("handbook"), "runtime", Map.of("language", "JavaScript")));
try (AxQuickJsCodeRuntime runtime = new AxQuickJsCodeRuntime()) {
Map<String, Object> result = assistant.forward(
client(),
Map.of(
"question", "A customer downgraded their plan today. When does it take effect, and can they get a refund for the current cycle?",
"handbook", HANDBOOK),
Map.of("runtime", runtime, "max_actor_steps", 12));
System.out.println(Json.pretty(result));
}
}
}Java Sequential Flow
Use a flow when the application should own the order of multi-step work.
Runs a two-step Ax flow against OpenAI.
- Level:
beginner - Run:
npm run example -- java src/examples/java/flows/SequentialFlowExample.java - Source: src/examples/java/flows/SequentialFlowExample.java
- More in this group: Flows examples
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 Text To Speech
Add audio when the same provider-backed contract should accept or produce speech.
Generates speech audio through OpenAI.
- Level:
beginner - Run:
npm run example -- java src/examples/java/audio/SpeechAudioExample.java - Source: src/examples/java/audio/SpeechAudioExample.java
- More in this group: Audio examples
import dev.axllm.ax.*;
import java.nio.file.*;
import java.util.*;
import java.util.Base64;
public final class SpeechAudioExample {
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 OpenAIResponsesClient client() {
return new OpenAIResponsesClient(
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 {
OpenAIResponsesClient audio = client();
Map<String, Object> speech = audio.speak(Map.of("text", "Ax turns LLM prompts into typed programs.", "voice", "alloy", "format", "mp3"));
System.out.println(Json.stringify(Map.of("format", speech.get("format"), "audioBytesBase64", String.valueOf(speech.get("audio")).length())));
}
}Java AxGen Optimization
Close the loop by measuring examples and applying optimizer artifacts to the program.
Runs a baseline OpenAI prediction and applies an optimizer artifact.
- Level:
beginner - Run:
npm run example -- java src/examples/java/optimization/AxgenOptimizationExample.java - Source: src/examples/java/optimization/AxgenOptimizationExample.java
- More in this group: Optimization examples
import dev.axllm.ax.*;
import java.nio.file.*;
import java.util.*;
public final class AxgenOptimizationExample {
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)));
}
static final class ExampleOptimizer implements OptimizerEngine {
public String name() { return "example"; }
public String version() { return "1"; }
public Map<String, Object> optimize(Map<String, Object> request) {
return Map.of("componentMap", Map.of("priority::instruction", "Classify operational risk. Use high for production-impacting urgency."), "metadata", Map.of("source", "axgen"));
}
}
public static void main(String[] args) throws Exception {
AxGen program = new AxGen(Ax.s("emailText:string -> priority:class \"high, normal, low\", rationale:string"), Map.of("id", "priority", "instruction", "Classify the email priority."));
Map<String, Object> baseline = program.forward(client(), Map.of("emailText", "Production checkout is failing for enterprise customers."));
Map<String, Object> artifact = program.optimizeWith(new ExampleOptimizer(), List.of(Map.of("emailText", "URGENT: checkout is down", "priority", "high")), Map.of("apply", false));
program.applyOptimization(Json.stringify(artifact));
Map<String, Object> after = program.forward(client(), Map.of("emailText", "Production checkout is failing for enterprise customers."));
System.out.println(Json.stringify(Map.of("baseline", baseline, "after", after)));
}
}