TheDocumentation 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.
aisdk/groq package is the official Groq provider for the PHP AI SDK. It connects to Groq’s OpenAI-compatible API so you can generate text with models like llama-3.3-70b-versatile using the same fluent interface as every other provider in the SDK.
Prerequisites
Install the package and export your Groq API key before running any examples.Basic Text Generation
Pass a prompt directly toGenerate::text() and chain ->model() with a Groq::model() call. Calling ->run() blocks until the full response is returned.
basic.php
Adding a System Prompt
Chain->instructions() to set a system-level prompt that shapes the model’s behaviour for the entire conversation, then use ->prompt() for the user turn.
with-instructions.php
The Response Object
->run() returns a TextModelResponse. The three most useful members are text, usage, and providerMetadata.
response-inspection.php
| Field | Type | Description |
|---|---|---|
$result->text | string | The model’s reply |
$result->usage->inputTokens | int | Prompt tokens consumed |
$result->usage->outputTokens | int | Completion tokens produced |
$result->providerMetadata['groq']['id'] | string | Unique completion ID returned by the Groq API |
$result->providerMetadata['groq']['model'] | string | Model ID echoed back by the API |
$result->providerMetadata['groq']['choice_finish_reason'] | string | Why the model stopped — stop, length, etc. |
Setting a Default Model
If your application always uses the same model, register it once withGenerate::model(). Every subsequent Generate::text() call will use it automatically so you do not need to repeat ->model() at each call site.
bootstrap.php
anywhere-in-your-app.php
You can still override the default on a per-call basis by chaining
->model() explicitly. The explicit call takes precedence over the registered default.Provider-Specific Options
The->providerOptions() escape hatch lets you forward raw request fields directly to the Groq API. This is useful for parameters that are not yet surfaced as first-class SDK options, such as top_k.
provider-options.php
'raw' are merged into the JSON body before the HTTP request is sent.
Programmatic API Key Configuration
If you prefer not to rely on environment variables — for example in a multi-tenant application — pass the key directly toGroq::create().
programmatic-config.php
Error Handling
The SDK maps HTTP status codes to typed exceptions. A429 Too Many Requests response from Groq throws \AiSdk\Exceptions\RateLimitException, which you can catch and handle independently from other errors.
rate-limit-handling.php
Available Models
The models below ship with theaisdk/groq package and are ready to use with Groq::model().
| Model ID | Streaming | Tool Calling | Structured Output | Image Input |
|---|---|---|---|---|
llama-3.1-8b-instant | ✅ Native | ✅ Native | ⚡ Adapted | ❌ |
llama-3.3-70b-versatile | ✅ Native | ✅ Native | ❌ | ❌ |
meta-llama/llama-4-scout* | ✅ Native | ✅ Native | ❌ | ✅ Native |
meta-llama/llama-4-maverick* | ✅ Native | ✅ Native | ❌ | ✅ Native |
moonshotai/kimi* | ✅ Native | ✅ Native | ✅ Native | ❌ |
openai/gpt-oss-20b | ✅ Native | ✅ Native | ✅ Native | ❌ |
openai/gpt-oss-120b | ✅ Native | ✅ Native | ✅ Native | ❌ |
Adapted structured output means the SDK automatically downgrades
json_schema to json_object and injects a JSON instruction into the system prompt. The output is still parsed and returned as a structured value — you do not need to handle the downgrade yourself.