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

AxGen

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L146

Extends

Extended by

Type Parameters

Type ParameterDefault type
INany
OUT extends AxGenOutany

Implements

Constructors

Constructor

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/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L170

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()

_forward1(
   ai: Readonly<AxAIService>, 
   values: IN | AxMessage<IN>[], 
options: Readonly<AxProgramForwardOptions<any>>): AxGenStreamingOut<OUT>;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L1651

Parameters

ParameterType
aiReadonly<AxAIService>
valuesIN | AxMessage<IN>[]
optionsReadonly<AxProgramForwardOptions<any>>

Returns

AxGenStreamingOut<OUT>


addAssert()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L261

Parameters

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

Returns

void


addFieldProcessor()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L336

Type Parameters

Type Parameter
K extends string | number | symbol

Parameters

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

Returns

void


addStreamingAssert()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L265

Parameters

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

Returns

void


addStreamingFieldProcessor()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L322

Type Parameters

Type Parameter
K extends string | number | symbol

Parameters

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

Returns

void


applyOptimization()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L307

Parameters

ParameterType
optimizedProgramAxOptimizedProgram<OUT>

Returns

void

Implementation of

AxProgrammable.applyOptimization

Inherited from

AxProgram.applyOptimization


clone()

clone(): AxGen<IN, OUT>;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L150

Returns

AxGen<IN, OUT>


forward()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L1868

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. Assert - Validate 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)
valuesIN | AxMessage<IN>[]Input values matching the signature’s input fields, or an array of AxMessage objects for multi-turn conversations
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

const gen = ax('question: string -> answer: string');
const result = await gen.forward(ai, { question: 'What is 2+2?' });
console.log(result.answer); // "4"
const result = await gen.forward(ai, { question: 'Explain quantum computing' }, {
  maxTokens: 2000,
  temperature: 0.3,
  stream: true
});
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
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


getId()

getId(): string;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L93

Returns

string

Implementation of

AxProgrammable.getId

Inherited from

AxProgram.getId


getInstruction()

getInstruction(): undefined | string;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L217

Returns

undefined | string


getSignature()

getSignature(): AxSignature;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L66

Returns

AxSignature

Implementation of

AxProgrammable.getSignature

Inherited from

AxProgram.getSignature


getTraces()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L148

Returns

AxProgramTrace<IN, OUT>[]

Implementation of

AxProgrammable.getTraces

Inherited from

AxProgram.getTraces


getUsage()

getUsage(): AxModelUsage & object[];

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L162

Returns

AxModelUsage & object[]

Implementation of

AxProgrammable.getUsage

Inherited from

AxProgram.getUsage


namedPrograms()

namedPrograms(): object[];

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L276

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

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

Returns

object[]

Inherited from

AxProgram.namedPrograms


register()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L97

Parameters

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

Returns

void

Inherited from

AxProgram.register


resetUsage()

resetUsage(): void;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L172

Returns

void

Implementation of

AxProgrammable.resetUsage

Inherited from

AxProgram.resetUsage


setDemos()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L181

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()

setDescription(description: string): void;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L84

Parameters

ParameterType
descriptionstring

Returns

void

Inherited from

AxProgram.setDescription


setExamples()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L2126

Parameters

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

Returns

void

Overrides

AxProgram.setExamples


setId()

setId(id: string): void;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L108

Parameters

ParameterType
idstring

Returns

void

Implementation of

AxProgrammable.setId

Inherited from

AxProgram.setId


setInstruction()

setInstruction(instruction: string): void;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L213

Parameters

ParameterType
instructionstring

Returns

void


setSignature()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/program.ts#L70

Parameters

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

Returns

void

Inherited from

AxProgram.setSignature


stop()

stop(): void;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L205

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

Returns

void


streamingForward()

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

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L2037

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

AxProgrammable.streamingForward


updateMeter()

updateMeter(meter?: Meter): void;

Defined in: https://github.com/ax-llm/ax/blob/71ea5064d766efdc031d375243a8e525911833e7/src/ax/dsp/generate.ts#L241

Parameters

ParameterType
meter?Meter

Returns

void