LatentKit

Laravel

Use your LatentKit route from Laravel and the official Laravel AI SDK.

LatentKit for Laravel connects a Laravel application to the route assigned to its LatentKit app key. It provides the framework-neutral PHP client plus a dedicated text provider for the official Laravel AI SDK.

Version 0.1.0-alpha.1 is available as latentkit/laravel on Packagist. It remains an alpha while live application and design-partner validation continue.

Source code and issues are available in the public Laravel package repository.

What the package provides

  • An auto-discovered LatentKit service provider
  • A container-bound PHP SDK client and Laravel facade
  • A safe latentkit:check connection command
  • A latentkit text provider for the official Laravel AI SDK
  • One logical latentkit-route model
  • Network-free testing fakes
  • Request-ID errors without provider response bodies

LatentKit remains the routing control plane. Laravel does not select or forward an upstream provider, model, route, or policy.

Requirements

  • PHP 8.3 or later
  • Laravel 12 or 13
  • Laravel AI SDK 0.9.1 or later
  • A LatentKit app key with a published route

Install

The first alpha depends on the PHP SDK beta. Request both prereleases explicitly from a stable Laravel application:

composer require \
  latentkit/latentkit-php:0.1.0-beta.3 \
  latentkit/laravel:0.1.0-alpha.1

Connect your app

Add the app-scoped key to the Laravel server environment:

LATENTKIT_API_KEY=lk_...

Self-hosted deployments may set LATENTKIT_BASE_URL. Optional LATENTKIT_TIMEOUT and LATENTKIT_RESPONSE_PROFILE values are also read from server configuration. These values flow through Laravel configuration and work with php artisan config:cache.

Never put the key in a controller response, Blade view, browser JavaScript, queued job payload, log context, screenshot, or committed .env file.

Verify the connection

php artisan latentkit:check

The command displays the connected workspace/app, assigned route, route model count, remaining credits, and the latest winning provider/model. It does not print the key, prompts, generated content, or provider response bodies.

Use the PHP SDK

Inject LatentKit\LatentKit into a controller, service, command, or queued job:

use LatentKit\LatentKit;

final class SummarizeReport
{
    public function __construct(private readonly LatentKit $client) {}

    public function __invoke(string $report): string
    {
        $response = $this->client->chat->create([
            ['role' => 'user', 'content' => 'Summarize this report: '.$report],
        ]);

        return $response['content'];
    }
}

Use Laravel AI

The alpha registers latentkit as a Laravel AI text provider. It consumes the required latentkit-route label and calls LatentKit's canonical /v1/chat endpoint. The assigned LatentKit route chooses the live provider and model.

use Laravel\Ai\AnonymousAgent;

$response = (new AnonymousAgent(
    instructions: 'Answer clearly and briefly.',
    messages: [],
    tools: [],
))->prompt(
    'Summarize the deployment status.',
    provider: 'latentkit',
);

echo $response->text;

Set AI_PROVIDER=latentkit to use it as the default text provider. Do not pass an upstream model. Any model other than latentkit-route is rejected before a network request.

The first alpha supports non-streaming text only. Streaming, tool calls, structured output, and attachments return explicit unsupported-feature errors. LatentKit does not use Laravel AI's OpenAI-compatible driver because the canonical route-based API is not a base-URL replacement for OpenAI chat completions.

Queues

Resolve the SDK client inside a job's handle() method. Store only your normal application input in the job; do not serialize the SDK client or API key.

The LatentKit API queue is enqueue-only and has no public job-result endpoint. When a Laravel job needs the generated result, call the synchronous SDK resource from the worker and store the result in your application.

Testing

Application code using the direct SDK binding can use the facade fake:

use LatentKit\Laravel\Facades\LatentKit;

$fake = LatentKit::fake([
    ['content' => 'Test summary'],
]);

$result = LatentKit::client()->chat->create([
    ['role' => 'user', 'content' => 'Summarize this'],
]);

$fake->assertChatSent(
    fn (array $request): bool => $request['messages'][0]['content'] === 'Summarize this',
);

Laravel AI agents can continue using the AI SDK's official agent fakes and prompt assertions. Neither test path contacts LatentKit.

Errors and privacy

Laravel AI failures report the HTTP status and LatentKit request ID when available. Use that ID for support correlation. Do not log API keys, prompts, generated content, or provider response bodies.

Review the privacy policy and terms before connecting a production app.

On this page