Content Pipeline
easy10 minpipeline-swarm.ts
Research, writing and editing are different jobs. Each agent should do one and pass its output on.
What it shows
- Chain agents as
pipelinestages - Read every stage’s output from
pipelineOutputs
The code
pipeline-swarm.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import { Swarm } from '@cogitator-ai/swarms';
const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');
const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });
const model = 'google/gemini-3.5-flash-lite';
const researcher = new Agent({
name: 'researcher',
model,
instructions: 'Produce a research brief: 3-5 key facts, statistics and expert opinions. Plain text.',
temperature: 0.5,
maxIterations: 1,
});
const writer = new Agent({
name: 'writer',
model,
instructions: 'Turn the research into a 300-word article with a hook, body and conclusion. Plain text.',
temperature: 0.7,
maxIterations: 1,
});
const editor = new Agent({
name: 'editor',
model,
instructions: 'Fix grammar and flow, tighten sentences and add a title. Return the final article.',
temperature: 0.3,
maxIterations: 1,
});
const pipeline = new Swarm(cog, {
name: 'content-pipeline',
strategy: 'pipeline',
pipeline: {
stages: [
{ name: 'research', agent: researcher },
{ name: 'writing', agent: writer },
{ name: 'editing', agent: editor },
],
},
});
pipeline.on('pipeline:stage', (event) => {
const { index, name, total } = event.data as { index: number; name: string; total: number };
console.log(`stage ${index + 1}/${total}: ${name}`);
});
const result = await pipeline.run({
input: 'Write an article about edge computing and why it matters for AI inference.',
});
for (const [stage, output] of result.pipelineOutputs ?? []) {
console.log(`--- ${stage}: ${String(output).slice(0, 100)}...`);
}
console.log('\nFinal article:\n', result.output);
await cog.close();Run it
install · bash
pnpm add @cogitator-ai/core @cogitator-ai/swarmsEnvironment
GOOGLE_API_KEYrun · bash
GOOGLE_API_KEY=your-key npx tsx pipeline-swarm.tsOr run the full example from a clone of the repository (keys in .env):
repository · bash
npx tsx examples/swarms/02-pipeline-swarm.ts