Building Custom Multi-Agent Tools

We recommend using the pre-made multi-agent messaging tools for most use cases, but advanced users can write custom tools to support complex communication patterns.

You can also write your own agent communication tools by using the Letta API and writing a custom tool in Python. Since Letta runs as a service, you can make request to the server from a custom tool to send messages to other agents via API calls.

Here’s a simple example of a tool that sends a message to a specific agent:

1async function customSendMessageToAgent(targetAgentId: string, messageContents: string) {
2 /**
3 * Send a message to a specific Letta agent.
4 *
5 * @param targetAgentId - The identifier of the target Letta agent.
6 * @param messageContents - The message to be sent to the target Letta agent.
7 */
8 const { LettaClient } = require('@letta-ai/letta-client');
9
10 // TODO: point this to the server where the worker agents are running
11 const client = new LettaClient({baseUrl: "http://127.0.0.1:8283"});
12
13 // message all worker agents async
14 const response = await client.agents.sendMessageAsync(
15 targetAgentId,
16 messageContents
17 );
18}

Below is an example of a tool that triggers agents tagged with worker to start their tasks:

1async function triggerWorkerAgents() {
2 /**
3 * Trigger worker agents to start their tasks, without waiting for a response.
4 */
5 const { LettaClient } = require('@letta-ai/letta-client');
6
7 // TODO: point this to the server where the worker agents are running
8 const client = new LettaClient({baseUrl: "http://127.0.0.1:8283"});
9
10 // message all worker agents async
11 const agents = await client.agents.list({tags: ["worker"]});
12 for (const agent of agents) {
13 const response = await client.agents.sendMessageAsync(
14 agent.id,
15 "Start my task"
16 );
17 }
18}