Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/groq/llms.txt

Use this file to discover all available pages before exploring further.

Tool calling lets a language model invoke PHP functions during a generation. You define a tool with a name, a description, and an input schema; the SDK sends your tools to the Groq API, detects when the model requests a call, executes your handler, and feeds the result back — all inside a single ->run() call. The final TextModelResponse contains the model’s natural-language answer after the loop completes.

How Tool Calling Works

1
Create a tool
2
Tool::make() takes a name and a short description. Chain ->input() to describe the arguments the model should supply, and ->run() to provide the PHP callable that handles the invocation.
3
use AiSdk\Schema;
use AiSdk\Tool;

$weather = Tool::make('weather', 'Get the current weather for a city')
    ->input(Schema::string(name: 'city')->required())
    ->run(fn (string $city): string => "Sunny in {$city}");
4
The ->run() closure receives named arguments that match the schema properties you declared, so parameter names must align with your schema field names.
5
Register the tool and run generation
6
Pass the tool to the generate builder with ->tool($weather). The SDK includes it in the API request and automatically handles any tool-call rounds before returning the final response.
7
use AiSdk\Generate;
use AiSdk\Groq;

$result = Generate::text()
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->prompt('What is the weather in Lahore?')
    ->tool($weather)
    ->run();
8
Read the response
9
The result is an ordinary TextModelResponse. Access the model’s final answer through $result->text.
10
echo $result->text;
// "The weather in Lahore is currently sunny."

Complete Weather Tool Example

Weather tool — complete example
use AiSdk\Generate;
use AiSdk\Groq;
use AiSdk\Schema;
use AiSdk\Tool;

// 1. Define the tool
$weather = Tool::make('weather', 'Get current weather for a city')
    ->input(Schema::string(name: 'city')->required())
    ->run(fn (string $city): string => "Sunny in {$city}");

// 2. Run the generation
$result = Generate::text()
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->prompt('What is the weather in Lahore?')
    ->tool($weather)
    ->run();

// 3. Print the answer
echo $result->text;

Using Multiple Tools

Register as many tools as you need by chaining additional ->tool() calls. The model will pick whichever tool (or sequence of tools) best answers the prompt.
Multiple tools
use AiSdk\Generate;
use AiSdk\Groq;
use AiSdk\Schema;
use AiSdk\Tool;

$weather = Tool::make('weather', 'Get current weather for a city')
    ->input(Schema::string(name: 'city')->required())
    ->run(fn (string $city): string => "Sunny in {$city}");

$population = Tool::make('population', 'Get the population of a city')
    ->input(Schema::string(name: 'city')->required())
    ->run(fn (string $city): string => "Population of {$city}: 14 million");

$result = Generate::text()
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->prompt('Compare the weather and population of Lahore and Karachi.')
    ->tool($weather)
    ->tool($population)
    ->run();

echo $result->text;
Keep tool descriptions concise and action-oriented. The model uses the description — not the schema — to decide which tool to call, so a clear one-liner dramatically improves call accuracy.

Supported Models

All Groq models in the catalog support tool calling natively. The following table lists the available models and their context window sizes:
Model IDContext Tokens
llama-3.1-8b-instant131 072
llama-3.3-70b-versatile131 072
meta-llama/llama-4-scout*
meta-llama/llama-4-maverick*
moonshotai/kimi*
openai/gpt-oss-20b131 072
openai/gpt-oss-120b131 072
You can verify tool calling support programmatically:
Check tool calling capability
use AiSdk\Capability;
use AiSdk\Groq;

$supports = Groq::model('llama-3.3-70b-versatile')
    ->supports(Capability::ToolCalling);

echo $supports ? 'supported' : 'not supported';

Real-World Tool Pattern

For production use, replace the inline closure with a service call or a database lookup:
Production tool with a service call
use AiSdk\Generate;
use AiSdk\Groq;
use AiSdk\Schema;
use AiSdk\Tool;

$stockPrice = Tool::make('stock_price', 'Get the latest stock price for a ticker symbol')
    ->input(Schema::string(name: 'ticker')->required())
    ->run(function (string $ticker): string {
        // Replace with your real data-source call
        $price = fetchStockPrice($ticker);

        return "{$ticker} is trading at \${$price}";
    });

$result = Generate::text()
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->prompt('What is the current price of AAPL?')
    ->tool($stockPrice)
    ->run();

echo $result->text;
The SDK executes your ->run() closure synchronously in the same PHP process. If your tool performs I/O (HTTP requests, database queries), that latency is added to the total generation time. Consider caching aggressively for frequently called tools.
Tool closures run with the same privileges as your application. Avoid passing user-controlled strings directly into shell commands, SQL queries, or file-system operations inside a tool handler.

Build docs developers (and LLMs) love