Code Tool

Give AI agents the ability to execute custom JavaScript code as a tool

Written By pvdyck

Last updated About 3 hours ago

Code Tool

The Code Tool lets you define a custom JavaScript tool that an AI agent can invoke during its reasoning process. Use it for calculations, data transformations, or logic the LLM cannot do natively.

How It Works

You write a JavaScript function, give it a name and description, and optionally define input parameters. The agent sees this as a callable tool and invokes it when its reasoning requires it.

Parameters

ParameterDescription
NameTool name the agent sees (e.g., "formatCurrency", "validateEmail"). Keep it descriptive.
DescriptionExplains what the tool does β€” the agent uses this to decide when to call it. Be specific.
LanguageJavaScript or Python. Note: in the indie.money Worker environment, only JavaScript is available (code runs in the QuickJS sandbox).
CodeThe JavaScript function body. Receives input via query (string) or structured input parameters. Must return a string.
Input SchemaOptional JSON Schema defining structured input parameters the agent provides when calling the tool.

Code Environment

FeatureAvailable
Synchronous JSYes β€” variables, loops, conditionals, string manipulation, math
query parameterThe agent's input string, available as query in your code
Return valueMust return a string β€” the agent receives this as the tool result
fetch / HTTPNo β€” no network access in the sandbox
console.logNo β€” no console output
Async / PromisesNo β€” synchronous code only
Workflow dataAccessible via $input, $json within the tool code

Connects To

Parent NodeDescription
AgentRegisters as a tool the agent can call during reasoning.

Example

Name: extractDomainDescription: "Extracts the domain from an email address"Code:

const email = query;return email.split('@')[1] || 'invalid';

Limitations

  • Executes in the QuickJS sandbox β€” synchronous JavaScript only.
  • No network access (fetch, XMLHttpRequest unavailable).
  • No console.log β€” use return values for output.
  • Must be connected to an Agent node β€” cannot run standalone.

Tips

  • Write a clear, specific Description β€” this is how the agent decides when to use your tool.
  • Keep code simple and focused on one task (Single Responsibility).
  • Define an Input Schema when your tool needs multiple parameters instead of a single string.
  • For pure math, prefer the Calculator tool. Use Code Tool for string manipulation, formatting, or custom logic.

Related