Skip to main content

Overview

The defineTool() function defines tools (functions) that language models can call during generation. Tools enable models to access external data, perform calculations, or trigger actions beyond text generation.

Function signature

Parameters

string
required
The unique name of the tool. This is what the model uses to identify and call the tool. Should be descriptive and follow snake_case convention (e.g., get_weather, search_database).
string
required
A clear description of what the tool does. This helps the model understand when and how to use it. Should explain the tool’s purpose, expected inputs, and what it returns.
SchemaInput
required
Schema of the tool’s input parameters: a Standard Schema with JSON Schema support, such as a Zod 4.2+ schema, or plain JSON Schema. The model generates arguments matching this schema. Check calls to tools with Standard Schema parameters with validateToolCall().
boolean
Opts this tool into provider-enforced adherence to its parameter schema. Omitted or false keeps the tool non-strict on every provider.

Return value

Returns the same ToolDefinition object that was passed in. This is a type-safe helper that provides IDE autocompletion and validation.

Strict schema enforcement

strict: true opts a single tool into provider-enforced schema adherence. The tool’s schema must fit the strict-capable subset (closed objects whose keys are required, nullable, or defaulted — in Zod, use .nullable() instead of a bare .optional()), which core-ai validates locally before the provider request. Check model.capabilities.tools.strictSchemas before opting tools in. Strict enforcement does not replace runtime validation. Check each call with validateToolCall() before executing the tool. See Strict tool schemas in the tool-calling guide for the schema contract, error reasons, and provider support.

validateToolCall()

Validates the arguments of a tool call with the tool’s own Standard Schema.
For Standard Schema parameters, it returns the schema’s result, typed with the schema’s output:
  • { value } when the arguments match. value is the schema’s output, with defaults and transforms applied.
  • { issues } when they do not. Each issue has a message and an optional path. A validator that throws, such as a transform calling new URL(), is reported as an issue too.
For plain JSON Schema parameters, such as tool schemas from an MCP server, it returns undefined: core-ai ships no JSON Schema validator, so validate those arguments yourself. It throws a ValidationError when toolCall is a call to another tool, or when the tool’s parameters are not a usable schema, which a request with the tool rejects too. Send issues back to the model as an error tool result, and run the tool with value, not with the raw ToolCall.arguments. See Validate tool calls for complete examples.

Examples

Basic tool definition

Tool with multiple parameters

Using tools with generate()

Tool execution loop

Complex parameters with nested objects

Tool with enum parameters

Multiple Tools in One Call

Controlling Tool Usage

Type Safety

The tool definition is fully type-safe:

Best Practices

Use descriptive tool names that clearly indicate their purpose. Follow the snake_case convention.
Write clear descriptions that explain what the tool does, when to use it, and what it returns. Good descriptions improve model accuracy.
Add descriptions to parameters, such as with Zod’s .describe() method, to provide additional context to the model.
Always validate tool results before passing them back to the model. Handle errors gracefully.

Common Patterns

1. Database Queries

2. API Calls

3. File Operations