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. We added the following tool rules:

  • 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
  • ToolRule(tool_name=..., children=[...]) - If the tool is called, it must be followed by one of the tools specified in children
By default, the send_message tool is marked with TerminalToolRule, since you usually do not want to agent to continue executing after it has send message to the user.

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

Python
1# create a new agent
2agent_state = client.create_agent(
3 # create the agent with an additional tool
4 tools=[tool.name],
5 # add tool rules that terminate execution after specific tools
6 tool_rules=[
7 # exit after roll_d20 is called
8 TerminalToolRule(tool_name=tool.name),
9 # exit after send_message is called (default behavior)
10 TerminalToolRule(tool_name="send_message"),
11 ],
12)
13print(f"Created agent with name {agent_state.name} with tools {agent_state.tools}")

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

Built with