Documentation

Build LLM-powered agents
with production-ready TypeScript

DSPy for TypeScript. Working with LLMs is complex—they don't always do what you want. DSPy makes it easier to build amazing things with LLMs. Just define your inputs and outputs (signature) and an efficient prompt is auto-generated and used. Connect together various signatures to build complex systems and workflows using LLMs.

15+ LLM Providers
End-to-end Streaming
Auto Prompt Tuning

AxFlow

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L70

AxFlow - A fluent, chainable API for building and orchestrating complex, stateful AI programs.

Now with advanced type-safe chaining where each method call evolves the type information, providing compile-time type safety and superior IntelliSense.

Example

const flow = new AxFlow<{ topic: string }, { finalAnswer: string }>()
  .node('summarizer', 'text:string -> summary:string')
  .node('critic', 'summary:string -> critique:string')
  .execute('summarizer', state => ({ text: `About ${state.topic}` })) // state is { topic: string }
  .execute('critic', state => ({ summary: state.summarizerResult.summary })) // state evolves!
  .map(state => ({ finalAnswer: state.criticResult.critique })) // fully typed!

const result = await flow.forward(ai, { topic: "AI safety" })

Type Parameters

Type ParameterDefault type
IN extends Record<string, any>-
OUT-
TNodes extends Record<string, AxProgrammable<any, any>>Record<string, never>
TState extends AxFlowStateIN

Implements

Constructors

Constructor

new AxFlow<IN, OUT, TNodes, TState>(options?: object): AxFlow<IN, OUT, TNodes, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L582

Parameters

ParameterType
options?{ autoParallel?: boolean; batchSize?: number; debug?: boolean; logger?: AxFlowLoggerFunction; }
options.autoParallel?boolean
options.batchSize?number
options.debug?boolean
options.logger?AxFlowLoggerFunction

Returns

AxFlow<IN, OUT, TNodes, TState>

Methods

b()

b(predicate: (_state: TState) => unknown): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1672

Short alias for branch()

Parameters

ParameterType
predicate(_state: TState) => unknown

Returns

this


branch()

branch(predicate: (_state: TState) => unknown): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1655

Starts a conditional branch based on a predicate function.

Example

flow.branch(state => state.qualityResult.needsMoreInfo)
  .when(true)
    .execute('queryGen', ...)
  .when(false)
    .execute('answer', ...)
  .merge()

Parameters

ParameterTypeDescription
predicate(_state: TState) => unknownFunction that takes state and returns a value to branch on

Returns

this

this (for chaining)


derive()

derive<T>(
   outputFieldName: string, 
   inputFieldName: string, 
   transformFn: (value: any, index?: number, state?: TState) => T, 
   options?: object): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2210

Derives a new field from an existing field by applying a transform function.

If the input field contains an array, the transform function is applied to each array element in parallel with batch size control. If the input field contains a scalar value, the transform function is applied directly.

Example

// Parallel processing of array items
flow.derive('processedItems', 'items', (item, index) => processItem(item), { batchSize: 5 })

// Direct transformation of scalar value
flow.derive('upperText', 'text', (text) => text.toUpperCase())

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
outputFieldNamestringName of the field to store the result
inputFieldNamestringName of the existing field to transform
transformFn(value: any, index?: number, state?: TState) => TFunction to apply to each element (for arrays) or the value directly (for scalars)
options?{ batchSize?: number; }Options including batch size for parallel processing
options.batchSize?number-

Returns

this

this (for chaining)


e()

e<TNodeName, TAI>(
   nodeName: TNodeName, 
   mapping: (_state: TState) => GetGenIn<TNodes[TNodeName]>, 
dynamicContext?: AxFlowDynamicContext<TAI>): AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1623

Short alias for execute()

Type Parameters

Type Parameter
TNodeName extends string
TAI extends Readonly<AxAIService<unknown, unknown, string>>

Parameters

ParameterType
nodeNameTNodeName
mapping(_state: TState) => GetGenIn<TNodes[TNodeName]>
dynamicContext?AxFlowDynamicContext<TAI>

Returns

AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>


end()

end(): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2184

Short alias for endWhile()

Returns

this


endWhile()

endWhile(): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2118

Marks the end of a loop block.

Returns

this

this (for chaining)


execute()

