Define and customize tools

Letta Tools

By default, agents in Letta are created with the following tools:

ToolDescription
send_messageSends a message to the human user.
conversation_searchSearch prior conversation history
conversation_search_dateSearch prior conversation history using a date range
archival_memory_insertAdd a memory to archival memory
archival_memory_searchSearch archival memory via embedding search

Custom Tools

You can create custom tools in Letta in Python. When you define the function for the tool, you must make sure to have a properly formatting docstring for Letta to parse the function schema.

1from letta import Letta
2import inspect
3from textwrap import dedent
4from pprint import pprint
5
6client = Letta(token="LETTA_API_KEY")
7
8# define a function with a docstring
9def roll_dice() -> str:
10 """
11 Simulate the roll of a 20-sided die (d20).
12
13 This function generates a random integer between 1 and 20, inclusive,
14 which represents the outcome of a single roll of a d20.
15
16 Returns:
17 str: The result of the die roll.
18 """
19 import random
20
21 dice_role_outcome = random.randint(1, 20)
22 output_string = f"You rolled a {dice_role_outcome}"
23 return output_string
24
25
26def parse_source_code(func) -> str:
27 """Parse the source code of a function and remove indendation"""
28 source_code = dedent(inspect.getsource(func))
29 return source_code
30
31# create the tool
32tool = client.tools.create(
33 source_code=parse_source_code(roll_dice)
34)
35print(f"Created tool {tool.name}")

Adding tools to agents

Once the tool is created, you can add it to an agent by passing the tool name to the tools parameter in the agent creation.

1# create a new agent
2agent = client.agents.create(
3 memory_blocks=[
4 {"label": "human", "limit": 2000, "value": "Name: Bob"},
5 {"label": "persona", "limit": 2000, "value": "You are a friendly agent"}
6 ],
7 llm="openai/gpt-4",
8 embedding="openai/text-embedding-ada-002",
9 tool_ids=[tool.id]
10)

External Tool Libraries

Letta also has early support for adding tools from the external tool library Composio. This integrations works by converting the external library tools into Letta tools.

Composio Tools

To use Letta with Composio tools, make sure you install dependencies with pip install 'letta[external-tools]. Then, make sure you log in to Composio and add any necessary authentication.

$composio login
>composio add github

For example, to use

1# TODO: not sure how this works

Now, you can include the tool to be used with your agent. See a working example here.

Excluding default tools

To skip adding default tools when creating a new agent, you can add in include_default_tools=False to the agent creation:

1# create an agent with no tools
2agent = client.agents.create(
3 include_default_tools=False
4)

This will create an agent with no tools, so the agent will have no ability to manage memory or search conversation history.

Tool Rules (Graphs)

Tool rules allow you to define graph-like constrains on your tools, such as requiring that a tool terminate agent execution or be followed by another tool.

Read more about tool rules here.

Built with