Tools

Tools are reusable functions that agents can call to perform specific actions like processing payments, sending emails, or updating databases.

Tool Structure

Every tool must implement the Tool interface:

// Example: Tool with input and optional output schemas
interface Tool<
  Input extends z.ZodSchema,
  Memory = any,
  Output extends z.ZodSchema = z.ZodTypeAny
> {
  name: string; // Unique tool identifier
  description: string; // What the tool does (used by LLM)
  input: Input; // Zod schema for input validation
  output?: Output; // Optional: Zod schema for output validation (e.g., outputSchema)
  isGlobal?: boolean; // Optional: available across all LLM calls
  execute: ({ input, state, agent }) => Promise<{ result? }>;
}

Execute Function Signature

The execute function is the core of every tool. It receives validated input from the LLM, current state (including memory), and the agent instance, then modifies state by reference and returns an optional result.

Parameters

  • input: Validated data matching your Zod schema - contains the parameters the LLM extracted from the conversation

  • state: Current conversation state including memory, sessionId, and other context data

  • agent: Agent instance providing access to PII gateway, logging, and other platform features

Return Value

The execute function returns a Promise with an object containing:

  • result? (optional): Result that gets sent back to the LLM as the tool's response

Important v2.0 Change: State is now updated by reference directly within the execute function. You no longer return state updates - instead, modify the state object directly.

State Object Structure

The state object contains:

  • memory: Your user-defined memory schema - the main working memory for your agent

  • sessionId: Unique identifier for the current session

  • messages: Array of conversation messages (AI, Human, System, etc.)

  • history: Array of HistoryStep objects tracking the flow execution (node visits, trigger events, tool calls)

  • Other platform context: Additional fields managed by the platform

Basic Tool Example

Here's a simple refund processing tool:

Input Schema Configuration

Input schemas use Zod for validation and provide important metadata to help the LLM understand how to better use each input parameter.

Parameter Descriptions

Add descriptions to parameters using Zod's .describe() method. These descriptions help the LLM understand what each parameter represents:

Optional Parameters

Make parameters optional using Zod's .optional() method. Optional parameters don't need to be provided by the LLM:

Combining Descriptions and Optional Parameters

You can chain Zod methods to create descriptive optional parameters:

Output Schema Configuration

Tools can optionally define an output schema to validate and structure their return values. This provides type safety and clear documentation of what the tool returns.

Defining Output Schema

Important: The output schema variable must be named outputSchema for the platform to parse it correctly.

Benefits of Output Schema

  • Type Safety: TypeScript will enforce that your return value matches the output schema

  • Validation: Zod validates the output at runtime to catch bugs early

  • Documentation: Output schema serves as clear documentation of what the tool returns

  • LLM Context: The LLM receives structured information about the tool's output format

Output Schema vs Result

  • Without output schema: Return any value in result, no validation

  • With output schema: Return value must match the schema structure

Tool Registration

Global Tools

Mark tools as global if you want them to be available in all LLM calls.

Global vs Non-Global Tools

Feature
Non-Global Tools
Global Tool

Execution

Guaranteed when node is reached

Only when LLM decides to call

Control

Explicit flow control

LLM-driven decision

Use Case

Required business logic steps

Flexible, context-dependent actions

State Management

Tools can read and update state including memory by reference:

Error Handling

Handle errors gracefully in tools:

Flow Control with goto

Tools can control the flow by setting the goto property directly on the state object. This allows tools to programmatically jump to a specific node by setting state.goto to the target node ID. This is useful for dynamic flow control based on runtime conditions.

Basic goto Usage

Dynamic Routing Example

Important: The jump occurs only when the current invocation completes and the next one begins. The agent will finish executing the current node/tool before jumping to the specified node.

Best Practices

  1. Clear Descriptions: Write descriptions that help the LLM understand when to use the tool

  2. Input Validation: Use Zod schemas to validate all inputs

  3. State Updates: Update state directly by reference - modify only the parts that need to change

  4. Error Handling: Always handle errors gracefully and provide meaningful messages

  5. Async Operations: Use async/await for external API calls and database operations

  6. Logging: Use the provided logger for consistent, structured logging with session context

Tool Nodes

Tool nodes force execution of specific tools at defined points in your flow, bypassing LLM decision-making.

Configuration

Overriding Tool Input Parameters

Tool input parameters' values are inferred by an LLM during runtime. You can override specific input parameters directly in your flow YAML to ensure deterministic values. Parameters support placeholder syntax to inject dynamic values from memory, environment variables, tool outputs, and system values. See Context for comprehensive details.

Available Placeholders

  • Memory: {state.memory.propertyName} or {memory.propertyName}

  • Tool outputs: {tools.NodeName.field}

  • Environment variables: {env.VARIABLE_NAME}

  • System values: {system.key}

Example

Note: Complex objects and arrays are passed to tools as native objects, not as JSON strings.

See Also

Last updated