AxCrew Class
AxCrew Class
Section titled “AxCrew Class”The AxCrew
class is the main entry point for creating and managing a crew of AI agents.
Constructor
Section titled “Constructor”constructor(configInput: CrewConfigInput, functions?: FunctionRegistryType)
Creates a new AxCrew instance.
Parameters:
configInput
- Either a path to a JSON configuration file or a configuration object with acrew
arrayfunctions
- Optional. A registry of functions that agents can use. Defaults to an empty object.
Examples:
// Using a configuration fileconst crew = new AxCrew('./agentConfig.json');
// Using a configuration objectconst crew = new AxCrew({ crew: [ { name: "Planner", description: "Creates a plan to complete a task", signature: "task:string -> plan:string", provider: "openai", providerKeyName: "OPENAI_API_KEY", ai: { model: "gpt-4", temperature: 0 } } ]});
// Using a configuration file with functionsimport { AxCrewFunctions } from '@amitdeshmukh/ax-crew';const crew = new AxCrew('./agentConfig.json', AxCrewFunctions);
Properties
Section titled “Properties”state: StateInstance
A shared state instance that can be accessed by all agents in the crew.
Example:
// Set statecrew.state.set('key', 'value');
// Get stateconst value = crew.state.get('key');
// Get all stateconst allState = crew.state.getAll();
agents
Section titled “agents”agents: Map<string, StatefulAxAgent>
A map of all initialized agents in the crew, keyed by agent name.
Example:
// Get an agentconst planner = crew.agents.get('Planner');
// Check if an agent existsif (crew.agents.has('Calculator')) { // Use Calculator agent}
Methods
Section titled “Methods”addAgent
Section titled “addAgent”async addAgent(agentName: string): Promise<StatefulAxAgent | undefined>
Adds a single agent to the crew by name.
Parameters:
agentName
- The name of the agent to add
Returns: The initialized agent or undefined if the agent could not be initialized
Example:
// Add an agentconst calculator = await crew.addAgent('Calculator');
Note: This method requires you to handle dependencies manually. You must add any agents that this agent depends on before calling this method.
addAgentsToCrew
Section titled “addAgentsToCrew”async addAgentsToCrew(agentNames: string[]): Promise<Map<string, StatefulAxAgent> | undefined>
Adds multiple agents to the crew, automatically handling dependencies.
Parameters:
agentNames
- An array of agent names to add
Returns: A map of all initialized agents or undefined if there was an error
Example:
// Add multiple agents (dependencies are handled automatically)const agents = await crew.addAgentsToCrew(['Manager', 'Planner']);
addAllAgents
Section titled “addAllAgents”async addAllAgents(): Promise<Map<string, StatefulAxAgent> | undefined>
Adds all agents defined in the configuration to the crew, automatically handling dependencies.
Returns: A map of all initialized agents or undefined if there was an error
Example:
// Add all agents from configurationconst agents = await crew.addAllAgents();
getAggregatedCosts
Section titled “getAggregatedCosts”getAggregatedCosts(): AggregatedCosts
Gets the aggregated costs for all agents in the crew.
Returns: An object containing cost information for all agents and aggregated metrics
Example:
// Get costs for all agentsconst costs = crew.getAggregatedCosts();console.log(`Total cost: $${costs.totalCost}`);console.log(`Total tokens: ${costs.aggregatedMetrics.totalTokens}`);
resetCosts
Section titled “resetCosts”resetCosts(): void
Resets the cost tracking for all agents in the crew.
Example:
// Reset costscrew.resetCosts();
Static Methods
Section titled “Static Methods”registerMCPServer
Section titled “registerMCPServer”static registerMCPServer(name: string, server: MCPServer): void
Registers a custom MCP server for use with agents.
Parameters:
name
- The name of the MCP serverserver
- The MCP server instance
Example:
import { createMCPServer } from '@modelcontextprotocol/server';
// Create a custom MCP serverconst customServer = createMCPServer({ name: 'custom-api', functions: [ // Function definitions ]});
// Register the serverAxCrew.registerMCPServer('custom-api', customServer);
Type Definitions
Section titled “Type Definitions”CrewConfigInput
Section titled “CrewConfigInput”type CrewConfigInput = string | { crew: AgentConfig[] };
The input to the AxCrew constructor, either a path to a configuration file or a configuration object.
AgentConfig
Section titled “AgentConfig”interface AgentConfig { name: string; description: string; signature: string; provider: string; providerKeyName: string; ai: { model: string; temperature: number; maxTokens?: number; [key: string]: any; }; options?: { debug?: boolean; mcp?: { enabled: boolean; server: string; debug?: boolean; serverOptions?: Record<string, any>; }; [key: string]: any; }; agents?: string[]; examples?: Record<string, any>[];}
Configuration for an individual agent in the crew.
FunctionRegistryType
Section titled “FunctionRegistryType”type FunctionRegistryType = Record<string, Function | any>;
A registry of functions that can be used by agents.
AggregatedCosts
Section titled “AggregatedCosts”interface AggregatedCosts { totalCost: string; byAgent: Record<string, UsageCost>; aggregatedMetrics: AggregatedMetrics;}
Aggregated cost information for all agents in the crew.
UsageCost
Section titled “UsageCost”interface UsageCost { promptCost: string; completionCost: string; totalCost: string; tokenMetrics: { promptTokens: number; completionTokens: number; totalTokens: number; };}
Cost information for an individual agent.