Define and customize tools
You can create custom tools in Letta using the Python SDK, as well as via the ADE tool builder.
For your agent to call a tool, Letta constructs an OpenAI tool schema (contained in json_schema
field) from the function you define. Letta can either parse this automatically from a properly formatting docstring, or you can pass in the schema explicitly by providing a Pydantic object that defines the argument schema.
Creating a custom tool
Specifying tools via Pydantic models
To create a custom tool, you can extend the BaseTool
class and specify the following:
name
- The name of the toolargs_schema
- A Pydantic model that defines the arguments for the tooldescription
- A description of the tooltags
- (Optional) A list of tags for the tool to query You must also define arun(..)
method for the tool code that takes in the fields from theargs_schema
.
Below is an example of how to create a tool by extending BaseTool
:
Specifying tools via function docstrings
You can create a tool by passing in a function with a properly formatting docstring specifying the arguments and description of the tool:
The tool creation will return a Tool
object. You can update the tool with client.tools.upsert_from_function(...)
.
Specifying arguments via Pydantic models
To specify the arguments for a complex tool, you can use the args_schema
parameter.
Note: this path for updating tools is currently only supported in Python.
Creating a tool from a file
You can also define a tool from a file that contains source code. For example, you may have the following file:
Then, you can define the tool in Letta via the source_code
parameter:
(Advanced) Accessing Agent State
Tools that use agent_state
currently do not work in the ADE live tool tester (they will error when you press “Run”), however if the tool is correct it will work once you attach it to an agent.
If you need to directly access the state of an agent inside a tool, you can use the reserved agent_state
keyword argument, for example: