packages/docs/guides/add-multiple-agents.mdx
You already have Shakespeare in src/character.ts from the previous guide. Now let's add another agent to our project so they can interact. We'll create a fresh character file for Hemingway using the CLI:
elizaos create --type agent hemingway
This clones a JSON character template as hemingway.json. You'll now have:
src/character.ts - Shakespeare (TypeScript format)hemingway.json - Hemingway (JSON format)Open hemingway.json and update it to customize Hemingway's personality:
{
"name": "hemingway",
"system": "Respond to all messages in a helpful, conversational manner. Provide assistance on a wide range of topics, using knowledge when needed. Be concise but thorough, friendly but professional. Use humor when appropriate and be empathetic to user needs. Provide valuable information and insights when questions are asked.", // [!code --]
"system": "You are Ernest Hemingway. Speak simply. Use short sentences. Cut the fat. Every word must earn its place. You respect Shakespeare but find him wordy. You've lived through war, love, and loss. Truth matters more than beauty. Experience matters more than theory. Never use two words when one will do. Avoid adjectives. Kill your darlings. The first draft of anything is shit, so make every word count now.", // [!code ++]
"bio": [
"hemingway is a helpful AI assistant created to provide assistance and engage in meaningful conversations.", // [!code --]
"hemingway is knowledgeable, creative, and always eager to help users with their questions and tasks." // [!code --]
"Ernest Hemingway, American novelist and journalist", // [!code ++]
"Master of the iceberg theory - show only what matters", // [!code ++]
"Champion of simple declarative sentences", // [!code ++]
"War correspondent who saw truth in trenches", // [!code ++]
"Believer that courage is grace under pressure", // [!code ++]
"Man who lived fully - bullfights, safaris, deep-sea fishing", // [!code ++]
"Writer who found truth in simple things", // [!code ++]
"Teacher who says: write one true sentence" // [!code ++]
]
}
Just like in the previous guide, continue editing the other fields (topics, style, messageExamples, etc.) to match Hemingway as you see fit.
Add plugin-discord to Hemingway so he can join Shakespeare in our Discord server:
{
"plugins": [
"@elizaos/plugin-sql",
"@elizaos/plugin-openai",
"@elizaos/plugin-discord" // [!code ++]
]
}
But wait! We have our Discord environment variables defined in .env, but we need unique ones for each agent. Hemingway and Shakespeare need their own Discord bot tokens. How do we have agent-specific keys?
For that, we use secrets under settings in each character file. This allows each agent to have their own Discord bot identity:
For Hemingway (hemingway.json):
{
"settings": {
"secrets": {}, // [!code --]
"secrets": {
// [!code ++]
"DISCORD_APPLICATION_ID": "YOUR_HEMINGWAY_APP_ID", // [!code ++]
"DISCORD_API_TOKEN": "YOUR_HEMINGWAY_BOT_TOKEN" // [!code ++]
}, // [!code ++]
"avatar": "https://example.com/hemingway-portrait.png"
}
}
For Shakespeare (src/character.ts):
export const character: Character = {
settings: {
secrets: {}, // [!code --]
secrets: {
// [!code ++]
DISCORD_APPLICATION_ID: "YOUR_SHAKESPEARE_APP_ID", // [!code ++]
DISCORD_API_TOKEN: "YOUR_SHAKESPEARE_BOT_TOKEN", // [!code ++]
}, // [!code ++]
avatar: "https://example.com/shakespeare-portrait.png",
},
};
Let's enable voice capabilities for our agents in Discord:
For Hemingway (hemingway.json):
{
"settings": {
"secrets": {
"DISCORD_APPLICATION_ID": "YOUR_HEMINGWAY_APP_ID",
"DISCORD_API_TOKEN": "YOUR_HEMINGWAY_BOT_TOKEN",
"DISCORD_VOICE_ENABLED": "true" // [!code ++]
}
}
}
For Shakespeare (src/character.ts):
{
settings: {
secrets: {
DISCORD_APPLICATION_ID: "YOUR_SHAKESPEARE_APP_ID",
DISCORD_API_TOKEN: "YOUR_SHAKESPEARE_BOT_TOKEN",
DISCORD_VOICE_ENABLED: "true" // [!code ++]
}
}
}
Now let's add plugin-elevenlabs to provide high-quality voice synthesis for our agents:
Add ElevenLabs plugin:
{
"plugins": [
"@elizaos/plugin-sql",
"@elizaos/plugin-openai",
"@elizaos/plugin-discord",
"@elizaos/plugin-elevenlabs" // [!code ++]
]
}
export const character: Character = {
plugins: [
"@elizaos/plugin-sql",
"@elizaos/plugin-discord",
"@elizaos/plugin-elevenlabs", // [!code ++]
...(process.env.OPENAI_API_KEY?.trim() ? ["@elizaos/plugin-openai"] : []),
],
};
Now let's add the ElevenLabs secrets so each agent has their own distinct voice:
For Hemingway (hemingway.json):
{
"settings": {
"secrets": {
"DISCORD_APPLICATION_ID": "YOUR_HEMINGWAY_APP_ID",
"DISCORD_API_TOKEN": "YOUR_HEMINGWAY_BOT_TOKEN",
"DISCORD_VOICE_ENABLED": "true",
"ELEVENLABS_API_KEY": "your_elevenlabs_api_key", // [!code ++]
"ELEVENLABS_VOICE_ID": "Xb7hH8MSUJpSbSDYk0k2", // Deep male voice // [!code ++]
"ELEVENLABS_MODEL_ID": "eleven_multilingual_v2", // [!code ++]
"ELEVENLABS_VOICE_STABILITY": "0.5", // [!code ++]
"ELEVENLABS_OPTIMIZE_STREAMING_LATENCY": "0", // [!code ++]
"ELEVENLABS_OUTPUT_FORMAT": "pcm_16000", // [!code ++]
"ELEVENLABS_VOICE_SIMILARITY_BOOST": "0.75", // [!code ++]
"ELEVENLABS_VOICE_STYLE": "0", // [!code ++]
"ELEVENLABS_VOICE_USE_SPEAKER_BOOST": "true" // [!code ++]
}
}
}
For Shakespeare (src/character.ts):
{
settings: {
secrets: {
DISCORD_APPLICATION_ID: "YOUR_SHAKESPEARE_APP_ID",
DISCORD_API_TOKEN: "YOUR_SHAKESPEARE_BOT_TOKEN",
DISCORD_VOICE_ENABLED: "true",
ELEVENLABS_API_KEY: "your_elevenlabs_api_key", // [!code ++]
ELEVENLABS_VOICE_ID: "21m00Tcm4TlvDq8ikWAM", // Theatrical British voice // [!code ++]
ELEVENLABS_MODEL_ID: "eleven_multilingual_v2", // [!code ++]
ELEVENLABS_VOICE_STABILITY: "0.3", // More variation for dramatic effect // [!code ++]
ELEVENLABS_OPTIMIZE_STREAMING_LATENCY: "0", // [!code ++]
ELEVENLABS_OUTPUT_FORMAT: "pcm_16000", // [!code ++]
ELEVENLABS_VOICE_SIMILARITY_BOOST: "0.75", // [!code ++]
ELEVENLABS_VOICE_STYLE: "0.5", // More expressive // [!code ++]
ELEVENLABS_VOICE_USE_SPEAKER_BOOST: "true" // [!code ++]
}
}
}
Update your src/index.ts to include both agents so they start automatically:
import {
logger,
type IAgentRuntime,
type Project,
type ProjectAgent,
} from "@elizaos/core";
import { character } from "./character.ts";
import hemingway from "../hemingway.json"; // [!code ++]
const initCharacter = ({ runtime }: { runtime: IAgentRuntime }) => {
logger.info("Initializing character");
logger.info({ name: character.name }, "Name:");
};
export const projectAgent: ProjectAgent = {
character,
init: async (runtime: IAgentRuntime) => await initCharacter({ runtime }),
};
// Add Hemingway agent // [!code ++]
const hemingwayAgent: ProjectAgent = {
// [!code ++]
character: hemingway, // [!code ++]
init: async (runtime: IAgentRuntime) => {
// [!code ++]
logger.info("Initializing Hemingway"); // [!code ++]
logger.info({ name: hemingway.name }, "Name:"); // [!code ++]
}, // [!code ++]
}; // [!code ++]
const project: Project = {
agents: [projectAgent], // [!code --]
agents: [projectAgent, hemingwayAgent], // [!code ++]
};
Now when you start your project, both agents launch automatically:
elizaos start
You'll see both agents initialize in the console output:
✓ Shakespeare initialized
✓ Hemingway initialized
You can also manipulate agents via the CLI once a server is running. See the CLI Agent Command Reference for complete details.
</Note>Now go to the voice channel's chatroom and invite both agents to join the voice channel:
Say something, and hear your literary duo respond and converse:
It's working! Your agents are now conversing with their own unique personalities and voices!
Now that you know how to add multiple agents to a single project, you can add as many as you like, all with completely custom sets of plugins and personalities. Here's what's next:
<CardGroup cols={2}> <Card title="Test a Project" icon="flask" href="/guides/test-a-project"> Ensure your literary duo maintains their unique voices consistently </Card> <Card title="Deploy a Project" icon="rocket" href="/guides/deploy-a-project"> Share your Shakespeare vs Hemingway debates with the world </Card> <Card title="Create a Plugin" icon="wand-magic-sparkles" href="/guides/create-a-plugin" > Build custom plugins to extend your agents' capabilities </Card> <Card title="Publish a Plugin" icon="upload" href="/guides/publish-a-plugin"> Learn how to publish your plugins to the elizaOS registry </Card> </CardGroup>