execute<TNodeName, TAI>(
   nodeName: TNodeName, 
   mapping: (_state: TState) => GetGenIn<TNodes[TNodeName]>, 
dynamicContext?: AxFlowDynamicContext<TAI>): AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1494

Executes a previously defined node with full type safety. The node name must exist in TNodes, and the mapping function is typed based on the node’s signature.

Example

flow.execute('summarizer', state => ({ text: state.originalText }), { ai: cheapAI })

Type Parameters

Type Parameter
TNodeName extends string
TAI extends Readonly<AxAIService<unknown, unknown, string>>

Parameters

ParameterTypeDescription
nodeNameTNodeNameThe name of the node to execute (must exist in TNodes)
mapping(_state: TState) => GetGenIn<TNodes[TNodeName]>Typed function that takes the current state and returns the input for the node
dynamicContext?AxFlowDynamicContext<TAI>Optional object to override the AI service or options for this specific step

Returns

AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>

New AxFlow instance with TState augmented with the node’s result


fb()

fb(
   condition: (_state: TState) => boolean, 
   targetLabel: string, 
   maxIterations: number): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2049

Short alias for feedback()

Parameters

ParameterTypeDefault value
condition(_state: TState) => booleanundefined
targetLabelstringundefined
maxIterationsnumber10

Returns

this


feedback()

feedback(
   condition: (_state: TState) => boolean, 
   targetLabel: string, 
   maxIterations: number): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1993

Creates a feedback loop that jumps back to a labeled step if a condition is met.

Example

flow.label('retry-point')
  .execute('answer', ...)
  .execute('qualityCheck', ...)
  .feedback(state => state.qualityCheckResult.confidence < 0.7, 'retry-point')

Parameters

ParameterTypeDefault valueDescription
condition(_state: TState) => booleanundefinedFunction that returns true to trigger the feedback loop
targetLabelstringundefinedThe label to jump back to
maxIterationsnumber10Maximum number of iterations to prevent infinite loops (default: 10)

Returns

this

this (for chaining)


forward()

forward<T>(
   ai: T, 
   values: IN | AxMessage<IN>[], 
options?: Readonly<AxAIServiceOptions & object & object>): Promise<OUT>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L792

Executes the flow with the given AI service and input values.

This is the main execution method that orchestrates the entire flow execution. It handles several complex aspects:

  1. Dynamic Signature Inference: If the flow was created with a default signature but has nodes defined, it will infer the actual signature from the flow structure.

  2. Execution Mode Selection: Chooses between optimized parallel execution (when auto-parallel is enabled) or sequential execution based on configuration.

  3. State Management: Maintains the evolving state object as it flows through each step, accumulating results and transformations.

  4. Performance Optimization: Uses the execution planner to identify independent operations that can run in parallel, reducing total execution time.

Execution Flow:

Type Parameters

Type Parameter
T extends Readonly<AxAIService<unknown, unknown, string>>

Parameters

ParameterTypeDescription
aiTThe AI service to use as the default for all steps
valuesIN | AxMessage<IN>[]The input values for the flow
options?Readonly<AxAIServiceOptions & object & object>Optional forward options to use as defaults (includes autoParallel override)

Returns

Promise<OUT>

Promise that resolves to the final output

Implementation of

AxFlowable.forward


getExecutionPlan()

getExecutionPlan(): object;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2298

Gets execution plan information for debugging automatic parallelization

Returns

object

Object with execution plan details

NameType
autoParallelEnabledboolean
groups?AxFlowParallelGroup[]
maxParallelismnumber
parallelGroupsnumber
steps?AxFlowExecutionStep[]
totalStepsnumber

getSignature()

getSignature(): AxSignature;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2317

Returns

AxSignature

Implementation of

AxFlowable.getSignature


getTraces()

getTraces(): AxProgramTrace<IN, OUT>[];

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L660

Returns

AxProgramTrace<IN, OUT>[]

Implementation of

AxFlowable.getTraces


getTracesReport()

getTracesReport(): Record<string, AxProgramTrace<any, any>[]>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L734

Gets a detailed trace report broken down by node name. This provides visibility into the execution traces for each node.

Returns

Record<string, AxProgramTrace<any, any>[]>

Object mapping node names to their trace data


getUsage()

getUsage(): AxModelUsage & object[];

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L677

Returns

AxModelUsage & object[]

Implementation of

