Negotiation
advanced15 minnegotiation-swarm.ts
Two agents with opposing goals should converge on a deal — or hand over to arbitration.
What it shows
- Run the
negotiationstrategy with turn order and deadlock handling - Read the agreed terms
The code
negotiation-swarm.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import { Swarm, type NegotiationResult } 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 buyer = new Agent({
name: 'buyer',
model,
instructions: `You are buying a software license. Your budget is $50,000/year.
Open at $30,000/year. Use terms: price, license_seats, support_tier, contract_length.`,
temperature: 0.7,
maxIterations: 5,
});
const seller = new Agent({
name: 'seller',
model,
instructions: `You are selling a software license. Your floor is $40,000/year.
Open at $65,000/year; discount longer contracts. Use terms: price, license_seats, support_tier, contract_length.`,
temperature: 0.7,
maxIterations: 5,
});
const negotiation = new Swarm(cog, {
name: 'license-negotiation',
strategy: 'negotiation',
agents: [buyer, seller],
negotiation: {
maxRounds: 6,
turnOrder: 'round-robin',
onDeadlock: 'arbitrate',
stagnationThreshold: 0.05,
},
});
negotiation.on('negotiation:round', (event) => {
const { round, maxRounds } = event.data as { round: number; maxRounds: number };
console.log(`round ${round}/${maxRounds}`);
});
const result = await negotiation.run({
input: 'Negotiate a software licensing deal: yearly price, seats, support tier and contract length.',
});
const outcome = result.negotiationResult as NegotiationResult | undefined;
if (outcome) {
console.log(`${outcome.outcome} after ${outcome.rounds} rounds and ${outcome.offers.length} offers`);
for (const term of outcome.agreement?.terms ?? []) {
console.log(` ${term.label}: ${JSON.stringify(term.value)}`);
}
}
console.log(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 negotiation-swarm.tsOr run the full example from a clone of the repository (keys in .env):
repository · bash
npx tsx examples/swarms/04-negotiation-swarm.ts