Code Tools
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
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
| Use Case | Why Code Works |
|---|---|
| SQL query processor | Dynamic query construction based on user input |
| Custom validators | Complex validation rules with multiple conditions |
| Data transformers | Parse and reshape data from various formats |
| Algorithm execution | Mathematical calculations, sorting, filtering |
Supported Languages
| Language | Runtime | Best For |
|---|---|---|
| JavaScript | Node.js | Web APIs, JSON processing, async operations |
| Python | Python 3.x | Data processing, ML integration, scripting |
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 Interface
Step 3: Write Code
JavaScript Example:Step 4: Test
- Click Test
- Provide sample input
- Review output and execution logs
- Debug any issues
Step 5: Deploy
Click Deploy to make the tool available to agents.Code Structure
Entry Point
Your code must define anexecute function:
JavaScript:
Input Parameter
Contains the parameters defined in your tool spec:Context Object
Provides access to environment and runtime info:Return Value
Return a JSON-serializable object:Environment Variables
Access secrets and configuration: JavaScript:- Navigate to tool settings → Environment
- Add key-value pairs
- Mark sensitive values as secrets
HTTP Requests
JavaScript (fetch)
Python (requests)
Error Handling
Always handle errors gracefully: JavaScript:Examples
Data Validator
Price Calculator
Execution Limits
| Limit | Value |
|---|---|
| Timeout | 30 seconds (configurable) |
| Memory | 256 MB |
| Output size | 1 MB |
Debugging
Console Logging
JavaScript:Execution Logs
View logs in the tool’s monitoring section:- 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