AxFlowable.getUsage


getUsageReport()

getUsageReport(): Record<string, AxProgramUsage[]>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L718

Gets a detailed usage report broken down by node name. This provides visibility into which nodes are consuming the most tokens.

Returns

Record<string, AxProgramUsage[]>

Object mapping node names to their usage statistics


l()

l(label: string): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1476

Short alias for label()

Parameters

ParameterType
labelstring

Returns

this


label()

label(label: string): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1465

Labels a step for later reference (useful for feedback loops).

Example

flow.label('retry-point')
  .execute('queryGen', ...)

Parameters

ParameterTypeDescription
labelstringThe label to assign to the current step position

Returns

this

this (for chaining, no type change)


m()

Call Signature

m<TNewState>(transform: (_state: TState) => TNewState): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1354

Short alias for map() - supports parallel option and async functions

Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterType
transform(_state: TState) => TNewState
Returns

AxFlow<IN, OUT, TNodes, TNewState>

Call Signature

m<TNewState>(transform: (_state: TState) => Promise<TNewState>): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1358

Short alias for map() - supports parallel option and async functions

Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterType
transform(_state: TState) => Promise<TNewState>
Returns

AxFlow<IN, OUT, TNodes, TNewState>

Call Signature

m<TNewState>(transforms: (_state: TState) => TNewState[], options: object): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1362

Short alias for map() - supports parallel option and async functions

Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterType
transforms(_state: TState) => TNewState[]
options{ parallel: true; }
options.paralleltrue
Returns

AxFlow<IN, OUT, TNodes, TNewState>

Call Signature

m<TNewState>(transforms: (_state: TState) => Promise<TNewState>[], options: object): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1367

Short alias for map() - supports parallel option and async functions

Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterType
transforms(_state: TState) => Promise<TNewState>[]
options{ parallel: true; }
options.paralleltrue
Returns

AxFlow<IN, OUT, TNodes, TNewState>


map()

Call Signature

map<TNewState>(transform: (_state: TState) => TNewState): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1173

Applies a synchronous transformation to the state object. Returns a new AxFlow type with the evolved state.

Example
flow.map(state => ({ ...state, processedText: state.text.toLowerCase() }))
Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterTypeDescription
transform(_state: TState) => TNewStateFunction that takes the current state and returns a new state
Returns

AxFlow<IN, OUT, TNodes, TNewState>

New AxFlow instance with updated TState type

Call Signature

map<TNewState>(transform: (_state: TState) => Promise<TNewState>): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1192

Applies an asynchronous transformation to the state object. Returns a new AxFlow type with the evolved state.

Example
flow.map(async state => ({
  ...state,
  apiResult: await fetchDataFromAPI(state.query)
}))
Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterTypeDescription
transform(_state: TState) => Promise<TNewState>Async function that takes the current state and returns a promise of new state
Returns

AxFlow<IN, OUT, TNodes, TNewState>

New AxFlow instance with updated TState type

Call Signature

map<TNewState>(transforms: (_state: TState) => TNewState[], options: object): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1215

Applies a transformation to the state object with optional parallel execution. When parallel is enabled, the transform function should prepare data for parallel processing. The actual parallel processing happens with the array of transforms provided.

Example
// Parallel map with multiple transforms
flow.map([
  state => ({ ...state, result1: processA(state.data) }),
  state => ({ ...state, result2: processB(state.data) }),
  state => ({ ...state, result3: processC(state.data) })
], { parallel: true })
Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterTypeDescription
transforms(_state: TState) => TNewState[]Array of transformation functions to apply in parallel
options{ parallel: true; }Options including parallel execution configuration
options.paralleltrue-
Returns

AxFlow<IN, OUT, TNodes, TNewState>

New AxFlow instance with updated TState type

Call Signature

map<TNewState>(transforms: (_state: TState) => Promise<TNewState>[], options: object): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1238

Applies async transformations to the state object with optional parallel execution. When parallel is enabled, all async transforms are executed concurrently.

Example
// Parallel async map with multiple transforms
flow.map([
  async state => ({ ...state, result1: await apiCall1(state.data) }),
  async state => ({ ...state, result2: await apiCall2(state.data) }),
  async state => ({ ...state, result3: await apiCall3(state.data) })
], { parallel: true })
Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterTypeDescription
transforms(_state: TState) => Promise<TNewState>[]Array of async transformation functions to apply in parallel
options{ parallel: true; }Options including parallel execution configuration
options.paralleltrue-
Returns

