Laravel has always focused on developer happiness. We build tools that remove friction so you can focus on shipping features. Integrating artificial intelligence follows that same philosophy. You don't need to be a data scientist to build intelligent applications. You just need the right tools in your PHP web framework.
Adding OpenAI to a Laravel project used to require manual API calls and complex Guzzle configurations. Today, the ecosystem provides streamlined paths to get AI running in minutes. Whether you want a simple chat completion or a complex autonomous agent, the infrastructure is ready.
Preparing Your Environment
Modern AI integration requires a solid foundation. Ensure you are running a recent version of Laravel. We recommend PHP 8.2 or higher to take advantage of the latest language features and performance improvements.
First, secure an API key from OpenAI. Once you have your key, add it to your .env file immediately.
OPENAI_API_KEY=sk-your-unique-key-here
Keeping credentials out of your code is the first rule of professional PHP developer tools. Laravel handles environment variables natively, making this step effortless.

The First-Party Way: Laravel AI SDK
We recently introduced the Laravel AI SDK. This is the gold standard for developers who want a unified API for multiple providers. It allows you to switch between OpenAI, Anthropic, and Gemini without rewriting your logic.
Installation
Install the core package via Composer. This tool is the backbone of dependency management in any modern PHP web framework.
composer require laravel/ai
Configuration
Publish the configuration file to customize your defaults. This gives you control over models and fallback providers.
php artisan vendor:publish --tag=ai-config
In your config/ai.php, confirm that OpenAI is your default provider. You can specify the model here, such as gpt-4o or the cost-efficient gpt-4o-mini.
Generating Your First Text
Testing the integration takes seconds. You can use the AI facade to generate a response. Facades provide a static interface to classes in the service container, offering an elegant syntax for complex operations.
use Laravel\AI\Facades\AI;
$result = AI::generate('Explain the beauty of Laravel in one sentence.');
echo $result;
This simple call handles the authentication, the request payload, and the response parsing. It allows you to build AI-powered features without getting bogged down in HTTP headers.
The Focused Way: openai-php/laravel
Some projects only require OpenAI. In those cases, the openai-php/laravel community package is a popular choice. It provides a thin wrapper around the official OpenAI PHP client.
Quick Setup
Install the wrapper through Composer:
composer require openai-php/laravel
This package integrates directly into the Laravel service container. It automatically picks up your OPENAI_API_KEY from the environment.
Building a Chat Feature
If you want to build a support bot or an interactive assistant, use the Chat resource. This is perfect for when you need to build a REST API with PHP that serves AI responses to a frontend built with Vue or React.
use OpenAI\Laravel\Facades\OpenAI;
$response = OpenAI::chat()->create([
'model' => 'gpt-4o',
'messages' => [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => 'How do I deploy a Laravel app?'],
],
]);
return $response->choices[0]->message->content;
This approach gives you granular control over the OpenAI-specific parameters like temperature and max tokens.

Real-World Use Cases: Beyond Chat
Integrating AI isn't just about chatbots. The real power comes from automating repetitive tasks and enhancing user data.
Automated Content Generation
You can use OpenAI to generate SEO descriptions for products or blog posts. When a user saves a new entry, a Laravel listener can trigger an AI job in the background.
Data Summarization
If your application handles large amounts of text: like customer reviews or legal documents: AI can provide instant summaries. Use the SDK to process these blocks of text and store the summary in your database for quick retrieval.
Building REST APIs for AI
When you build a REST API with PHP to power AI features, you often need to handle long-running requests. Laravel's queue system is essential here. Dispatch a job to handle the AI processing and use WebSockets via Laravel Reverb to push the result to the client when it's ready.
Deployment and Monitoring
AI features can be resource-intensive and sensitive to latency. Monitoring your production environment is critical for a smooth user experience.
Scaling with Laravel Forge
Deploying your AI-powered application should be as simple as the development process. Laravel Forge automates the provisioning of servers optimized for PHP applications. It handles your SSL certificates, deployment scripts, and queue workers with a single click.
Monitoring with Nightwatch
To keep an eye on your API costs and response times, use Nightwatch. It provides real-time logs and monitoring, allowing you to see exactly how your OpenAI integration performs in the wild. If the OpenAI API goes down or experiences latency, you'll be the first to know.

Forward Looking
The integration of AI into the Laravel ecosystem is just beginning. Tools like Laravel Boost are pushing the boundaries of what's possible, providing even deeper integration with large language models.
We encourage you to experiment. Start small by automating a single field in your admin panel. As you get comfortable with the SDK, move toward more complex implementations like vector search or autonomous agents.
The community is building incredible things every day. Your story belongs here, too. We’d love to hear what you build with these tools.
Ready to start? Dig into the official documentation and ship something today.