CodiFly IT Solutions

Laravel 13: First-party AI SDK, PHP 8.3 & what's new in 2026

Apr 09, 2026 13 views 0 comments
Laravel 13: First-party AI SDK, PHP 8.3 & what's new in 2026
Web Development Backend Technologies AI & Machine Learning Generative AI AI in Software Development API Development Product Development
PHP 8.3+ AI-Native Laravel 13 March 2026

Laravel 13: Built for the age of AI

From a first-party AI SDK to typed PHP attributes and smarter caching — Laravel 13 is the most significant release in years. Here's everything you need to know, with real implementation steps.

CF
CodiFly Team
April 2026 · 8 min read
🤖
Feature highlight #01 · Biggest release

First-party AI SDK

Laravel 13 ships with an official, first-party AI SDK — laravel/ai. Instead of managing separate packages for each provider, you get one unified interface for text generation, tool-calling agents, embeddings, audio, image generation, and vector databases. Switch from Claude to GPT-4o by changing a single environment variable.

Text generation Tool-calling agents Embeddings Image & audio generation Vector databases
Provider-agnostic by design. The SDK supports Anthropic, OpenAI, Gemini, Groq, xAI, DeepSeek, Ollama, and more — all under one API. Automatic failover means if OpenAI hits a rate limit, requests can fall through to Anthropic instantly.
Step-by-step: Connecting Anthropic & OpenAI Agents
  1. Install the AI SDK via Composer
    composer require laravel/ai
  2. Publish the config and database migrations This creates config/ai.php and migration tables for agent conversation history.
    php artisan vendor:publish --provider="Laravel\Ai\AiServiceProvider"
    php artisan migrate
  3. Add your API keys to .env
    # .env
    ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxx
    OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx
  4. Configure providers in config/ai.php Set your default provider and enable automatic failover. If Anthropic is the default and OpenAI is the fallback, the SDK handles the switch transparently.
    // config/ai.php
    return [
        'default' => env('AI_PROVIDER', 'anthropic'),
        'providers' => [
            'anthropic' => [
                'driver'   => 'anthropic',
                'key'      => env('ANTHROPIC_API_KEY'),
                'fallback' => 'openai', // auto failover
            ],
            'openai' => [
                'driver' => 'openai',
                'key'    => env('OPENAI_API_KEY'),
            ],
        ],
    ];
  5. Generate an Agent class Agents are the core building block — each agent is a dedicated PHP class with instructions, tools, and output schema.
    php artisan make:agent SupportAssistant
  6. Define your agent — with per-provider options The providerOptions() method lets you pass provider-specific settings. For Anthropic, you can enable extended thinking; for OpenAI, you can set reasoning effort and frequency penalties.
    // app/Ai/Agents/SupportAssistant.php
    namespace App\Ai\Agents;
    
    use Laravel\Ai\Contracts\Agent;
    use Laravel\Ai\Contracts\HasProviderOptions;
    use Laravel\Ai\Enums\Lab;
    use Laravel\Ai\Promptable;
    
    class SupportAssistant implements Agent, HasProviderOptions
    {
        use Promptable;
    
        public function instructions(): string
        {
            return 'You are a helpful customer support agent.
                    Answer clearly and escalate when unsure.';
        }
    
        public function providerOptions(Lab|string $provider): array
        {
            return match($provider) {
                Lab::Anthropic => [
                    'thinking' => ['budget_tokens' => 1024],
                ],
                Lab::OpenAI => [
                    'reasoning'         => ['effort' => 'low'],
                    'frequency_penalty' => 0.5,
                ],
                default => [],
            };
        }
    }
  7. Prompt your agent from a controller or service
    use App\Ai\Agents\SupportAssistant;
    use Laravel\Ai\Enums\Lab;
    
    // Use the default provider (Anthropic)
    $response = (new SupportAssistant)->prompt(
        'Customer says: my order has not arrived after 2 weeks.'
    );
    
    // Explicitly target OpenAI for this request
    $response = (new SupportAssistant)->prompt(
        'Summarise this legal disclaimer for plain-English display.',
        provider: Lab::OpenAI
    );
    
    // Use failover: try Anthropic first, fall back to OpenAI
    $response = (new SupportAssistant)->prompt(
        'Classify this ticket as urgent or routine.',
        provider: [Lab::Anthropic, Lab::OpenAI]
    );
