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:

1def custom_send_message_to_agent(target_agent_id: str, message_contents: str):
2 """
3 Send a message to a specific Letta agent.
4
5 Args:
6 target_agent_id (str): The identifier of the target Letta agent.
7 message_contents (str): The message to be sent to the target Letta agent.
8 """
9 from letta_client import Letta
10
11 # TODO: point this to the server where the worker agents are running
12 client = Letta(base_url="http://127.0.0.1:8283")
13
14 # message all worker agents async
15 response = client.agents.send_message_async(
16 agent_id=target_agent_id,
17 message=message_contents,
18 )

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

1def trigger_worker_agents():
2 """
3 Trigger worker agents to start their tasks, without waiting for a response.
4 """
5 from letta_client import Letta
6
7 # TODO: point this to the server where the worker agents are running
8 client = Letta(base_url="http://127.0.0.1:8283")
9
10 # message all worker agents async
11 for agent in client.agents.list(tags=["worker"]):
12 response = client.agents.send_message_async(
13 agent_id=agent.id,
14 message="Start my task",
15 )
Built with