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

agent

Call Signature

function agent<T>(signature: T, config: AxAgentConfig<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"]>): AxAgent<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"]>;

Defined in: https://github.com/ax-llm/ax/blob/8dfd0ce02b8cb386fb2c93fa280a2ec0da2d6011/src/ax/prompts/agent.ts#L682

Creates a strongly-typed AI agent from a signature. This is the recommended way to create agents, providing better type inference and cleaner syntax. Supports both string signatures and AxSignature objects.

Example

// Using string signature
const myAgent = agent('userInput:string -> responseText:string', {
  name: 'myAgent',
  description: 'An agent that processes user input and returns a response',
  definition: 'You are a helpful assistant that responds to user queries...'
});

// Using AxSignature object
const sig = s('userInput:string -> responseText:string');
const myAgent2 = agent(sig, {
  name: 'myAgent2',
  description: 'Same agent but using AxSignature object'
});

// With child agents
const parentAgent = agent('taskDescription:string -> completedTask:string', {
  name: 'parentAgent',
  description: 'Coordinates child agents to complete tasks',
  agents: [childAgent1, childAgent2]
});

// Type-safe usage
const result = await myAgent.forward(ai, { userInput: 'Hello!' });
console.log(result.responseText); // TypeScript knows this exists

Type Parameters

Type Parameter
T extends string

Parameters

ParameterTypeDescription
signatureTThe input/output signature as a string or AxSignature object
configAxAgentConfig<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"]>Configuration options for the agent

Returns

AxAgent<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"]>

A typed agent instance

Call Signature

function agent<TInput, TOutput>(signature: AxSignature<TInput, TOutput>, config: AxAgentConfig<TInput, TOutput>): AxAgent<TInput, TOutput>;

Defined in: https://github.com/ax-llm/ax/blob/8dfd0ce02b8cb386fb2c93fa280a2ec0da2d6011/src/ax/prompts/agent.ts#L689

Creates a strongly-typed AI agent from a signature. This is the recommended way to create agents, providing better type inference and cleaner syntax. Supports both string signatures and AxSignature objects.

Example

// Using string signature
const myAgent = agent('userInput:string -> responseText:string', {
  name: 'myAgent',
  description: 'An agent that processes user input and returns a response',
  definition: 'You are a helpful assistant that responds to user queries...'
});

// Using AxSignature object
const sig = s('userInput:string -> responseText:string');
const myAgent2 = agent(sig, {
  name: 'myAgent2',
  description: 'Same agent but using AxSignature object'
});

// With child agents
const parentAgent = agent('taskDescription:string -> completedTask:string', {
  name: 'parentAgent',
  description: 'Coordinates child agents to complete tasks',
  agents: [childAgent1, childAgent2]
});

// Type-safe usage
const result = await myAgent.forward(ai, { userInput: 'Hello!' });
console.log(result.responseText); // TypeScript knows this exists

Type Parameters

Type Parameter
TInput extends Record<string, any>
TOutput extends Record<string, any>

Parameters

ParameterTypeDescription
signatureAxSignature<TInput, TOutput>The input/output signature as a string or AxSignature object
configAxAgentConfig<TInput, TOutput>Configuration options for the agent

Returns

AxAgent<TInput, TOutput>

A typed agent instance