aiTool

Create callable function tools that AI agents can use to gather information or take actions.

Syntax

aiTool(name, description, callable)

Parameters

Parameter
Type
Required
Description

name

string

Yes

Unique function name (used by AI to identify the tool)

description

string

No

Describes what the tool does (helps AI decide when to use it)

callable

function

No

The function the AI will execute

Returns

Returns a Tool instance that can be passed to AI agents or models.

Examples

Basic Tool

// Simple weather tool
weatherTool = aiTool(
    "get_weather",
    "Get current weather for a location",
    ( location ) => {
        return "72°F and sunny in #location#";
    }
);

// Use with agent
agent = aiAgent(
    name: "WeatherBot",
    tools: [ weatherTool ]
);

response = agent.run( "What's the weather in Paris?" );
// Agent automatically calls get_weather("Paris")

Tool with Multiple Parameters

Database Lookup Tool

API Integration Tool

Multiple Tools

Tool with Rich Output

Tool with Validation

Tool Function Requirements

Return Value

  • Must return a value that can be cast to a string

  • Return JSON strings for complex data: return data.toJSON()

  • Return clear error messages on failure

Parameters

  • Parameters automatically inferred from function signature

  • Parameter descriptions can be added via @param tags in function comments

  • AI will pass arguments based on context

Error Handling

Use Cases

✅ External Data Retrieval

Fetch real-time data from APIs, databases, or files.

✅ Calculations

Perform complex computations AI can't do directly.

✅ System Actions

Execute system commands, file operations, etc.

✅ API Integrations

Connect AI to external services and APIs.

✅ Database Operations

Query or update database records.

❌ Heavy Processing

Don't use for long-running operations - AI requests time out.

❌ Stateful Operations

Keep tools stateless - AI may call multiple times.

Tool Selection

AI automatically selects tools based on:

  1. Tool name: Clear, descriptive names help AI choose correctly

  2. Description: Detailed descriptions improve tool selection

  3. Context: User query and conversation context

  4. Parameters: Parameter names and types guide AI usage

Notes

  • Automatic invocation: AI decides when to call tools based on user input

  • Multiple calls: AI may call same tool multiple times in one response

  • Parameter inference: AI determines arguments from context

  • Return format: Always return string or JSON-serializable data

  • Tool descriptions: Critical for helping AI choose correct tool

  • No async: Tool functions must be synchronous

Best Practices

Last updated