Tool calling allows an OpenAI model to request the execution of a PHP function you define. Instead of generating freeform text, the model analyzes the user’s prompt, decides which registered tool best satisfies it, and returns a structured JSON payload containing the function name and its arguments. The PHP AI SDK parses that payload into aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openai/llms.txt
Use this file to discover all available pages before exploring further.
ToolCallPart so your application can act on it — running the function, collecting the result, and optionally continuing the conversation. This pattern is the foundation for agentic workflows, data retrieval, API orchestration, and anything else that requires bridging language models with real-world side effects.
Defining a Tool
UseTool::make() to create a named, described tool with a typed input schema. Chain ->input() to declare the parameters and ->run() to provide the PHP callable that executes when the tool is invoked:
weather tool is relevant, emit a tool call for city = "Lahore", and the SDK will invoke the closure and return the result.
How It Works
Serialize tools to OpenAI format
ChatToolConverter::convert() iterates over the registered tools and maps each one to the OpenAI function-calling schema:Set tool_choice
tool_choice defaults to "auto", meaning the model decides whether to call a tool. You can force or disable tool use — see Tool Choice below.Tool Choice
By default, OpenAI decides autonomously whether to invoke a tool (tool_choice: "auto"). You can override this behaviour using ToolChoice:
ToolChoice::tool('weather') serializes to the OpenAI format {type: "function", function: {name: "weather"}}, forcing the model to call that specific function regardless of the prompt.
ToolChoice value | OpenAI wire value | Behaviour |
|---|---|---|
null (default) | "auto" | Model decides whether to call a tool. |
ToolChoice::tool('name') | {type: "function", function: {name: "name"}} | Forces a specific tool call. |
Reading Tool Call Results
After->run(), inspect $result->parts for ToolCallPart instances. Each part exposes the call’s id, name, and decoded arguments:
arguments array is decoded from the JSON string the model returns, so all values are native PHP types ready for use without manual json_decode calls.
Multi-Tool Requests
Pass an array of tools via->tools() to let the model choose from multiple functions in a single request:
tools is non-empty, ChatRequestBuilder always includes both the tools array and a tool_choice value in the request body. The model may call zero, one, or multiple tools depending on the prompt and tool_choice setting.
Tool calling is supported on
gpt-4o, gpt-4.1, o-series, and gpt-5 models. Check capability with $model->supports(Capability::ToolCalling).