Build agents with Custom Actions

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:

  1. Installed Sema4.ai Studio — Your local development environment for building agents
  2. Created a basic AI agent — Understanding the fundamentals will help you integrate custom actions
  3. 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.

  1. Open VS Code or Cursor
  2. Navigate to the Extensions marketplace
  3. Search for "Sema4.ai"
  4. Click Install

Once the SDK is installed, you will see the in the activity bar.

Sema4.ai SDK Installed

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:

  1. Create a folder on your computer for the action
  2. Open the folder in VS Code
  3. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P):
  4. Type "Sema4.ai: Create Action Package" and confirm the command
  5. Follow the prompts to create a basic action template Basic template selection
💡

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:

  1. Create a test file in the devdata folder named input_your_function_name.json (e.g., input_calculate_metrics.json)
  2. Add sample input values in JSON format:
{
  "data_point": 42.5,
  "time_period": "weekly"
}
  1. Open Command Pallete and type Sema4.ai: Run Action and enter
  2. Review the output in the Terminal pane to ensure your action works as expected

Publish to Studio

After testing your custom action:

  1. Open Command Pallete and type Sema4.ai: Publish Action Package to Sema4.ai Studio
  2. 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
  3. 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

Basic template selection

Implement in Your Agent

Now you can use your custom action in your agent:

  1. Open Studio and navigate to your agent
  2. Add the custom action from the Action Gallery Basic template selection
  3. Update your agent's runbook to utilize the new action
  4. 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.