Development
Extensions Framework
Agent Zero is built with extensibility in mind, providing a framework for creating custom extensions, tools, API endpoints, and helpers to enhance functionality.
Extensibility Overview
The Python framework controlling Agent Zero is built as simple as possible, relying on independent smaller and modular scripts for individual tools, API endpoints, system extensions and helper scripts. This way individual components can be easily replaced, upgraded or extended.
Extensible Components
- Extensions: Hook into agent lifecycle points to modify behavior
- Tools: Modular components providing specific functionality to agents
- API Endpoints: Expose Agent Zero functionality to external systems
- Helpers: Utility modules providing shared logic across the framework
- Prompts: Instructions and context provided to the LLM
Extensions
Extensions are components that hook into specific points in the agent's lifecycle. They allow you to modify or enhance the behavior of Agent Zero at predefined extension points. The framework uses a plugin-like architecture where extensions are automatically discovered and loaded.
Extension Points
- agent_init: Executed when an agent is initialized
- before_main_llm_call: Executed before the main LLM call is made
- message_loop_start: Executed at the start of the message processing loop
- message_loop_prompts_before: Executed before prompts are processed
- message_loop_prompts_after: Executed after prompts are processed
- message_loop_end: Executed at the end of the message processing loop
- monologue_start: Executed at the start of agent monologue
- monologue_end: Executed at the end of agent monologue
- reasoning_stream: Executed when reasoning stream data is received
- response_stream: Executed when response stream data is received
- system_prompt: Executed when system prompts are processed
Extension Mechanism
The extension mechanism works through the call_extensions function:
- Loads default extensions from
/python/extensions/{'{{'}}extension_point{{'}}'}/ - Loads agent-specific extensions from
/agents/{'{{'}}agent_profile{{'}}'}/extensions/{'{{'}}extension_point{{'}}'}/ - Merges them, with agent-specific extensions overriding default ones based on filename
- Executes each extension in order
Creating Custom Extensions
To create a custom extension:
- Create a Python class that inherits from the
Extensionbase class - Implement the
executemethod - Place the file in the appropriate extension point directory
# File: /agents/_example/extensions/agent_init/_10_example_extension.py
from python.helpers.extension import Extension
class ExampleExtension(Extension):
async def execute(self, **kwargs):
# rename the agent to SuperAgent0
self.agent.agent_name = "SuperAgent" + str(self.agent.number)
💡 Override Logic: When an extension with the same filename exists in both default and agent-specific locations, the agent-specific version takes precedence.
Tools
Tools are modular components that provide specific functionality to agents. They are invoked by the agent through tool calls in the LLM response. Tools are discovered dynamically and can be extended or overridden.
Tool Structure
Each tool is implemented as a Python class that inherits from the base Tool class. Tools are located in:
- Default tools:
/python/tools/ - Agent-specific tools:
/agents/{'{{'}}agent_profile{{'}}'}/tools/
Tool Override Logic
When a tool with the same name is requested, Agent Zero first checks for its existence in the agent-specific tools directory. If found, that version is used. If not found, it falls back to the default tools directory.
# File: /agents/_example/tools/response.py
from python.helpers.tool import Tool, Response
class ResponseTool(Tool):
async def execute(self, **kwargs):
print("Redefined response tool executed")
return Response(
message=self.args["text"] if "text" in self.args else self.args["message"],
break_loop=True
)
Tool Execution Flow
- Tool initialization
before_executionmethodexecutemethod (main functionality)after_executionmethod
API Endpoints
API endpoints expose Agent Zero functionality to external systems or the user interface. They are modular and can be extended or replaced.
Location:
- Default endpoints:
/python/api/
Each endpoint is a separate Python file that handles a specific API request. This modular approach makes it easy to add new endpoints or modify existing ones.
Helpers
Helper modules provide utility functions and shared logic used across the framework. They support the extensibility of other components by providing common functionality.
Location:
- Default helpers:
/python/helpers/
Prompts
Prompts define the instructions and context provided to the LLM. They are highly extensible and can be customized for different agents.
Prompt Locations
- Default prompts:
/prompts/ - Agent-specific prompts:
/agents/{'{{'}}agent_profile{{'}}'}/prompts/
📝 Note: Since v0.9.7, custom prompts should be placed under agents/<agent_profile>/prompts/ instead of a shared prompts subdirectory.
Prompt Features
Variable Placeholders:
Prompts can include variables using the {{'{{'}}var{{'}}'}} syntax:
# Current system date and time of user
- current datetime: {{'{{'}}date_time{{'}}'}}
- rely on this info always up to date
Dynamic Variable Loaders:
Create Python files with the same name as your prompt files to generate variables at runtime:
from python.helpers.files import VariablesPlugin
from python.helpers import files
class MyVariablesPlugin(VariablesPlugin):
async def generate(self):
# Generate dynamic content
tools_list = await self.load_tools()
return {
"tools": tools_list,
"custom_var": "custom value"
}
💡 See the Prompts & Customization guide for detailed information on prompt engineering and customization.
Best Practices
Extension Development
- Keep extensions focused on a single responsibility
- Use appropriate extension points for your use case
- Test extensions in isolation before deploying
- Document extension behavior and requirements
- Use agent-specific extensions for custom agent profiles
Tool Development
- Inherit from the base
Toolclass - Implement clear input validation
- Provide meaningful error messages
- Use
before_executionfor setup,after_executionfor cleanup - Test tools with various input scenarios
Maintenance
- Version control your custom extensions and tools
- Keep extensions updated with framework changes
- Monitor extension performance impact
- Review and refactor regularly
Need Help?
Questions about extending Agent Zero? Reach out to the community!