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

ax

Call Signature

function ax<T, ThoughtKey>(signature: T, options?: Readonly<AxAIServiceOptions & object & object>): AxGen<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"] & string extends ThoughtKey ? object : { [P in string]?: string }>;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/template.ts#L110

Creates a type-safe AI generator from a signature string or AxSignature object.

This is the primary way to define AI-powered functions in Ax. The signature string declares input and output fields with their types, which are then used to generate prompts and parse responses.

Signature String Format:

"inputField1: type, inputField2: type -> outputField1: type, outputField2: type"

Supported Field Types:

Type Modifiers:

Field Descriptions: Add descriptions after the type using a string literal:

"question: string 'The user question' -> answer: string 'A helpful response'"

Type Parameters

Type ParameterDefault type
T extends string-
ThoughtKey extends string"thought"

Parameters

ParameterTypeDescription
signatureTEither a signature string or a pre-built AxSignature object
options?Readonly<AxAIServiceOptions & object & object>Optional configuration for the generator

Returns

AxGen<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"] & string extends ThoughtKey ? object : { [P in string]?: string }>

An AxGen instance that can be executed with .forward(ai, inputs)

Examples

const qa = ax('question: string -> answer: string');
const result = await qa.forward(ai, { question: 'What is TypeScript?' });
console.log(result.answer);
const classifier = ax('text: string -> sentiment: class(positive, negative, neutral)');
const result = await classifier.forward(ai, { text: 'I love this!' });
console.log(result.sentiment); // 'positive'
const extractor = ax(`
  document: string ->
  summary: string,
  keywords: string[],
  wordCount: number
`);
const result = await extractor.forward(ai, { document: longText });
const solver = ax('problem: string -> solution: string', {
  thoughtFieldName: 'reasoning'
});
// Enable thinking in forward options to get step-by-step reasoning
const agent = ax('query: string -> response: string');
const result = await agent.forward(ai, { query: 'What is 25 * 4?' }, {
  functions: [{
    name: 'calculate',
    description: 'Perform math calculations',
    parameters: { type: 'object', properties: { expression: { type: 'string' } } },
    func: ({ expression }) => eval(expression)
  }]
});

Call Signature

function ax<TInput, TOutput, ThoughtKey>(signature: AxSignature<TInput, TOutput>, options?: Readonly<AxAIServiceOptions & object & object>): AxGen<TInput, TOutput & string extends ThoughtKey ? object : { [P in string]?: string }>;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/template.ts#L125

Creates a type-safe AI generator from a signature string or AxSignature object.

This is the primary way to define AI-powered functions in Ax. The signature string declares input and output fields with their types, which are then used to generate prompts and parse responses.

Signature String Format:

"inputField1: type, inputField2: type -> outputField1: type, outputField2: type"

Supported Field Types:

Type Modifiers:

Field Descriptions: Add descriptions after the type using a string literal:

"question: string 'The user question' -> answer: string 'A helpful response'"

Type Parameters

Type ParameterDefault type
TInput extends Record<string, any>-
TOutput extends Record<string, any>-
ThoughtKey extends string"thought"

Parameters

ParameterTypeDescription
signatureAxSignature<TInput, TOutput>Either a signature string or a pre-built AxSignature object
options?Readonly<AxAIServiceOptions & object & object>Optional configuration for the generator

Returns

AxGen<TInput, TOutput & string extends ThoughtKey ? object : { [P in string]?: string }>

An AxGen instance that can be executed with .forward(ai, inputs)

Examples

const qa = ax('question: string -> answer: string');
const result = await qa.forward(ai, { question: 'What is TypeScript?' });
console.log(result.answer);
const classifier = ax('text: string -> sentiment: class(positive, negative, neutral)');
const result = await classifier.forward(ai, { text: 'I love this!' });
console.log(result.sentiment); // 'positive'
const extractor = ax(`
  document: string ->
  summary: string,
  keywords: string[],
  wordCount: number
`);
const result = await extractor.forward(ai, { document: longText });
const solver = ax('problem: string -> solution: string', {
  thoughtFieldName: 'reasoning'
});
// Enable thinking in forward options to get step-by-step reasoning
const agent = ax('query: string -> response: string');
const result = await agent.forward(ai, { query: 'What is 25 * 4?' }, {
  functions: [{
    name: 'calculate',
    description: 'Perform math calculations',
    parameters: { type: 'object', properties: { expression: { type: 'string' } } },
    func: ({ expression }) => eval(expression)
  }]
});