Skip to main content
Tool calling enables the model to request specific actions from your application. Instead of generating a text response, the model returns structured function calls with arguments, which your code can execute and optionally feed back into the conversation. This is the foundation for building agents, assistants, and any application that needs to interact with external systems.

Defining Tools

Tools are defined as arrays following the OpenAI function calling format. Each tool describes a function’s name, purpose, and expected parameters:

Making a Tool Call Request

Pass your tool definitions via the tools parameter and control how the model selects tools with toolChoice:

Processing Tool Call Results

The response object provides methods for inspecting whether the model made tool calls and extracting their details:

Convenience Accessors

For simple cases where you just need the tool call arguments as data, Polyglot provides shortcut methods:
When the model returns a single tool call, asToolCallJsonData() returns that call’s arguments as an array. When multiple tool calls are returned, it returns an array of all calls.

Controlling Tool Selection

The toolChoice parameter controls how the model decides whether to use tools:

Multiple Tools

You can provide multiple tool definitions in a single request. The model will select the most appropriate one based on the user’s message:

Using the Fluent API

The fluent builder methods withTools() and withToolChoice() offer an alternative to passing everything through with():

Provider Support

Tool calling support varies across providers: You can query tool support programmatically through DriverCapabilities::supportsToolCalling() and DriverCapabilities::supportsToolChoice().

When to Use Tool Calling

Tool calling is ideal for:
  • Building agents that interact with external APIs and services
  • Creating assistants that retrieve real-time information
  • Implementing multi-step workflows where the model orchestrates actions
  • Extracting structured data using function schemas (an alternative to JSON Schema mode)
  • Giving the model access to specific capabilities like calculations, database queries, or file operations