Add and Reuse Functions in AI Agent Studio
JavaScript function templates enable you to define common logic once and use it across multiple agentic workflows.
Create function templates from the Functions subtab under the Resources tab in AI Agent Studio. Each template can include one or more JavaScript functions and generated test cases to validate function behavior before publishing. After a function template is published, it becomes available from the Functions subtab for use in Code nodes across agentic workflows.
Create a Function Template
- Go to AI Agent Studio and open the
Resources
tab. - Select the Functions subtab.
- Select Add.
- Enter a name and description for the new function template. The code is automatically populated based on the name.
- Select Add to create a function.
- Enter a name and description for the function. The code is automatically populated based on the name.
- In the Code subtab, write the JavaScript source code manually or generate
the function code.
- To generate the function code, select Optimize, describe the logic that you want the function to perform, and then press the Enter key. The function code is generated and added to the Code subtab. It might take some time to generate the code. After the code is added, you can view the function parameters in the Input subtab and the return values in the Output subtab.
- From the Code subtab, select Run. If an error is displayed, review and modify the generated code.
- From the Test Cases subtab, generate or upload test cases to validate the function. Test cases verify expected results using sample inputs and include expected output, actual output, and pass or fail status before the function is used in workflows.
- Select Run Test Cases to verify that the function behaves as expected.
- Save and close the function template or publish it.
Published function templates are available from the Functions subtab for use in Code nodes within workflows.
Use a Reusable Function in a Workflow
- From the Workflows tab, edit a workflow and add a Code node.
- Enter a name and description for the code node. The code is automatically populated based on the name.
- Select an error handler node to define failure handling.
- From Source, select Reusable Functions.
- From the next two lists, select the published function template and then the function you want to use.
- (Optional) Review and update the output specification.
- Publish the workflow.
When the workflow runs, the Code node calls the selected reusable function from the published function template and returns the function output to the next node in the workflow.