AxFlow<IN, OUT, TNodes, TNewState>

New AxFlow instance with updated TState type

Call Signature

map<TNewState>(transform: (_state: TState) => TNewState | Promise<TNewState>, options?: object): AxFlow<IN, OUT, TNodes, TNewState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1243

Applies a synchronous transformation to the state object. Returns a new AxFlow type with the evolved state.

Example
flow.map(state => ({ ...state, processedText: state.text.toLowerCase() }))
Type Parameters
Type Parameter
TNewState extends AxFlowState
Parameters
ParameterTypeDescription
transform(_state: TState) => TNewState | Promise<TNewState>Function that takes the current state and returns a new state
options?{ parallel?: boolean; }-
options.parallel?boolean-
Returns

AxFlow<IN, OUT, TNodes, TNewState>

New AxFlow instance with updated TState type


mapOutput()

mapOutput<TOutput>(transform: (_state: TState) => TOutput): AxFlow<IN, TOutput, TNodes, TOutput & TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2444

Applies a final transformation to the state object that updates both state and output type. This is specifically designed for terminal transformations that shape the final output.

Example

const result = await flow
  .node('analyzer', 'userQuestion:string -> analysisResult:string')
  .execute('analyzer', state => ({ userQuestion: state.userQuestion }))
  .mapOutput(state => ({
    // Note: Node results are typed as AxFieldValue, so you may need to cast
    finalAnswer: state.analyzerResult.analysisResult as string
  }))
  .forward(ai, { userQuestion: 'test' });

// result is typed as { finalAnswer: string }

Type Parameters

Type Parameter
TOutput

Parameters

ParameterTypeDescription
transform(_state: TState) => TOutputFunction that takes the current state and returns the final output

Returns

AxFlow<IN, TOutput, TNodes, TOutput & TState>

New AxFlow instance with updated OUT and TState types


merge()

merge<TMergedState>(): AxFlow<IN, OUT, TNodes, TMergedState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1731

Merges the results of conditional branches into a single execution path.

This method is called after defining conditional branches with branch() and when() methods. It creates a merge point where the flow continues with the results from whichever branch was executed based on the branch condition.

How conditional merging works:

  1. The branch predicate is evaluated against the current state
  2. The matching branch’s steps are executed sequentially
  3. If no branch matches, the state is returned unchanged
  4. The merged result becomes the new state for subsequent steps

Type safety note: The TMergedState generic allows for type-level tracking of what fields will be available after the merge, though runtime behavior depends on which branch actually executes.

Example

flow
  .branch(state => state.complexity > 0.5)
  .when(true)
    .execute('complexProcessor', state => ({ input: state.text }))
  .when(false)
    .execute('simpleProcessor', state => ({ input: state.text }))
  .merge() // Combines results from either branch

Type Parameters

Type ParameterDefault type
TMergedState extends AxFlowStateTState

Returns

AxFlow<IN, OUT, TNodes, TMergedState>

AxFlow with updated state type reflecting the merged result


mg()

mg<TMergedState>(): AxFlow<IN, OUT, TNodes, TMergedState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1793

Short alias for merge()

Type Parameters

Type ParameterDefault type
TMergedState extends AxFlowStateTState

Returns

AxFlow<IN, OUT, TNodes, TMergedState>


mo()

mo<TOutput>(transform: (_state: TState) => TOutput): AxFlow<IN, TOutput, TNodes, TOutput & TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2490

Short alias for mapOutput()

Type Parameters

Type Parameter
TOutput

Parameters

ParameterType
transform(_state: TState) => TOutput

Returns

AxFlow<IN, TOutput, TNodes, TOutput & TState>


n()

Call Signature

n<TName, TSig>(name: TName, signature: TSig): AxFlow<IN, OUT, TNodes & { [K in string]: InferAxGen<TSig> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1122

Short alias for node() - supports signature strings, AxSignature instances, AxGen instances, and program classes

Type Parameters
Type Parameter
TName extends string
TSig extends string
Parameters
ParameterType
nameTName
signatureTSig
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: InferAxGen<TSig> }, TState>

Call Signature

n<TName>(name: TName, signature: AxSignature): AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1127

