Overview
Concrete tool for dynamically creating and executing LLM-generated code tools via a code interpreter backend.
Description
The GeneratedCodeTool class wraps LLM-generated source code (e.g., Python) into a callable Tool. When invoked, it executes the stored code through a code executor (Docker or Python interpreter) and passes a generic `payload` object into the code as the `payload` variable. The generated code should assign its final result to a variable named `result`, which is serialized to JSON and returned. This enables the Alita agent pattern — agents that generate their own tools on-the-fly.
Usage
- Dynamic tool creation: Use when an agent needs to create a tool from a natural language description at runtime.
- Code execution workflows: Pair with `DockerInterpreterToolkit` or `PythonInterpreterToolkit` as the code executor backend.
- Alita-style agents: Integrate with `CustomizeAgent` to enable self-generating tool capabilities.
Code Reference
Source Location
Signature
class GeneratedCodeTool(Tool):
name: str = "generated_code_tool"
description: str = "..."
inputs: Dict[str, Dict[str, str]] = {"payload": {"type": "object", "description": "..."}}
required: Optional[List[str]] = []
def __init__(
self,
code_executor: Optional[Tool],
tool_name: str,
description: str,
code: str,
language: str = "python",
)
def __call__(self, payload: dict = None) -> Dict[str, Any]
Import
from evoagentx.tools.alita_agent import GeneratedCodeTool
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| code_executor |
Tool |
Yes |
Code interpreter toolkit (Docker or Python)
|
| tool_name |
str |
Yes |
Name for the generated tool
|
| description |
str |
Yes |
Description of what the tool does
|
| code |
str |
Yes |
LLM-generated source code to execute
|
| language |
str |
No |
Programming language (default "python")
|
| payload |
dict |
No |
Payload object passed to the code as `payload` variable
|
Outputs
| Name |
Type |
Description
|
| __call__ returns |
Dict[str, Any] |
JSON-serialized `result` variable from the code, or error dict
|
Usage Examples
Dynamic Tool from LLM-Generated Code
from evoagentx.tools.alita_agent import GeneratedCodeTool
from evoagentx.tools.interpreter_python import PythonInterpreterToolkit
# 1. Prepare a code executor toolkit
executor = PythonInterpreterToolkit()
# 2. Create a generated code tool from LLM output
tool = GeneratedCodeTool(
code_executor=executor,
tool_name="calculate_fibonacci",
description="Calculate the nth Fibonacci number",
code="def fib(n):\n a, b = 0, 1\n for _ in range(n):\n a, b = b, a + b\n return a\nresult = fib(payload['n'])",
language="python",
)
# 3. Invoke the tool with a payload
output = tool(payload={"n": 10})
print(output) # {"success": True, "result": 55}
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.