Skip to main content

Agents (Multi-Agent Orchestration)

The Agents class enables multi-agent orchestration, allowing multiple agents to work together sequentially or in parallel.

Quickstart

import { Agent, Agents } from 'praisonai';

// Create agents
const researcher = new Agent({ instructions: "Research the topic thoroughly" });
const writer = new Agent({ instructions: "Write based on the research" });

// Orchestrate with array syntax
const agents = new Agents([researcher, writer]);
const results = await agents.start();

Installation

npm install praisonai

Basic Usage

import { Agent, Agents } from 'praisonai';

const agent1 = new Agent({ instructions: "Analyze the data" });
const agent2 = new Agent({ instructions: "Summarize the analysis" });

// Simple array syntax
const agents = new Agents([agent1, agent2]);
await agents.start();

Config Object Syntax

const agents = new Agents({
  agents: [agent1, agent2],
  process: 'sequential',  // or 'parallel'
  verbose: true
});

Process Modes

Sequential (Default)

Agents run one after another, with each agent receiving the output of the previous:
const researcher = new Agent({ 
  name: "Researcher",
  instructions: "Research AI trends" 
});

const writer = new Agent({ 
  name: "Writer",
  instructions: "Write an article based on the research" 
});

const agents = new Agents({
  agents: [researcher, writer],
  process: 'sequential'
});

const results = await agents.start();
// researcher runs first, writer receives researcher's output

Parallel

All agents run simultaneously:
const analyst1 = new Agent({ instructions: "Analyze market A" });
const analyst2 = new Agent({ instructions: "Analyze market B" });
const analyst3 = new Agent({ instructions: "Analyze market C" });

const agents = new Agents({
  agents: [analyst1, analyst2, analyst3],
  process: 'parallel'
});

const results = await agents.start();
// All analysts run at the same time

Configuration Options

interface PraisonAIAgentsConfig {
  // Required
  agents: Agent[];              // Array of Agent instances
  
  // Optional
  tasks?: string[];             // Custom tasks for each agent
  process?: 'sequential' | 'parallel';  // Execution mode
  verbose?: boolean;            // Enable logging
  pretty?: boolean;             // Pretty output formatting
}

Custom Tasks

Override agent instructions with specific tasks:
const agents = new Agents({
  agents: [researcher, writer],
  tasks: [
    "Research the history of artificial intelligence",
    "Write a 500-word summary of the research"
  ]
});

Examples

Research Pipeline

import { Agent, Agents } from 'praisonai';

const researcher = new Agent({
  name: "Researcher",
  instructions: "You are a thorough researcher. Find key facts and data."
});

const analyst = new Agent({
  name: "Analyst", 
  instructions: "You analyze research findings and identify patterns."
});

const writer = new Agent({
  name: "Writer",
  instructions: "You write clear, engaging content based on analysis."
});

const pipeline = new Agents([researcher, analyst, writer]);
const results = await pipeline.start();

console.log("Research:", results[0]);
console.log("Analysis:", results[1]);
console.log("Article:", results[2]);

Parallel Analysis

const sentimentAgent = new Agent({
  instructions: "Analyze the sentiment of the text"
});

const summaryAgent = new Agent({
  instructions: "Summarize the main points"
});

const keywordsAgent = new Agent({
  instructions: "Extract key topics and keywords"
});

const agents = new Agents({
  agents: [sentimentAgent, summaryAgent, keywordsAgent],
  process: 'parallel',
  tasks: [
    "Analyze: 'The product is amazing but expensive'",
    "Summarize: 'The product is amazing but expensive'",
    "Extract keywords: 'The product is amazing but expensive'"
  ]
});

const [sentiment, summary, keywords] = await agents.start();

With Tools

const searchWeb = (query: string) => `Results for: ${query}`;
const analyzeData = (data: string) => `Analysis of: ${data}`;

const researcher = new Agent({
  instructions: "Research using web search",
  tools: [searchWeb]
});

const analyst = new Agent({
  instructions: "Analyze the research data",
  tools: [analyzeData]
});

const agents = new Agents([researcher, analyst]);
await agents.start();

Accessing Results

const agents = new Agents([agent1, agent2, agent3]);
const results = await agents.start();

// Results is an array matching agent order
results.forEach((result, index) => {
  console.log(`Agent ${index + 1}: ${result}`);
});

Legacy Compatibility

Agents is an alias for PraisonAIAgents:
// Both are equivalent
import { Agents, PraisonAIAgents } from 'praisonai';

const a1 = new Agents([agent1, agent2]);
const a2 = new PraisonAIAgents({ agents: [agent1, agent2] });

See Also