Define and customize tools

Letta has a native integration with Composio, which supports over 7000 pre-made tools to connect with popular services such as Slack, GitHub, Discord, etc. You can also create your own custom tools from scratch using Python.

Default 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
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
2
3client = Letta(token="LETTA_API_KEY")
4
5# define a function with a docstring
6def roll_dice() -> str:
7 """
8 Simulate the roll of a 20-sided die (d20).
9
10 This function generates a random integer between 1 and 20, inclusive,
11 which represents the outcome of a single roll of a d20.
12
13 Returns:
14 str: The result of the die roll.
15 """
16 import random
17
18 dice_role_outcome = random.randint(1, 20)
19 output_string = f"You rolled a {dice_role_outcome}"
20 return output_string
21
22
23# create the tool
24tool = client.tools.create_from_function(
25 func=roll_dice
26)
27print(f"Created tool {tool.name}")

Adding tools to agents in the Python SDK

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)

Tool Environment Variables

You can set agent-scoped environment variables for your tools. These environment variables will be accessible in the sandboxed environment that any of the agent tools are run in.

For example, if you define a custom tool that requires an API key to run (e.g. EXAMPLE_TOOL_API_KEY), you can set the variable at time of agent creation by using the tool_exec_environment_variables parameter:

1curl -X POST http://localhost:8283/v1/agents/ \
2 -H "Content-Type: application/json" \
3 -d '{
4 "llm":"openai/gpt-4",
5 "embedding":"openai/text-embedding-ada-002",
6 "tool_exec_environment_variables": {
7 "EXAMPLE_TOOL_API_KEY": "banana"
8 },
9 "memory_blocks": []
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.

For more information on setting up Composio tools, see our Composio guide.

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)

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

Tool Rules

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