Skip to content
Logic SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

AI tools

Give an AI SDK model a named tool that runs as a caller.

toAITool exposes one authorized function as an AI SDK tool.

Choose the function and scope

Use AI SDK 7 and schemas that convert to JSON Schema, such as Zod 4. Choose a function with authorize. Build the caller from the request’s scope.

Give the model the tool

import { generateText, isStepCount } from "ai";
import { toAITool } from "logic-sdk/ai";

const caller = registry.as(await scopeFrom(request));
const result = await generateText({
  model,
  prompt: "Confirm Ana's booking for July.",
  tools: {
    confirmBooking: toAITool(caller, bookings.confirm, {
      description: "Confirm a pending booking by ID.",
    }),
  },
  stopWhen: isStepCount(5),
});
  • Description. Use meta.ai.description for a default. Include AIMeta in the init meta type. The tool option overrides it.
  • Tool options. Spread the result to add AI SDK options such as toModelOutput. Use AI SDK’s toolApproval when a person must approve a tool call.
  • Schemas. The model sees JSON Schema. The tool checks input before approval and passes the raw value to run, which applies transforms.
  • Invalid input. The AI SDK refuses the tool call. No person sees an approval request. The model gets the issues and can try again.
  • Errors. See tool error results. Unexpected exceptions reach the tool’s onError, which defaults to console.error.

The model can use confirmBooking when policy allows it.

Running as an agent

Use toAITool(ctx, fn) inside an action, listener, or cron. A helper can take CallerLike<Scope>. To change identity, run an action with an agent scope and build tools inside that action. Include a field in the scope if policies or logs need to identify the agent.

See also

Was this page helpful?