Skip to main content
Tool calling (also called function calling) lets a model invoke functions you define when it needs external data or actions to fulfill a request. You define the tool, the model decides when to use it, and Genkit handles the back-and-forth automatically.

Defining a tool

inputSchema and outputSchema are Zod schemas. The descriptions are passed to the model so it knows when and how to call the tool.

Passing tools to generate()

Pass tools to generate() using the tools option. The model will call tools as needed and Genkit will automatically run the tool implementations, send results back to the model, and continue until the model produces a final text response.

Automatic multi-turn loop

Genkit handles the full tool call/response loop for you:
  1. You call ai.generate() with a prompt and tools.
  2. The model returns a tool call request instead of a final answer.
  3. Genkit invokes the tool implementation with the model’s arguments.
  4. The tool’s output is sent back to the model as a tool response.
  5. The model uses the tool output to produce its final response.
  6. Steps 2–5 repeat until the model returns a final text response.
This loop is transparent—generate() returns only after the model has produced its final response. You don’t need to write any polling or loop logic.
The default maximum number of tool-call iterations is 5. You can change this via the maxTurns option in TypeScript (config.maxTurns) or the ToolConfig.MaxTurns field in Go.

Tool choice

Control whether the model is required to use a tool with the toolChoice option:

Real-world example: web search agent

Here is a more complete example combining multiple tools in a flow:

Using tools with prompts

Tools can also be declared in Dotprompt files:

Dynamic tools

For tools that should not be registered in the global registry (for example, tools created per-request), use ai.dynamicTool() (TypeScript):

Next steps

Agents

Build multi-step autonomous agents using tools.

Prompts

Declare tools inside .prompt files.

Models

Learn about all generate() options.

Flows

Wrap tool-using logic in observable, deployable flows.