Toolkit
Reference for the Toolkit class.
The Toolkit class provides a way to group and manage multiple tools (functions) together. It handles tool registration, filtering, caching, and execution control.
Import
1from kern.tools.toolkit import ToolkitConstructor
Parameters
| Parameter | Type | Description |
|---|---|---|
name | str | A descriptive name for the toolkit. |
tools | List[Callable] | List of callable functions to include in the toolkit. |
instructions | str | Instructions for using the toolkit. Can be added to agent context. |
add_instructions | bool | Whether to add toolkit instructions to the agent's context. |
include_tools | list[str] | List of tool names to include from the toolkit. If specified, only these tools will be registered. |
exclude_tools | list[str] | List of tool names to exclude from the toolkit. These tools will not be registered. |
requires_confirmation_tools | list[str] | List of tool names that require user confirmation before execution. |
external_execution_required_tools | list[str] | List of tool names that will be executed outside of the agent loop. |
stop_after_tool_call_tools | List[str] | List of tool names that should stop the agent after execution. |
show_result_tools | List[str] | List of tool names whose results should be shown to the user. |
cache_results | bool | Enable in-memory caching of function results. |
cache_ttl | int | Time-to-live for cached results in seconds (default: 1 hour). |
cache_dir | str | Directory to store cache files. Defaults to system temp directory. |
auto_register | bool | Whether to automatically register all tools in the toolkit upon initialization. |
Usage Examples
Basic Toolkit
1from kern.tools.toolkit import Toolkit23class WebSearchTools(Toolkit):4 def __init__(self, **kwargs):5 tools = [6 self.search_web,7 ]8 super().__init__(name="web_search_tools", tools=tools, **kwargs)910 def search_web(self, query: str) -> str:11 """Search the web for information."""12 return f"Searching for: {query}"Toolkit with Instructions
1class CalculatorTools(Toolkit):2 def __init__(self, **kwargs):3 tools = [4 self.add,5 self.subtract,6 self.multiply,7 self.divide,8 ]910 instructions = "Use these tools to perform calculations. Always validate inputs before execution."1112 super().__init__(name="calculator_tools", tools=tools, instructions=instructions, **kwargs)13 14 def add(self, a: float, b: float) -> float:15 """Add two numbers and return the result."""16 return a + b17 18 def subtract(self, a: float, b: float) -> float:19 """Subtract two numbers and return the result."""20 return a - b21 22 def multiply(self, a: float, b: float) -> float:23 """Multiply two numbers and return the result."""24 return a * b25 26 def divide(self, a: float, b: float) -> float:27 """Divide two numbers and return the result."""28 return a / b