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

AxLearn

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L147

AxLearn wraps an AxGen with automatic trace logging and self-improvement capabilities.

Example

const gen = ax(`question -> answer`);

// Create the learner with all configuration
const learner = new AxLearn(gen, {
  name: 'math-bot',
  teacher: gpt4o,
  storage: new AxMemoryStorage(),
  budget: 20
});

// Use in production
await learner.forward(ai, { question: 'What is 2+2?' });

// Run optimization (uses config from constructor)
await learner.optimize();

Type Parameters

Type Parameter
IN extends AxGenIn
OUT extends AxGenOut

Implements

Constructors

Constructor

new AxLearn<IN, OUT>(gen: AxGen<IN, OUT>, options: AxLearnOptions): AxLearn<IN, OUT>;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L155

Parameters

ParameterType
genAxGen<IN, OUT>
optionsAxLearnOptions

Returns

AxLearn<IN, OUT>

Methods

addAssert()

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

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L224

Parameters

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

Returns

void


addFeedback()

addFeedback(traceId: string, feedback: object): Promise<void>;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L464

Add feedback to a specific trace.

Parameters

ParameterType
traceIdstring
feedback{ comment?: string; label?: string; score?: number; }
feedback.comment?string
feedback.label?string
feedback.score?number

Returns

Promise<void>


addFieldProcessor()

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

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L236

Parameters

ParameterType
fieldNamekeyof OUT
fn(value: OUT[keyof OUT], 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/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L228

Parameters

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

Returns

void


addStreamingFieldProcessor()

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

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L246

Parameters

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

Returns

void


clone()

clone(): AxLearn<IN, OUT>;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L256

Returns

AxLearn<IN, OUT>


forward()

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

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L174

Forward call - behaves exactly like AxGen.forward() but logs traces.

Parameters

ParameterType
aiAxAIService
valuesIN | AxMessage<IN>[]
options?Readonly<AxProgramForwardOptions<string>>

Returns

Promise<OUT>

Implementation of

AxForwardable.forward


getGen()

getGen(): AxGen<IN, OUT>;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L437

Get the underlying AxGen instance.

Returns

AxGen<IN, OUT>


getInstruction()

getInstruction(): undefined | string;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L216

Returns

undefined | string


getSignature()

getSignature(): AxSignature;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L208

Returns

AxSignature


getStorage()

getStorage(): AxStorage;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L444

Get the storage backend.

Returns

AxStorage


getTraces()

getTraces(options?: object): Promise<AxTrace[]>;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L451

Get recent traces for this agent.

Parameters

ParameterType
options?{ limit?: number; since?: Date; }
options.limit?number
options.since?Date

Returns

Promise<AxTrace[]>


getUsage()

getUsage(): AxModelUsage & object[];

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L200

Returns

AxModelUsage & object[]

Implementation of

AxUsable.getUsage


optimize()

optimize(overrides: Partial<Omit<AxLearnOptions, "id" | "storage" | "teacher">>): Promise<AxLearnResult<IN, OUT>>;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L266

Optimize the agent using the configuration provided in constructor. Can optionally override options.

Parameters

ParameterType
overridesPartial<Omit<AxLearnOptions, "id" | "storage" | "teacher">>

Returns

Promise<AxLearnResult<IN, OUT>>


resetUsage()

resetUsage(): void;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L204

Returns

void

Implementation of

AxUsable.resetUsage


setInstruction()

setInstruction(instruction: string): void;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L212

Parameters

ParameterType
instructionstring

Returns

void


streamingForward()

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

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L188

Streaming forward call - behaves exactly like AxGen.streamingForward() but logs traces.

Parameters

ParameterType
aiAxAIService
valuesIN | AxMessage<IN>[]
options?Readonly<AxProgramForwardOptions<string>>

Returns

AxGenStreamingOut<OUT>

Implementation of

AxForwardable.streamingForward


updateMeter()

updateMeter(meter?: Meter): void;

Defined in: https://github.com/ax-llm/ax/blob/242cf18d21de9f9d58c7c82f53305f0605497473/src/ax/dsp/learn.ts#L220

Parameters

ParameterType
meter?Meter

Returns

void