AxGen Generated TypeScript API reference. typescript api api/reference build/apidocs/Class.AxGen.md class AxGen

AxGen

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L149

Extends

Type Parameters

Type ParameterDefault type
INany
OUT extends AxGenOutany

Implements

Constructors

Constructor

TypeScript
new AxGen<IN, OUT>(signature: 
  | string
  | Readonly<AxSignatureConfig>
| Readonly<AxSignature<IN & Record<string, any>, OUT>>, options?: Readonly<AxProgramForwardOptions<any>>): AxGen<IN, OUT>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L180

Parameters

ParameterType
signature| string | Readonly<AxSignatureConfig> | Readonly<AxSignature<IN & Record<string, any>, OUT>>
options?Readonly<AxProgramForwardOptions<any>>

Returns

AxGen<IN, OUT>

Overrides

AxProgram.constructor

Methods

_forward1()

TypeScript
_forward1(
   ai: Readonly<AxAIService>, 
   values: IN, 
options: Readonly<AxProgramForwardOptions<any>>): AxGenStreamingOut<OUT>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L2402

Parameters

ParameterType
aiReadonly<AxAIService>
valuesIN
optionsReadonly<AxProgramForwardOptions<any>>

Returns

AxGenStreamingOut<OUT>


_measurePromptCharsForInternalUse()

TypeScript
_measurePromptCharsForInternalUse(
   ai: Readonly<AxAIService>, 
   values: IN, 
options?: Readonly<Partial<Omit<AxProgramForwardOptions<any>, "functions">>>): Promise<AxPromptMetrics>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L558

Internal

Parameters

ParameterType
aiReadonly<AxAIService>
valuesIN
options?Readonly<Partial<Omit<AxProgramForwardOptions<any>, "functions">>>

Returns

Promise<AxPromptMetrics>


addAssert()

TypeScript
addAssert(fn: (values: OUT) => 
  | undefined
  | string
  | boolean
  | Promise<undefined | string | boolean>, message?: string): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L611

Parameters

ParameterType
fn(values: OUT) => | undefined | string | boolean | Promise<undefined | string | boolean>
message?string

Returns

void


addFieldProcessor()

TypeScript
addFieldProcessor<K>(fieldName: K, fn: (value: OUT[K], context?: object) => unknown): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L686

Type Parameters

Type Parameter
K extends string | number | symbol

Parameters

ParameterType
fieldNameK
fn(value: OUT[K], context?: object) => unknown

Returns

void


addStreamingAssert()

TypeScript
addStreamingAssert(
   fieldName: keyof OUT, 
   fn: (content: string, done?: boolean) => 
  | undefined
  | string
  | boolean
  | Promise<undefined | string | boolean>, 
   message?: string): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L615

Parameters

ParameterType
fieldNamekeyof OUT
fn(content: string, done?: boolean) => | undefined | string | boolean | Promise<undefined | string | boolean>
message?string

Returns

void


addStreamingFieldProcessor()

TypeScript
addStreamingFieldProcessor<K>(fieldName: K, fn: (value: string, context?: object) => unknown): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L672

Type Parameters

Type Parameter
K extends string | number | symbol

Parameters

ParameterType
fieldNameK
fn(value: string, context?: object) => unknown

Returns

void


applyOptimization()

TypeScript
applyOptimization(optimizedProgram: AxOptimizedProgram<OUT>): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L370

Parameters

ParameterType
optimizedProgramAxOptimizedProgram<OUT>

Returns

void

Implementation of

AxProgrammable.applyOptimization

Inherited from

AxProgram.applyOptimization


applyOptimizedComponents()

TypeScript
applyOptimizedComponents(updates: Readonly<Record<string, string>>): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L444

Broadcast component updates across this subtree. Each node filters keys belonging to itself and dispatches via applyLocalOptimizedComponents.

Parameters

ParameterType
updatesReadonly<Record<string, string>>

Returns

void

Implementation of

AxProgrammable.applyOptimizedComponents

Inherited from

AxProgram.applyOptimizedComponents


clearInstruction()

TypeScript
clearInstruction(): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L235

Returns

void


clone()

TypeScript
clone(): AxGen<IN, OUT>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L153

Returns

AxGen<IN, OUT>


forward()

TypeScript
forward<T>(
   ai: T, 
   values: IN, 
options?: Readonly<AxProgramForwardOptionsWithModels<T>>): Promise<OUT>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L2626

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

This is the main entry point for running an AI generation. The execution pipeline:

  1. Validate - Check input values match the signature
  2. Render - Build the prompt from signature, examples, and inputs
  3. Call - Send the request to the AI service
  4. Parse - Extract structured outputs from the response
  5. Validate - Validate parsed outputs and retry with error correction if needed

Type Parameters

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

Parameters

ParameterTypeDescription
aiTThe AI service instance to use (created via ai() factory)
valuesINInput values matching the signature’s input fields
options?Readonly<AxProgramForwardOptionsWithModels<T>>Optional execution configuration

Returns

Promise<OUT>

Promise resolving to the output values matching the signature’s output fields

Throws

When input values don’t match the signature

Throws

When output parsing/validation fails after all retries

Throws

When the AI service request fails

Examples

TypeScript
const gen = ax('question: string -> answer: string');
const result = await gen.forward(ai, { question: 'What is 2+2?' });
console.log(result.answer); // "4"
TypeScript
const result = await gen.forward(ai, { question: 'Explain quantum computing' }, {
  maxTokens: 2000,
  temperature: 0.3,
  stream: true
});
TypeScript
const mem = new AxMemory();
const chat = ax('message: string -> reply: string');

