These TypeScript examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.
TypeScript Sequential Flow
Runs a two-step Ax flow against OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- typescript src/examples/typescript/flows/flow-openai.ts - Source: src/examples/typescript/flows/flow-openai.ts
import { AxAIOpenAIModel, ai, flow } from '@ax-llm/ax';
const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}
const llm = ai({
name: 'openai',
apiKey,
config: {
model: AxAIOpenAIModel.GPT54Mini,
temperature: 0,
},
});
const workflow = flow<{ documentText: string }>()
.description(
'TypeScript Sequential Flow',
'Runs a two-step Ax flow against OpenAI.'
)
.node('summarizer', 'documentText:string -> summaryText:string')
.node(
'classifier',
'textContent:string -> priority:class "high, normal, low"'
)
.execute('summarizer', (state) => ({ documentText: state.documentText }))
.execute('classifier', (state) => ({
textContent: state.summarizerResult.summaryText,
}))
.returns((state) => ({
summary: state.summarizerResult.summaryText as string,
priority: state.classifierResult.priority as string,
}));
const result = await workflow.forward(llm, {
documentText:
'Ax gives developers typed signatures, provider clients, agents, flows, tracing, and optimization so LLM features can be built as ordinary programs.',
});
console.log(JSON.stringify(result, null, 2));TypeScript 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 -- typescript src/examples/typescript/flows/branch-flow.ts - Source: src/examples/typescript/flows/branch-flow.ts
import { AxAIOpenAIModel, ai, flow } from '@ax-llm/ax';
const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}
const llm = ai({
name: 'openai',
apiKey,
config: {
model: AxAIOpenAIModel.GPT54Mini,
temperature: 0,
},
});
const workflow = flow<{ request: string }>()
.description(
'TypeScript Branching Flow',
'Routes a classification through follow-up flow logic backed by OpenAI.'
)
.node(
'classifier',
'request:string -> route:class "support, sales, engineering"'
)
.node('responder', 'request:string, route:string -> response:string')
.execute('classifier', (state) => ({ request: state.request }))
.execute('responder', (state) => ({
request: state.request,
route: state.classifierResult.route,
}))
.returns((state) => ({
route: state.classifierResult.route as string,
response: state.responderResult.response as string,
}));
const result = await workflow.forward(llm, {
request: 'A customer says checkout is down for their enterprise account.',
});
console.log(JSON.stringify(result, null, 2));TypeScript 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 -- typescript src/examples/typescript/flows/composed-flow.ts - Source: src/examples/typescript/flows/composed-flow.ts
import { AxAIOpenAIModel, ai, flow } from '@ax-llm/ax';
const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}
const llm = ai({
name: 'openai',
apiKey,
config: {
model: AxAIOpenAIModel.GPT54Mini,
temperature: 0,
},
});
const workflow = flow<{ topic: string }>()
.description(
'TypeScript Composed Flow',
'Composes multiple typed programs into one OpenAI-backed flow.'
)
.node('outline', 'topic:string -> outline:string[]')
.node('brief', 'topic:string, outline:string[] -> brief:string')
.execute('outline', (state) => ({ topic: state.topic }))
.execute('brief', (state) => ({
topic: state.topic,
outline: state.outlineResult.outline,
}))
.returns((state) => ({
outline: state.outlineResult.outline as string[],
brief: state.briefResult.brief as string,
}));
const result = await workflow.forward(llm, {
topic:
'How Ax moves from typed generation to agents, flows, and optimization',
});
console.log(JSON.stringify(result, null, 2));