Skip to content

xAI ​

Direct access to xAI's Grok family. Use it when your product standardizes on Grok for chat text with streaming plus image generation.

Capabilities ​

Text with streaming and image generation. Provider tools: code execution, file search, and web search (details).

Configuration ​

Configure via environment variables. config/ai.php only forwards them with env() — set the values in .env or the host environment, never hardcode keys in the config file.

php
// In config/ai.php
return [
    'Ai' => [
        'providers' => [
            'xai' => [
                'className' => 'Crustum\Ai\Providers\XaiProvider',
                'key' => env('XAI_API_KEY'),
                'url' => env('XAI_URL', 'https://api.x.ai/v1'),
                'models' => [
                    'text' => [
                        'default' => env('XAI_TEXT_MODEL', 'grok-4.7'),
                        'cheapest' => env('XAI_TEXT_MODEL_CHEAPEST', 'grok-4.20-non-reasoning'),
                        'smartest' => env('XAI_TEXT_MODEL_SMARTEST', 'grok-4.7'),
                    ],
                    'image' => [
                        'default' => env('XAI_IMAGE_MODEL', 'grok-imagine-image-2.0'),
                    ],
                ],
            ],
        ],
    ],
];
Config keyEnv varPurpose
className—Provider class. Keep the default unless subclassing.
key (apiKey also accepted)XAI_API_KEYKey from console.x.ai. Required.
urlXAI_URLAPI base URL. Change only for proxies or gateways.
models.text.defaultXAI_TEXT_MODELGrok chat model for normal calls.
models.text.cheapestXAI_TEXT_MODEL_CHEAPESTBudget / non-reasoning model.
models.text.smartestXAI_TEXT_MODEL_SMARTESTFlagship Grok model.
models.image.defaultXAI_IMAGE_MODELImage generation model.
headers— (config only)Extra HTTP headers merged into every request. Add as a plain array if needed.

Model selection priority per call: explicit model: argument wins, then the matching models.* value above, then the provider's built-in fallback.

See also ​

Released under the MIT License.