await chat.forward(ai, { message: 'Hi, my name is Alice' }, { mem });
const result = await chat.forward(ai, { message: 'What is my name?' }, { mem });
// result.reply will reference "Alice" from conversation history
TypeScript
const result = await gen.forward(ai, values, {
  functions: [{
    name: 'getWeather',
    description: 'Get current weather for a city',
    parameters: {
      type: 'object',
      properties: { city: { type: 'string', description: 'City name' } },
      required: ['city']
    },
    func: async ({ city }) => fetchWeather(city)
  }],
  maxSteps: 5
});

Implementation of

AxProgrammable.forward


getChatLog()

TypeScript
getChatLog(): readonly AxChatLogEntry[];

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L705

Get the normalized chat log from the last forward() call. One entry per AI chat round-trip, with roles normalized to system/user/assistant/tool and inline XML for thinking and tool calls.

Returns

readonly AxChatLogEntry[]

Implementation of

AxProgrammable.getChatLog

Overrides

AxProgram.getChatLog


getId()

TypeScript
getId(): string;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L96

Returns

string

Implementation of

AxProgrammable.getId

Inherited from

AxProgram.getId


getInstruction()

TypeScript
getInstruction(): undefined | string;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L231

Returns

undefined | string


getOptimizableComponents()

TypeScript
getOptimizableComponents(): readonly AxOptimizableComponent[];

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L393

Walks the program tree and emits one AxOptimizableComponent per string-valued artifact reachable from this node. Subclasses override localOptimizableComponents() to add their own; tree traversal is handled here so callers never need to recurse.

Returns

readonly AxOptimizableComponent[]

Implementation of

AxProgrammable.getOptimizableComponents

Inherited from

AxProgram.getOptimizableComponents


getSignature()

TypeScript
getSignature(): AxSignature;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L69

Returns

AxSignature

Implementation of

AxProgrammable.getSignature

Inherited from

AxProgram.getSignature


getTraces()

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

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L151

Returns

AxProgramTrace<IN, OUT>[]

Implementation of

AxProgrammable.getTraces

Inherited from

AxProgram.getTraces


getUsage()

TypeScript
getUsage(): AxModelUsage & object[];

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L165

Returns

AxModelUsage & object[]

Implementation of

AxProgrammable.getUsage

Inherited from

AxProgram.getUsage


namedProgramInstances()

TypeScript
namedProgramInstances(): AxNamedProgramInstance<IN, OUT>[];

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L337

Returns

AxNamedProgramInstance<IN, OUT>[]

Implementation of

AxProgrammable.namedProgramInstances

Inherited from

AxProgram.namedProgramInstances


namedPrograms()

TypeScript
namedPrograms(): object[];

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L306

Returns all programs in the hierarchy with their IDs and signatures. Use this to discover the IDs needed for setDemos().

Equivalent to DSPy’s named_parameters().

Example

TypeScript
agent.setId('qa');
console.log(agent.namedPrograms());
// [
//   { id: 'qa.actor', signature: '... -> javascriptCode' },
//   { id: 'qa.responder', signature: '... -> answer' },
// ]

Returns

object[]

Inherited from

AxProgram.namedPrograms


register()

TypeScript
register(prog: Readonly<AxTunable<IN, OUT> & AxUsable>, name?: string): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L100

Parameters

ParameterType
progReadonly<AxTunable<IN, OUT> & AxUsable>
name?string

Returns

void

Inherited from

AxProgram.register


resetUsage()

TypeScript
resetUsage(): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L202

Returns

void

Implementation of

AxProgrammable.resetUsage

Inherited from

AxProgram.resetUsage


setDemos()

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

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L211

Parameters

ParameterType
demosreadonly AxProgramDemos<IN, OUT>[]
options?{ modelConfig?: Record<string, unknown>; }
options.modelConfig?Record<string, unknown>

Returns

void

Implementation of

AxProgrammable.setDemos

Inherited from

AxProgram.setDemos


setDescription()

TypeScript
setDescription(description: string): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L87

Parameters

ParameterType
descriptionstring

Returns

void

Inherited from

AxProgram.setDescription


setExamples()

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

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L2900

Parameters

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

Returns

void

Overrides

AxProgram.setExamples


setId()

TypeScript
setId(id: string): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L111

Parameters

ParameterType
idstring

Returns

void

Implementation of

AxProgrammable.setId

Inherited from

AxProgram.setId


setInstruction()

TypeScript
setInstruction(instruction: string): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L227

Parameters

ParameterType
instructionstring

Returns

void


setSignature()

TypeScript
setSignature(signature: 
  | string
  | Readonly<AxSignature<Record<string, any>, Record<string, any>>>
  | Readonly<AxSignatureConfig>): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/program.ts#L73

Parameters

ParameterType
signature| string | Readonly<AxSignature<Record<string, any>, Record<string, any>>> | Readonly<AxSignatureConfig>

Returns

void

Inherited from

AxProgram.setSignature


stop()

TypeScript
stop(): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L219

Stops an in-flight generation. Causes forward() / streamingForward() to throw AxAIServiceAbortedError.

Returns

void


streamingForward()

TypeScript
streamingForward<T>(
   ai: T, 
   values: IN, 
options?: Readonly<AxProgramStreamingForwardOptionsWithModels<T>>): AxGenStreamingOut<OUT>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L2802

Type Parameters

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

Parameters

ParameterType
aiT
valuesIN
options?Readonly<AxProgramStreamingForwardOptionsWithModels<T>>

Returns

AxGenStreamingOut<OUT>

Implementation of

AxProgrammable.streamingForward


updateMeter()

TypeScript
updateMeter(meter?: Meter): void;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/generate.ts#L591

Parameters

ParameterType
meter?Meter

Returns

void

Docs