Short alias for node() - supports signature strings, AxSignature instances, AxGen instances, and program classes

Type Parameters
Type Parameter
TName extends string
Parameters
ParameterType
nameTName
signatureAxSignature
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>

Call Signature

n<TName, TProgram>(name: TName, programClass: TProgram): AxFlow<IN, OUT, TNodes & { [K in string]: InstanceType<TProgram> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1137

Short alias for node() - supports signature strings, AxSignature instances, AxGen instances, and program classes

Type Parameters
Type Parameter
TName extends string
TProgram extends () => AxProgrammable<any, any>
Parameters
ParameterType
nameTName
programClassTProgram
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: InstanceType<TProgram> }, TState>

Call Signature

n<TName, TProgram>(name: TName, programInstance: TProgram): AxFlow<IN, OUT, TNodes & { [K in string]: TProgram }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1145

Short alias for node() - supports signature strings, AxSignature instances, AxGen instances, and program classes

Type Parameters
Type Parameter
TName extends string
TProgram extends AxProgrammable<any, any, string>
Parameters
ParameterType
nameTName
programInstanceTProgram
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: TProgram }, TState>


node()

Call Signature

node<TName, TSig>(name: TName, signature: TSig): AxFlow<IN, OUT, TNodes & { [K in string]: InferAxGen<TSig> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L956

Declares a reusable computational node using a signature string. Returns a new AxFlow type that tracks this node in the TNodes registry.

Example
flow.node('summarizer', 'text:string -> summary:string')
flow.node('analyzer', 'text:string -> analysis:string, confidence:number', { debug: true })
Type Parameters
Type Parameter
TName extends string
TSig extends string
Parameters
ParameterTypeDescription
nameTNameThe name of the node
signatureTSigSignature string in the same format as AxSignature
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: InferAxGen<TSig> }, TState>

New AxFlow instance with updated TNodes type

Call Signature

node<TName>(name: TName, signature: AxSignature): AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L980

Declares a reusable computational node using an AxSignature instance. This allows using pre-configured signatures in the flow.

Example
const sig = s('text:string -> summary:string')
flow.node('summarizer', sig, { temperature: 0.1 })
Type Parameters
Type Parameter
TName extends string
Parameters
ParameterTypeDescription
nameTNameThe name of the node
signatureAxSignatureAxSignature instance to use for this node
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>

New AxFlow instance with updated TNodes type

Call Signature

node<TName, TProgram>(name: TName, programClass: TProgram): AxFlow<IN, OUT, TNodes & { [K in string]: InstanceType<TProgram> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1006

Declares a reusable computational node using a class that extends AxProgram. This allows using custom program classes in the flow.

Example
class CustomProgram extends AxProgram<{ input: string }, { output: string }> {
  async forward(ai, values) { return { output: values.input.toUpperCase() } }
}
flow.node('custom', CustomProgram)
Type Parameters
Type Parameter
TName extends string
TProgram extends () => AxProgrammable<any, any>
Parameters
ParameterTypeDescription
nameTNameThe name of the node
programClassTProgramClass that extends AxProgram to use for this node
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: InstanceType<TProgram> }, TState>

New AxFlow instance with updated TNodes type

Call Signature

node<TName, TProgram>(name: TName, programInstance: TProgram): AxFlow<IN, OUT, TNodes & { [K in string]: TProgram }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1027

Declares a reusable computational node using an AxProgrammable instance. This allows using pre-configured AxGen instances or other programmable objects in the flow.

Type Parameters
Type Parameter
TName extends string
TProgram extends AxProgrammable<any, any, string>
Parameters
ParameterTypeDescription
nameTNameThe name of the node
programInstanceTProgramThe AxProgrammable instance to use for this node
Returns

AxFlow<IN, OUT, TNodes & { [K in string]: TProgram }, TState>

New AxFlow instance with updated TNodes type


nodeExtended()

nodeExtended<TName>(
   name: TName, 
   baseSignature: 
  | string
  | AxSignature<Record<string, any>, Record<string, any>>, 
extensions: object): AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2346

Creates a new AxFlow node from an existing signature by extending it with additional fields.

Example

// Create a chain-of-thought node
flow.nodeExtended('reasoner', 'question:string -> answer:string', {
  prependOutputs: [
    { name: 'reasoning', type: f.internal(f.string('Step-by-step reasoning')) }
  ]
})

