Skip to main content

Overview

The Orchestrator class from @agentlib/orchestrator enables you to coordinate multiple agents, running them in parallel or sequentially based on your workflow needs. It uses a planner agent to decide how to delegate tasks to specialized sub-agents.

Constructor

import { Orchestrator } from '@agentlib/orchestrator'

const orchestrator = new Orchestrator(planner, config)
planner
AgentInstance
required
The planner agent that coordinates sub-agents. This agent receives special tools to invoke and manage other agents.
config
OrchestratorConfig
required
Configuration object for the orchestrator.

Methods

run()

Execute the planner agent with the given input.
const result = await orchestrator.run(input)
input
string
required
The user prompt or task to execute.
output
string
The final response from the planner agent.
state
ExecutionState
The execution state containing steps, messages, and token usage.

invokeAgent()

Manually invoke a sub-agent with a prompt. The agent runs asynchronously in parallel.
await orchestrator.invokeAgent('researcher', 'Find information about AI safety')
name
string
required
Name of the agent to invoke (must exist in the agents config).
prompt
string
required
The prompt to send to the agent.

waitAgent()

Wait for a specific agent to complete and retrieve its output.
const output = await orchestrator.waitAgent('researcher')
name
string
required
Name of the agent to wait for.
output
string
The agent’s final output.

waitAll()

Wait for all currently running agents to complete.
const results = await orchestrator.waitAll()
// { researcher: '...', writer: '...', ... }
results
Record<string, string>
Map of agent names to their outputs.

getAgentState()

Get the current state of a specific agent.
const state = orchestrator.getAgentState('researcher')
name
string
required
Name of the agent.
state
AgentState | undefined

getAllStates()

Get the current states of all agents.
const states = orchestrator.getAllStates()
states
AgentState[]
Array of all agent states.

clearAgentHistory()

Clear the memory/history of a specific agent.
await orchestrator.clearAgentHistory('researcher')
name
string
required
Name of the agent whose history should be cleared.

getHistory()

Retrieve aggregated history from all sub-agents.
const history = await orchestrator.getHistory()
history
ModelMessage[]
Array of messages representing the last activity from each agent (if extendedSubBuffers is enabled).

Built-in Planner Tools

When exposeAgentsAsTools is true (default), the planner automatically receives these tools:

invoke_agent

Launch an agent with a specific prompt. The agent runs in parallel.
{
  "agentName": "researcher",
  "prompt": "Research AI safety principles"
}

wait_for_agent

Wait for a specific agent to finish and retrieve its result.
{
  "agentName": "researcher"
}

wait_all_agents

Wait for all currently running agents to complete.

get_agent_state

Get the current state and last output of a specific agent.
{
  "agentName": "researcher"
}

get_all_agent_states

Get the current status of all agents.

clear_agent_history

Clear the temporary memory of a specific agent.
{
  "agentName": "researcher"
}

Events

The orchestrator extends EventEmitter and emits the following events:
planner:step
{ step: any }
Emitted when the planner takes a reasoning step.
agent:invoke
{ agent: string; prompt: string }
Emitted when a sub-agent is invoked.
agent:completed
{ agent: string; output: string }
Emitted when a sub-agent completes execution.
agent:step
{ agent: string; step: any }
Emitted when a sub-agent takes a reasoning step.
step:summary
StepSummary
Emitted when an agent step is summarized (if summarize is enabled).
finished
{ output: string }
Emitted when the orchestrator completes execution.

Example

import { defineAgent } from '@agentlib/core'
import { Orchestrator } from '@agentlib/orchestrator'
import { openai } from '@agentlib/openai'

// Define specialized agents
const researcher = defineAgent({
  name: 'researcher',
  description: 'Researches topics and gathers information',
  model: openai('gpt-4'),
})

const writer = defineAgent({
  name: 'writer',
  description: 'Writes content based on research',
  model: openai('gpt-4'),
})

// Define planner agent
const planner = defineAgent({
  name: 'planner',
  description: 'Coordinates research and writing tasks',
  model: openai('gpt-4'),
})

// Create orchestrator
const orchestrator = new Orchestrator(planner, {
  agents: {
    researcher,
    writer,
  },
  summarize: true,
  context: {
    project: 'AI Documentation',
    audience: 'developers',
  },
})

// Listen to events
orchestrator.on('agent:step', ({ agent, step }) => {
  console.log(`[${agent}]`, step.type)
})

// Execute
const result = await orchestrator.run(
  'Write a comprehensive guide about transformer architectures'
)

console.log(result.output)

Orchestration Patterns

Sequential Execution

The planner invokes agents one at a time, waiting for results before proceeding:
1. invoke_agent(researcher, prompt)
2. wait_for_agent(researcher)
3. invoke_agent(writer, prompt_with_research)
4. wait_for_agent(writer)

Parallel Execution

The planner invokes multiple agents, then waits for all to complete:
1. invoke_agent(researcher_A, prompt_A)
2. invoke_agent(researcher_B, prompt_B)
3. wait_for_agent(researcher_A)
4. wait_for_agent(researcher_B)
5. invoke_agent(writer, combined_prompt)
6. wait_for_agent(writer)

Mixed Execution

Combine parallel and sequential patterns as needed:
1. invoke_agent(researcher_A, prompt_A)
2. invoke_agent(researcher_B, prompt_B)
3. wait_all_agents()
4. invoke_agent(analyzer, analysis_prompt)
5. wait_for_agent(analyzer)

Build docs developers (and LLMs) love