01What are workflows?
A workflow runs a list of agents over one input. AgentSea has three patterns:
sequentialparallelsupervisor02Sequential workflows
The result is the last agent's response. Token counts and iterations are summed across all agents.
import {
WorkflowFactory,
AnthropicProvider,
ToolRegistry,
httpRequestTool,
} from '@lov3kaizen/agentsea-core';
const provider = new AnthropicProvider();
const toolRegistry = new ToolRegistry();
toolRegistry.register(httpRequestTool);
const workflow = WorkflowFactory.create(
{
name: 'content-pipeline',
type: 'sequential',
agents: [
{
name: 'researcher',
description: 'Gathers information on the topic',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Research the topic and gather information.',
tools: [httpRequestTool],
},
{
name: 'writer',
description: 'Drafts the article',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Write a comprehensive article based on the research.',
},
{
name: 'editor',
description: 'Polishes the article',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Edit and polish the article for publication.',
},
],
},
provider,
toolRegistry,
);
const result = await workflow.execute('Write an article about AI agents', context);
console.log(result.content); // the editor's output03Parallel workflows
Every agent receives the same input. The result content lists each agent's response, labeled by agent name, in configuration order.
const workflow = WorkflowFactory.create(
{
name: 'multi-analysis',
type: 'parallel',
agents: [
{
name: 'sentiment-analyzer',
description: 'Scores sentiment',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Analyze the sentiment of the text.',
},
{
name: 'keyword-extractor',
description: 'Extracts keywords',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Extract key topics and keywords.',
},
{
name: 'summarizer',
description: 'Summarizes the text',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Create a concise summary.',
},
],
},
provider,
toolRegistry,
);
const result = await workflow.execute('Analyze this product review: ...', context);
// [sentiment-analyzer]: ...
//
// [keyword-extractor]: ...
//
// [summarizer]: ...
console.log(result.content);04Supervisor workflows
A supervisor workflow starts with the first agent in agents. After each response, the next agent is chosen in this order:
- The response's
nextAgentfield, when an agent sets it. - The first matching rule in
routing.rules. Each rule is acondition(context, response)function and anextAgentname. - The round-robin order, when
routing.strategyis'round-robin'.
When none of these picks an agent, the workflow ends and returns the last response. The loop stops after 10 routing steps.
import { WorkflowFactory, AnthropicProvider, ToolRegistry } from '@lov3kaizen/agentsea-core';
const workflow = WorkflowFactory.create(
{
name: 'customer-support',
type: 'supervisor',
agents: [
{
name: 'triage',
description: 'Classifies the request and routes it',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: `You route customer requests. Reply with one word:
technical, billing or general.`,
},
{
name: 'technical-support',
description: 'Technical troubleshooting',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Provide technical support and troubleshooting.',
tools: [databaseQueryTool],
},
{
name: 'billing-support',
description: 'Billing and payment questions',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Handle billing and payment inquiries.',
tools: [paymentApiTool],
},
{
name: 'general-support',
description: 'General customer support',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'Provide general customer support.',
},
],
routing: {
strategy: 'conditional',
rules: [
{
condition: (_context, response) => response.content.includes('technical'),
nextAgent: 'technical-support',
},
{
condition: (_context, response) => response.content.includes('billing'),
nextAgent: 'billing-support',
},
{
condition: () => true,
nextAgent: 'general-support',
},
],
},
},
new AnthropicProvider(),
new ToolRegistry(),
);
const result = await workflow.execute('My credit card payment failed', context);Note
nextAgent set by the agent where you can.05Error handling
Set errorHandling on the workflow config. It is one string value:
'fail-fast' (default)'retry'retry.maxAttempts times. If every attempt fails, the error propagates.'fallback'error.'continue'const workflow = WorkflowFactory.create(
{
name: 'resilient-workflow',
type: 'sequential',
agents: [/* ... */],
errorHandling: 'continue',
},
provider,
toolRegistry,
);
try {
const result = await workflow.execute(input, context);
console.log(result.content);
} catch (error) {
console.error('Workflow failed:', error);
}06Retry strategy
With errorHandling: 'retry', the agent is re-invoked after a failure. Tune the attempts and backoff with the retry config:
const workflow = WorkflowFactory.create(
{
name: 'resilient-workflow',
type: 'sequential',
agents: [/* ... */],
errorHandling: 'retry',
retry: {
maxAttempts: 5, // total attempts, including the first
initialDelayMs: 500, // base delay between attempts
backoff: 'exponential', // 'linear' | 'exponential'
maxDelayMs: 10000, // cap on the computed delay
},
},
provider,
toolRegistry,
);When retry is omitted, the defaults are 3 attempts with no delay. The delay applies only when initialDelayMs is greater than 0. linear waits initialDelayMs * attempt. exponential waits initialDelayMs * 2 ** (attempt - 1). maxDelayMs caps either value.
07Context and results
A workflow takes the same AgentContext as an agent. Its conversationId and memory store are used by every agent in the workflow.
const context = {
conversationId: 'workflow-123',
sessionData: {
userId: '12345',
userPreferences: { language: 'en', timezone: 'UTC' },
},
history: [],
};
const result = await workflow.execute('Create a report', context);
console.log(result.content);
console.log(result.metadata.tokensUsed);
console.log(result.metadata.latencyMs);
console.log(result.metadata.iterations);Note
executeStream() on an individual Agent.08Workflow factory
WorkflowFactory.create(config, provider, toolRegistry, memory?) returns the workflow class that matches config.type. The types are sequential, parallel and supervisor. A custom workflow must be created with its class directly.
import { SupervisorWorkflow } from '@lov3kaizen/agentsea-core';
// The same config as the factory, constructed directly
const workflow = new SupervisorWorkflow(config, provider, toolRegistry, memory);09Use cases
Sequential
- Content pipelines: research, write, edit
- Data processing: extract, transform, load
- Multi-step analysis: collect, analyze, report
Parallel
- Multi-perspective analysis: sentiment, keywords, summary
- Competitive research across several companies
- Generating several variations to compare
Supervisor
- Customer support routing
- Dynamic task delegation
- Multi-round problem solving
10Best practices
- Specialize agents: give each agent one clear role and a specific system prompt.
- Choose an error strategy for the use case. Use
retryfor flaky external calls andfallbackwhen a partial answer is acceptable. - Route with clear signals: have agents set
nextAgentor return a structured answer that a rule can read. - Mind the loop limit: supervisor workflows stop after 10 routing steps. Design routing so the loop ends before that.
- Share context through sessionData: pass what the agents need in the context.
- Track cost and latency: record
tokensUsedandlatencyMsfrom each run.