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
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
- Navigate to Tools → + New Tool.
- Select Code Tool.
- 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
propertiesfield to list all fields inside the object (mandatory for object types) - For each field in
properties, specify:type: string, number, boolean, enum, or objectdescription(optional): A brief explanation of the field’s purposerequired(optional): Whether the field is mandatory
- Use the
requiredarray to list mandatory fields of the object - Nested objects must define their own
propertiesandrequiredfields. Set type as object.
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
artifactsfield 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.
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.
Step 5: Test
- Click Run Sample Execution
- Provide sample input values for each parameter
- Review the output and execution logs
- Debug any issues
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:Examples
Data Validator
Price Calculator
Debugging
Console Logging
JavaScript: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