Creating Tool Rules

Tool rules allows developer to define constrains on their tools, such as requiring that a tool terminate agent execution or be followed by another tool.

Letta currently supports the following tool rules (with more being added):

  • TerminalToolRule(tool_name=...)
    • If the tool is called, the agent ends execution
  • InitToolRule(tool_name=...)
    • The tool must be called first when an agent is run
  • ChildToolRule(tool_name=..., children=[...])
    • If the tool is called, it must be followed by one of the tools specified in children
  • ParentToolRule(tool_name=..., children=[...])
    • The tool must be called before the tools specified in children can be called
  • ConditionalToolRule(tool_name=..., child_output_mapping={...})
    • If the tool is called, it must be followed by one of the tools specified in children based off the tool’s output
  • ContinueToolRule(tool_name=...)
    • If the tool is called, the agent must continue execution
  • MaxCountPerStepToolRule(tool_name=..., max_count_limit=...)
    • The tool cannot be called more than max_count_limit times in a single step

Default tool rules

Depending on your agent configuration, there may be default tool rules applied to improve performance.

Tool rule examples

For example, you can ensure that the agent will stop execution after the roll_d20 tool is called by specifying tool rules in the agent creation:

1// create a new agent
2const agentState = await client.createAgent({
3 // create the agent with an additional tool
4 tools: [tool.name],
5 // add tool rules that terminate execution after specific tools
6 toolRules: [
7 // exit after roll_d20 is called
8 {toolName: tool.name, type: "exit_loop"},
9 ],
10});
11
12console.log(`Created agent with name ${agentState.name} with tools ${agentState.tools}`);

You can see a full working example of tool rules here.