Skip to main content
Build tools with custom JavaScript or Python code.

Overview

Code tools execute custom scripts to perform tasks that require:
  • Complex business logic
  • Dynamic data processing
  • Custom calculations
  • Conditional operations beyond visual flows
  • Integration with systems requiring custom protocols
Supported languages: JavaScript (Node.js) and Python (Python 3.x)

When to Use

Code tools are ideal when:
  • Logic is dynamic or conditional
  • You need fine-grained control over data handling
  • Visual workflows become too complex
  • External services require custom integration
  • You need computational operations

Good Fit Examples


Creating a Code Tool

Step 1: Create the Tool

  1. Navigate to Tools+ New Tool.
  2. Select Code Tool.
  3. Choose language (JavaScript or Python).

Step 2: Define Metadata

Step 3: Define Input Parameters

Define the parameters the tool needs to perform its task. For each parameter, specify: Supported types:

Defining an Object Parameter

Use an object parameter when the tool requires structured input that groups multiple related fields. Object parameters enable structured, nested inputs and allow the agent to validate and construct accurate tool requests before execution. Adhere to the Sample Schema for detailed structure and formatting guidelines. When defining an object-type parameter:
  • Use the properties field to list all fields inside the object (mandatory for object types)
  • For each field in properties, specify:
    • type: string, number, boolean, enum, or object
    • description (optional): A brief explanation of the field’s purpose
    • required (optional): Whether the field is mandatory
  • Use the required array to list mandatory fields of the object
  • Nested objects must define their own properties and required fields. Set type as object.
Example: An employee object with id and email (mandatory), and a nested location object containing city, state, and country (where city and state are mandatory):

Step 4: Define Output Parameters

Output parameters define how the tool’s response is processed and made available to the application. Choose one of the following modes: Use Complete Tool Output — Returns the entire tool response without modification. This is the default and is recommended when the tool already returns data in the required format.
  • Include Tool Response in Artifacts — When enabled, the tool’s response is included in the artifacts field of the Execute API response payload. This only affects the API response and does not change tool execution behavior, playground simulations, or other agent and tool functionalities.
Define Custom Parameters — Extracts specific values from the tool’s response and maps them as structured outputs. Use this when you need control over which parts of the response are passed to the agent or stored as artifacts. When selected, configure one or more output parameters: Click + Add to define multiple output parameters if the response contains several values needed by the agent. Example: To extract a status message from the response, set Name to Status, Type to string, and Path to tool.output.status.message. For each output parameter, configure Output Routing to define where the extracted value is sent:

Step 5: Write Code

This is the core logic of the tool. This is the custom code that gets executed when the tool is invoked. This can be written in either JavaScript or Python. Using Input Parameters in the script In JavaScript, parameters are injected directly as $ParameterName variables. Assign them to local variables at the top of your scrip - myinput = $value; //input variable = value. In Python, parameters are passed as it’s containerId = value # input variable = value Returning a Response Use return to pass the result back to the agent. Error Handling If your script throws an unhandled exception, the error message is passed to the agent for further investigation. This means the agent may attempt to recover or respond to the user based on the error, so write clear, descriptive error messages rather than generic ones. Use try/catch in JavaScript or try/except in Python to control exactly what the agent receives when something goes wrong. Working With Memory To read from or write to the memory store inside your script, refer to Memory Stores. Working with Context Object Refer to this to learn context object structure. Context object can be accessed as context.<variables>. Logging in scripts
  • Use console.log() method for log messages in javascript.
  • For Python, use: print(“This is a log message”)
  • Design time logs during tool testing can be viewed in the Execution Logs section during testing of the tool.
  • Execution logs from the tool can be viewed in the Traces.
Script execution has a time limit of 180 seconds.
Examples: JavaScript Example:
Python Example:

Step 5: Test

  1. Click Run Sample Execution
  2. Provide sample input values for each parameter
  3. Review the output and execution logs
  4. Debug any issues
Execution logs include timestamps, log levels, messages, and execution context—useful for troubleshooting.

Step 6: Deploy

This tool is available with the app deployment. Once the app is deployed, the agent automatically invokes the tool when a matching intent is detected.

Code Structure

Access environment variables

Access Input Parameter

Contains the parameters defined in your tool spec:

Access Context variables

Access Environment Variables

Access secrets and configuration from environment variables: JavaScript:

Return Value

Return a JSON-serializable object:

Error Handling

Always handle errors gracefully: JavaScript:
Python:

Examples

Data Validator

Price Calculator

Debugging

Console Logging

JavaScript:
Python:

Execution Logs

View logs in the traces.
  • Timestamp
  • Log level
  • Message content
  • Execution context

Best Practices

Keep Functions Focused

One tool = one capability. Don’t combine unrelated logic.

Validate Input Early

Use Type Checking

Handle Edge Cases

  • Empty inputs
  • Null values
  • Invalid formats
  • API failures

Document Your Code

Add comments for complex logic and business rules.