Local tool execution

Learn how to enable your agents to execute local code

Often times, tool definitions will rely on importing code from other files or packages:

1def my_tool():
2 # import code from other files
3 from my_repo.subfolder1.module import my_function
4
5 # import packages
6 import cowsay
7
8 # custom code

To ensure that your tools are able to run, you need to make sure that the files and packages they rely on are accessible from the Letta server. When running Letta locally, the tools are executed inside of the Docker container running the Letta service, and the files and packages they rely on must be accessible from the Docker container.

Importing modules from external files

Tool definitions will often rely on importing code from other files. For example, say you have a repo with the following structure:

my_repo/
├── requirements.txt
├── subfolder1/
└── module.py

We want to import code from module.py in a custom tool as follows:

1def my_tool():
2 from my_repo.subfolder1.module import my_function # MUST be inside the function scope
3 return my_function()

Any imports MUST be inside the function scope, since only the code inside the function scope is executed.
To ensure you can properly import my_function, you need to mount your repository in the Docker container and also explicitly set the location of tool execution by setting the TOOL_EXEC_DIR environment variable.

1docker run \
2 -v /path/to/my_repo:/app/my_repo \ # mount the volume
3 -e TOOL_EXEC_DIR="/app/my_repo" \ # specify the directory
4 -v ~/.letta/.persist/pgdata:/var/lib/postgresql/data \
5 -p 8283:8283 \
6 letta/letta:latest

This will ensure that tools are executed inside of /app/my_repo and the files inside of my_repo are accessible via the volume.

Specifying pip packages

You can specify packages to be installed in the tool execution environment by setting the TOOL_EXEC_VENV_NAME environment variable. This will enable Letta to explicitly create a virtual environment and and install packages specified by requirements.txt at the server start time.

1docker run \
2 -v /path/to/my_repo:/app/my_repo \ # mount the volume
3 -e TOOL_EXEC_DIR="/app/my_repo" \ # specify the directory
4 -e TOOL_EXEC_VENV_NAME="env" \ # specify the virtual environment name
5 -v ~/.letta/.persist/pgdata:/var/lib/postgresql/data \
6 -p 8283:8283 \
7 letta/letta:latest

This will ensure that the packages specified in /app/my_repo/requirements.txt are installed in the virtual environment where the tools are executed.

Letta needs to create and link the virtual environment, so do not create a virtual environment manually with the same name as TOOL_EXEC_VENV_NAME.

Attaching the tool to an agent

Now, you can create a tool that imports modules from your tool execution directory or from the packages specified in requirements.txt. When defining custom tools, make sure you have a properly formatting docstring (so it can be parsed into the OpenAI tool schema) or use the args_schema parameter to specify the arguments for the tool.

1from letta_client import Letta
2
3def my_tool(my_arg: str) -> str:
4 """
5 A custom tool that imports code from other files and packages.
6
7 Args:
8 my_arg (str): A string argument
9 """
10 # import code from other files
11 from my_repo.subfolder1.module import my_function
12
13 # import packages
14 import cowsay
15
16 # custom code
17 return my_function(my_arg)
18
19client = Letta(base_url="http://localhost:8283")
20
21# create the tool
22tool = client.tools.upsert_from_function(
23 func=my_tool
24)
25
26# create the agent with the tool
27agent = client.agents.create(
28 memory_blocks=[
29 {"label": "human", "limit": 2000, "value": "Name: Bob"},
30 {"label": "persona", "limit": 2000, "value": "You are a friendly agent"}
31 ],
32 model="openai/gpt-4o-mini",
33 embedding="openai/text-embedding-3-small",
34 tool_ids=[tool.id]
35)

See more on creating custom tools here.