Building AI Agents with Custom Actions
Ready to supercharge your AI agents with custom capabilities? Sema4.ai Actions lets you connect your agents to virtually any enterprise system—from SharePoint to SAP, APIs to browsers—unlocking powerful automation possibilities tailored to your specific business needs.
Prerequisites
Before building custom actions, you should have:
- Installed Sema4.ai Studio — Your local development environment for building agents
- Created a basic AI agent — Understanding the fundamentals will help you integrate custom actions
- Python knowledge — Basic familiarity with Python syntax and concepts
Get up to speed with Build your first AI Agent tutorial which provides step-by-step guidance on installing Studio and creating your first agent, giving you the foundation you'll need before adding custom actions.
Install VS Code Extension
The Sema4.ai SDK provides a comprehensive framework for developing custom actions that optimize performance and ensure agents retrieve data accurately and securely, all while leveraging the full power of the Python ecosystem.
- Open VS Code or Cursor
- Navigate to the Extensions marketplace
- Search for "Sema4.ai"
- Click Install
Once the SDK is installed, you will see the in the activity bar.
The extension provides specialized tools for developing custom actions, including syntax highlighting, code snippets, and validation features.
Create a Custom Action
You can create a new custom action using a template:
- Create a folder on your computer for the action
- Open the folder in VS Code
- Open the Command Palette (
Ctrl+Shift+P
orCmd+Shift+P
): - Type "Sema4.ai: Create Action Package" and confirm the command
- Follow the prompts to create a basic action template
Sema4.ai actions are Python code packages with natural-language descriptions that help AI agents understand the purpose, inputs, and outputs of each action. Agents use their own function-calling capabilities to determine when to call an action and with what inputs.
Each action package consists of four main elements contained within a specific folder structure:
- Package configuration file (
package.yaml
) - Action Python code (
actions.py
) - Test data inputs (
.json
files) - Package icon (
package.png
)
Custom actions use the @action decorator to define functions that your agent can call:
@action
def calculate_metrics(data_point: float, time_period: str) -> str:
"""Calculate business metrics based on input parameters.
Args:
data_point (float): The primary value to analyze
time_period (str): The time period for analysis (daily, weekly, monthly)
Returns:
str: Calculated metrics including trend, variance, and forecast
"""
# Your custom logic here
result = f"Analysis for {data_point} over {time_period} period shows positive trend"
return result
Test Your Custom Action
Once you've created your action, test it with sample inputs:
- Create a test file in the devdata folder named input_your_function_name.json (e.g., input_calculate_metrics.json)
- Add sample input values in JSON format:
{
"data_point": 42.5,
"time_period": "weekly"
}
- Open Command Pallete and type
Sema4.ai: Run Action
and enter - Review the output in the Terminal pane to ensure your action works as expected
Publish to Studio
After testing your custom action:
- Open Command Pallete and type
Sema4.ai: Publish Action Package to Sema4.ai Studio
- Now, Sema4.ai SDK packages your action along with all dependencies to make it available for Agent's use. Once packaged, it will be pushed to Sema4.ai Studio on your machine
- Your action package will be available in the Sema4.ai Studio Action Gallery
Instead of using Command Pallete, you can also use the Sema4.ai Extension in the activity bar to Test, Run and Publish actions. Find all the commands under AGENT/ACTION/TASK PACKAGES
Implement in Your Agent
Now you can use your custom action in your agent:
- Open Studio and navigate to your agent
- Add the custom action from the Action Gallery
- Update your agent's runbook to utilize the new action
- Test your agent with the new capability
Security & Enterprise Integration
Sema4.ai Actions includes enterprise-grade features for secure integration:
- Secrets Management: Securely handle sensitive credentials and API keys
- OAuth Integration: Authenticate and perform operations on behalf of users
- Dependency Management: The integrated Python environment handles isolation and versioning
- Comprehensive Logging: Track execution and troubleshoot issues with detailed logs
What's Next?
Learn how to create Named Queries for optimized data retrieval
By creating custom actions, you're unlocking the full potential of your AI agents, enabling them to interact with your unique business systems and processes, making AI truly work for your enterprise.