Quick tip: Use Lab::Anthropic and Lab::OpenAI enum values instead of plain strings — they're type-safe and auto-complete in your IDE.

The SDK also includes built-in SimilaritySearch tools for RAG (retrieval-augmented generation), provider tools like WebSearch and WebFetch, and first-class testing support via SupportAssistant::fake().

⚙️
Feature highlight #02

PHP 8.3 is now mandatory

Laravel 13 drops support for PHP 8.1 and 8.2. PHP 8.3 is the minimum, and PHP 8.5 (released November 2025) is also supported — bringing further JIT improvements and native URI handling.

Minimum version
PHP 8.3
Also supported
PHP 8.4, PHP 8.5

Key benefits unlocked by requiring 8.3:

  • JIT improvements — better throughput for CPU-bound workloads, including AI inference pipelines
  • Typed class constants — constants now enforce type safety at the PHP level
  • json_validate() — validate JSON without decoding it, useful when handling large AI responses
// PHP 8.3: typed class constants
class ApiConfig {
    const string DEFAULT_MODEL = 'claude-sonnet-4-6';
    const int    MAX_TOKENS    = 4096;
}

// json_validate() — no need to decode first
if (json_validate($aiResponse)) {
    $data = json_decode($aiResponse, true);
}
Check your server PHP version before upgrading. Run php -v and update your hosting environment or Docker image to PHP 8.3+ before deploying a Laravel 13 app.
🧩
Feature highlight #03

Native PHP Attributes for cleaner code

Laravel 13 leans heavily into PHP Attributes — the #[...] syntax — across models, jobs, commands, and routing. This reduces the need for config-heavy patterns and makes code more declarative and self-documenting.

// Before: config-based job setup
class ProcessAiReport implements ShouldQueue {
    public string  $queue   = 'ai';
    public int     $tries   = 3;
    public int     $timeout = 120;
}

// After: attribute-driven (Laravel 13)
#[Queue('ai'), Tries(3), Timeout(120)]
class ProcessAiReport implements ShouldQueue {
    // clean and concise
}

Attributes can also be used on Eloquent models for casting, relations, and validation, reducing the need for long $casts arrays and boot methods. The code becomes easier to scan at a glance.

Feature highlight #04

Cache::touch() — smart cache refresh

A small but practical addition: Cache::touch() extends a cache entry's TTL without re-fetching or rewriting the value. Ideal for keeping active sessions alive, or preventing expensive AI embeddings from expiring mid-session.

// Extend TTL without touching the value
Cache::touch('user:embeddings:42');

// Extend with a specific duration
Cache::touch('conversation:context', now()->addHours(2));

// Practical use: refresh AI session context on every request
if (Cache::has('ai:session:'.$userId)) {
    Cache::touch('ai:session:'.$userId);
}
Particularly useful for AI apps: keep cached embeddings or conversation context alive while a user is active, without the overhead of a full cache re-write on every request.
🚀
Feature highlight #05

Performance improvements

Laravel 13's performance gains come from two directions: PHP 8.3's improved JIT compiler and internal framework optimisations to request handling and service resolution. For most apps, this is a free throughput boost with no code changes required.

Source
PHP 8.3 JIT improvements
Impact
Better request throughput

For AI-heavy workloads — like running multiple concurrent agent prompts, processing embeddings in queued jobs, or streaming large language model responses — the combination of JIT improvements and smarter service container resolution means more requests handled per second at the same infrastructure cost.

Upgrade to PHP 8.3+ and Laravel 13, run your benchmarks, and enjoy the gains — no refactoring required.
Danny Lalwani
Written by
Danny Lalwani

Tech Entrepreneurial leadership, Technology Whiz in ReactJS , Laravel and NodeJS having 7+ years in web and backend development .

Share
Chat with us