// Create a node with context and confidence
flow.nodeExtended('analyzer', 'input:string -> output:string', {
  appendInputs: [{ name: 'context', type: f.optional(f.string('Context')) }],
  appendOutputs: [{ name: 'confidence', type: f.number('Confidence score') }]
})

Type Parameters

Type Parameter
TName extends string

Parameters

ParameterTypeDescription
nameTNameThe name of the new node
baseSignature| string | AxSignature<Record<string, any>, Record<string, any>>The base signature to extend (string or AxSignature)
extensions{ appendInputs?: object[]; appendOutputs?: object[]; prependInputs?: object[]; prependOutputs?: object[]; }Object defining how to extend the signature
extensions.appendInputs?object[]-
extensions.appendOutputs?object[]-
extensions.prependInputs?object[]-
extensions.prependOutputs?object[]-

Returns

AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>

New AxFlow instance with the extended node


nx()

nx<TName>(
   name: TName, 
   baseSignature: 
  | string
  | AxSignature<Record<string, any>, Record<string, any>>, 
extensions: object): AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2405

Short alias for nodeExtended() - creates nodes with extended signatures

Type Parameters

Type Parameter
TName extends string

Parameters

ParameterType
nameTName
baseSignature| string | AxSignature<Record<string, any>, Record<string, any>>
extensions{ appendInputs?: object[]; appendOutputs?: object[]; prependInputs?: object[]; prependOutputs?: object[]; }
extensions.appendInputs?object[]
extensions.appendOutputs?object[]
extensions.prependInputs?object[]
extensions.prependOutputs?object[]

Returns

AxFlow<IN, OUT, TNodes & { [K in string]: AxGen<AxGenIn, AxGenOut> }, TState>


p()

p(branches: (
  | AxFlowParallelBranch
  | AxFlowTypedParallelBranch<TNodes, TState>)[]): object;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1963

Short alias for parallel()

Parameters

ParameterType
branches( | AxFlowParallelBranch | AxFlowTypedParallelBranch<TNodes, TState>)[]

Returns

object

NameType
merge()(resultKey: TResultKey, mergeFunction: (…_results: unknown[]) => T) =>

parallel()

parallel(branches: (
  | AxFlowParallelBranch
  | AxFlowTypedParallelBranch<TNodes, TState>)[]): object;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1834

Executes multiple operations in parallel and provides a merge method for combining results.

This method enables true parallel execution of independent operations, which is particularly useful for operations like:

How parallel execution works:

  1. Each branch function receives a sub-context for defining operations
  2. All branches are executed simultaneously using Promise.all()
  3. Results are stored in _parallelResults for the merge operation
  4. The merge function combines the results into a single field

Performance benefits:

Example

flow.parallel([
  subFlow => subFlow.execute('retrieve1', state => ({ query: state.query1 })),
  subFlow => subFlow.execute('retrieve2', state => ({ query: state.query2 })),
  subFlow => subFlow.execute('retrieve3', state => ({ query: state.query3 }))
]).merge('documents', (docs1, docs2, docs3) => [...docs1, ...docs2, ...docs3])

Parameters

ParameterTypeDescription
branches( | AxFlowParallelBranch | AxFlowTypedParallelBranch<TNodes, TState>)[]Array of functions that define parallel operations

Returns

object

Object with merge method for combining results

NameType
merge()(resultKey: TResultKey, mergeFunction: (…_results: unknown[]) => T) =>

r()

r<TNewOut>(transform: (_state: TState) => TNewOut): AxFlow<IN, TNewOut, TNodes, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1447

Short alias for returns() - r() is to returns() as m() is to map()

Type Parameters

Type Parameter
TNewOut extends Record<string, unknown>

Parameters

ParameterTypeDescription
transform(_state: TState) => TNewOutFunction that transforms the current state to the final output

Returns

AxFlow<IN, TNewOut, TNodes, TState>

A new flow with the output type set to the transform result


resetTraces()

resetTraces(): void;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L704

Resets trace tracking for the flow. This is called automatically on each forward/streamingForward call.

Returns

void


resetUsage()

resetUsage(): void;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L688

Returns

void

Implementation of

AxFlowable.resetUsage


returns()

returns<TNewOut>(transform: (_state: TState) => TNewOut): AxFlow<IN, TNewOut, TNodes, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1397

