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.

This guide takes you from zero to a working Groq-powered PHP application. By the end you will have installed the package, authenticated with the Groq API, generated text, and seen a streaming response — all using the same fluent Generate interface shared across every provider in the PHP AI SDK.
1

Install the package

Require aisdk/groq via Composer. It automatically pulls in aisdk/core and aisdk/openai-compatible.
Terminal
composer require aisdk/groq
2

Set your API key

The provider reads your key from the GROQ_API_KEY environment variable by default. Get a free key from console.groq.com under API Keys.
.env
GROQ_API_KEY=gsk_...
You can also pass the key programmatically when bootstrapping the provider — useful in multi-tenant apps or when reading credentials from a secrets manager:
bootstrap.php
use AiSdk\Groq;

Groq::create([
    'apiKey'  => $yourSecretManager->get('GROQ_API_KEY'),
    'baseUrl' => 'https://api.groq.com/openai/v1', // optional override
]);
3

Generate text

Use Generate::text() to send a prompt and read the response. The ->model() call accepts any Groq::model() handle.
generate.php
use AiSdk\Generate;
use AiSdk\Groq;

$result = Generate::text()
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
// "A closure is an anonymous function that can capture variables from its surrounding scope..."
The $result object exposes both the generated text and token-usage information:
usage.php
echo $result->usage->inputTokens;   // e.g. 18
echo $result->usage->outputTokens;  // e.g. 74
4

Set a default model

If your application always uses the same model, register it once with Generate::model(). Every subsequent Generate::text() call skips the ->model() step.
bootstrap.php
use AiSdk\Generate;
use AiSdk\Groq;

// Call this once during application bootstrap (e.g. a service provider).
Generate::model(Groq::model('llama-3.3-70b-versatile'));
anywhere-in-your-app.php
use AiSdk\Generate;

// No ->model() needed — uses the default registered above.
$result = Generate::text('What is the capital of France?')->run();

echo $result->text; // "Paris."
5

Stream a response

For long-form output or real-time UIs, use ->stream() instead of ->run(). Iterate over $stream->chunks() to receive text as it arrives, then call ->run() to get the completed TextModelResponse.
stream.php
use AiSdk\Generate;
use AiSdk\Groq;

$stream = Generate::text('Tell me a short story about a robot learning to cook.')
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->stream();

foreach ($stream->chunks() as $chunk) {
    echo $chunk;        // Print each token as it arrives
    flush();
}

// Finalise to access usage stats and provider metadata.
$result = $stream->run();

echo "\n\nTokens used: " . $result->usage->outputTokens;

Provider Metadata

Every completed response includes a providerMetadata map keyed by provider name. The groq key holds response-level details returned by the Groq API:
metadata.php
use AiSdk\Generate;
use AiSdk\Groq;

$result = Generate::text('Hello!')
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->run();

$meta = $result->providerMetadata['groq'];

echo $meta['id'];                  // "chatcmpl_groq_abc123"
echo $meta['model'];               // "llama-3.3-70b-versatile"
echo $meta['choice_finish_reason']; // "stop"
choice_finish_reason can be "stop" (natural end), "tool_calls" (model invoked a tool), "length" (context limit reached), or "content_filter". Check this field if you suspect truncation.

Next Steps

Configuration

Customise the base URL, inject headers, and register models that aren’t in the catalogue yet.

Text Generation Guide

Deep-dive into system instructions, tool calling, and structured output with schemas.

Models Overview

See every supported Groq model, its context window, and its capability flags.

API Reference

Full reference for the Groq facade, GroqProvider, GroqOptions, and response types.

Build docs developers (and LLMs) love