Laravel Daily's

How to Integrate OpenAI with the Laravel AI SDK in 5 Minutes

hero image

AI used to feel like a separate layer. You had to juggle custom API clients, handle raw JSON responses, and manage conversation state manually. Not anymore.

The Laravel AI SDK brings OpenAI directly into your application's workflow. It treats AI as a first-class citizen. You get the same elegant syntax you expect from the rest of the framework.

Here is how you bridge your Laravel app and OpenAI in five minutes.

1. Install the SDK: The Foundation

First, you need the official package. Laravel released the AI SDK to provide a unified API for various providers. It makes switching from OpenAI to Anthropic or Gemini a matter of configuration, not a code rewrite.

Open your terminal and run:

composer require laravel/ai

Once installed, publish the configuration and migrations. This step creates the config/ai.php file and the database tables needed to store AI conversations.

php artisan vendor:publish --provider="Laravel\Ai\AiServiceProvider"
php artisan migrate

2. Configure Your Keys: Setting the Bridge

Grab your API key from the OpenAI Dashboard. Open your .env file and add your credentials.

OPENAI_API_KEY=sk-your-openai-key-here
AI_PROVIDER=openai

Now, look at config/ai.php. The SDK uses a provider-based system. Ensure the openai driver is set up to point to your environment variables.

'providers' => [
    'openai' => [
        'driver' => 'openai',
        'key'   => env('OPENAI_API_KEY'),
    ],
],

This configuration makes OpenAI your default engine. You are now ready to ship.

The Laravel AI SDK dashboard interface showing integrated AI features like automated content suggestions

3. The Quick Start: Using the Agent Helper

Sometimes you don't need a complex setup. You just want a quick answer from a model. The agent() helper is perfect for this. It’s the "fire-and-forget" method for simple prompts.

use Laravel\Ai\Contracts\Lab;

$response = agent()->prompt(
    'Suggest five catchy titles for a blog about PHP web frameworks.',
    provider: Lab::OpenAI,
    model: 'openai/gpt-4o-mini',
);

echo (string) $response;

This snippet sends a request to OpenAI and returns a string-castable response. It’s clean, readable, and fits perfectly inside a controller or a background job.

4. Build Real Features: Creating Agent Classes

For production features like support bots or content generators, you need more structure. Laravel AI SDK encourages the use of Agent classes. These classes encapsulate the logic, tools, and model preferences for a specific task.

Generate your first agent:

php artisan make:agent ContentAssistant

Inside your new agent class, you can define which provider and model to use using PHP attributes. This keeps your business logic tidy.

namespace App\Ai\Agents;

use Laravel\Ai\Agents\Agent;

#[Provider(['openai'])]
#[Model('openai/gpt-4o-mini')]
class ContentAssistant extends Agent
{
    // Define custom tools or system prompts here
}

Using this agent in your application is a breeze:

use App\Ai\Agents\ContentAssistant;

$reply = ContentAssistant::make()
    ->prompt("Summarize the latest Laravel update.");

return response()->json(['data' => (string) $reply]);

A developer using Laravel to build an AI-integrated feedback tool with clean code syntax

5. Deployment and Scaling: Laravel Cloud

Building the feature is only half the battle. You need to deploy it. Because the AI SDK is part of the Laravel ecosystem, it works seamlessly with Laravel Cloud.

When you deploy, your environment variables are managed securely. Your background jobs for long-running AI tasks are handled by Laravel Horizon. The entire stack is optimized for rapid shipping of features.

The integrated Laravel ecosystem architecture showing Laravel Cloud and the AI tool suite

Beyond the Basics: Laravel Boost

If you want to take your productivity further, look into Laravel Boost. It’s a suite of tools designed to enhance the developer experience. Boost integrates with the AI SDK to provide intelligent code completions and automated testing scenarios.

By combining the AI SDK with Boost, you aren't just adding features. You are transforming how you write code. You close gaps in your workflow and spend more time on the creative aspects of development.

The Future is Integrated

Integrating OpenAI into Laravel used to take days of custom work. With the Laravel AI SDK, it takes five minutes.

The framework handles the complexity. You focus on the user experience. Whether you are building a REST API with PHP or a complex SaaS platform, AI is now just another tool in your artisan belt.

We'd love to hear what you're building. Every corner of the community is finding new ways to use these tools. Your story belongs here.

Laravel's API integration overview highlighting connectivity with third-party services

Previous
Does PHP Really Matter in 2026? Here’s Why Laravel is Still Winning
Next
Laravel 13 Sneak Peek: What Every Developer Needs to Know for 2026