Terminal transformation that sets the final output type of the flow. Use this as the last transformation to get proper type inference for the flow result.

Example

const flow = flow<{ input: string }>()
  .map(state => ({ ...state, processed: true }))
  .returns(state => ({
    result: state.processed ? "done" : "pending"
  })) // TypeScript now knows the output is { result: string }

Type Parameters

Type Parameter
TNewOut extends Record<string, unknown>

Parameters

ParameterTypeDescription
transform(_state: TState) => TNewOutFunction that transforms the current state to the final output

Returns

AxFlow<IN, TNewOut, TNodes, TState>

A new flow with the output type set to the transform result


setDemos()

setDemos(demos: readonly AxProgramDemos<IN, OUT>[]): void;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L672

Parameters

ParameterType
demosreadonly AxProgramDemos<IN, OUT>[]

Returns

void

Implementation of

AxFlowable.setDemos


setExamples()

setExamples(examples: Readonly<AxProgramExamples<IN, OUT>>, options?: Readonly<AxSetExamplesOptions>): void;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L642

Parameters

ParameterType
examplesReadonly<AxProgramExamples<IN, OUT>>
options?Readonly<AxSetExamplesOptions>

Returns

void

Implementation of

AxFlowable.setExamples


setId()

setId(id: string): void;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L650

Parameters

ParameterType
idstring

Returns

void

Implementation of

AxFlowable.setId


setParentId()

setParentId(parentId: string): void;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L655

Parameters

ParameterType
parentIdstring

Returns

void

Implementation of

AxFlowable.setParentId


streamingForward()

streamingForward<T>(
   ai: T, 
   values: IN | AxMessage<IN>[], 
options?: Readonly<AxProgramStreamingForwardOptionsWithModels<T>>): AxGenStreamingOut<OUT>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L744

Type Parameters

Type Parameter
T extends Readonly<AxAIService<unknown, unknown, string>>

Parameters

ParameterType
aiT
valuesIN | AxMessage<IN>[]
options?Readonly<AxProgramStreamingForwardOptionsWithModels<T>>

Returns

AxGenStreamingOut<OUT>

Implementation of

AxFlowable.streamingForward


w()

w(value: unknown): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1696

Short alias for when()

Parameters

ParameterType
valueunknown

Returns

this


wh()

wh(condition: (_state: TState) => boolean, maxIterations: number): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2109

Short alias for while()

Parameters

ParameterTypeDefault value
condition(_state: TState) => booleanundefined
maxIterationsnumber100

Returns

this


when()

when(value: unknown): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L1682

Defines a branch case for the current branch context.

Parameters

ParameterTypeDescription
valueunknownThe value to match against the branch predicate result

Returns

this

this (for chaining)


while()

while(condition: (state: TState) => boolean, maxIterations: number): this;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L2071

Marks the beginning of a loop block.

Example

flow.while(state => state.iterations < 3, 10)
  .map(state => ({ ...state, iterations: (state.iterations || 0) + 1 }))
  .endWhile()

Parameters

ParameterTypeDefault valueDescription
condition(state: TState) => booleanundefinedFunction that takes the current state and returns a boolean
maxIterationsnumber100Maximum number of iterations to prevent infinite loops (default: 100)

Returns

this

this (for chaining)


create()

static create<IN, OUT, TNodes, TState>(options?: object): AxFlow<IN, OUT, TNodes, TState>;

Defined in: https://github.com/ax-llm/ax/blob/9a5a7060a48f9eef46efc680b0cdf6b42bff5df2/src/ax/flow/flow.ts#L616

Static factory method to create a new AxFlow instance with proper type safety

Type Parameters

Type ParameterDefault type
IN extends Record<string, any>Record<string, never>
OUTobject
TNodes extends Record<string, AxProgrammable<any, any, string>>Record<string, never>
TState extends AxFlowStateIN

Parameters

ParameterTypeDescription
options?{ autoParallel?: boolean; batchSize?: number; debug?: boolean; logger?: AxFlowLoggerFunction; }Optional configuration for the flow
options.autoParallel?boolean-
options.batchSize?number-
options.debug?boolean-
options.logger?AxFlowLoggerFunction-

Returns

AxFlow<IN, OUT, TNodes, TState>

New AxFlow instance with type-safe defaults