Skip to main content

Introduction

Deck by PromptPHP provides first-class, optional integration with the Laravel AI SDK. When the AI SDK is installed, you get:
  • Automatic prompt scaffolding — Running make:agent automatically creates a matching prompt directory.
  • The HasPromptTemplate trait — Provides instructions() and promptMessages() methods that load versioned prompts directly into your AI agents.
  • The TrackPromptMiddleware — Automatically records prompt executions (tokens, latency, model, etc.) using Deck’s tracking system.
All of this is entirely optional. Deck works perfectly without the AI SDK.

Installation

Deck does not require the AI SDK — it’s listed as a suggest dependency. Install it when you’re ready:
Once laravel/ai is installed, Deck’s AI SDK features activate automatically. No additional configuration is needed.

Automatic prompt scaffolding

When the Laravel AI SDK is installed, Deck automatically hooks into the make:agent command. Whenever you create a new agent:
Deck detects the successful command and automatically runs:
This creates a versioned prompt directory ready for the agent to use via the HasPromptTemplate trait — zero extra setup required.

How it works

Deck registers a listener (AfterMakeAgent) on Laravel’s CommandFinished event. When make:agent completes successfully, the listener:
  1. Extracts the agent name from the command input.
  2. Converts it to kebab-case (SalesCoachsales-coach).
  3. Strips any namespace prefix (App\Ai\Agents\SalesCoachsales-coach).
  4. Checks if the prompt already exists (skips if it does).
  5. Runs make:prompt with the derived name.
The listener is only registered when laravel/ai is installed. If the prompt creation fails for any reason, it does not break the make:agent workflow — the agent is still created successfully.

Example output

Disabling auto-scaffolding

To disable automatic prompt scaffolding, set the configuration option:
Or via environment variable:

Quick start

Use the HasPromptTemplate trait on any agent class:
That’s it. The HasPromptTemplate trait provides the instructions() method required by the Agent contract, loading the system prompt from your Deck files.

The HasPromptTemplate trait

The HasPromptTemplate trait bridges Deck’s file-based templates with the Laravel AI SDK’s agent contracts.

How it maps to AI SDK contracts

Mapping diagram

Customising the prompt

Prompt name

By default, the prompt name is derived from the class name in kebab-case:
  • SalesCoachsales-coach
  • DocumentAnalyzerdocument-analyzer
Override promptName() to use a custom name:

Pinning a version

By default, the active version is loaded. Pin to a specific version by overriding promptVersion():
Return null (the default) to always load the active version — useful for A/B testing and gradual rollouts.

Variable interpolation

Pass dynamic values into your prompt templates by overriding promptVariables():
In your system.md:
Variables are interpolated into all roles (system, user, assistant, etc.) when accessed via instructions() or promptMessages().

Full agent example

Here’s a complete agent using all Deck features with the AI SDK:
Create and populate the prompt files:
Edit prompts/sales-coach/v1/system.md:

Conversation context

If your agent implements Conversational, you can load pre-defined conversation context from Deck role files using the promptMessages() method.

Loading all non-system roles

By default, promptMessages() returns all roles except system (which goes through instructions()):

Limiting to specific roles

Pass an array to limit which roles are included:

Merging with database history

Combine template messages with conversation history from your database:

Performance tracking middleware

The TrackPromptMiddleware automatically records prompt executions via Deck’s tracking system.

Setting up the middleware

1

Enable tracking

Set the tracking configuration in config/deck.php:
2

Publish and run migrations

3

Add middleware to your agent

What gets tracked

The middleware automatically records the following fields to the prompt_executions table:

How it works internally

The middleware:
  1. Records the start time before the request using hrtime(true).
  2. Passes the prompt to the next middleware in the pipeline.
  3. Uses the response’s then() hook to record execution data after the response completes.
  4. Calls PromptManager::track() with the collected data.
The middleware only tracks agents that use the HasPromptTemplate trait. If the agent doesn’t have a promptName() method, the tracking is silently skipped.

Accessing the template directly

You can access the full PromptTemplate object from within your agent for advanced use cases:
The template instance is cached for the lifetime of the agent object, so repeated calls to promptTemplate() don’t incur additional filesystem or cache lookups.

Clearing the cached template

Clear the cached template to force a fresh load on next access:
This is useful in:
  • Long-running processes (queue workers, daemons) where prompts might change between jobs.
  • Tests where you switch prompt versions between assertions.
The method returns $this for fluent chaining:

Without the AI SDK

The HasPromptTemplate trait works even without laravel/ai installed. The instructions() method simply returns a string, and promptMessages() falls back to returning raw arrays instead of AI SDK Message objects:
This allows you to use Deck’s template loading in any context, not just with the AI SDK.