Building Laravel-Native AI Applications with Prism

Overview: The Unified AI Interface

Integrating Artificial Intelligence into a modern application often feels like chasing a moving target. One week

is the leader; the next,
Anthropic
releases a model that changes the game. Without a proper abstraction layer, developers find themselves trapped in a maintenance nightmare, juggling multiple APIs and varying response formats.
Prism
solves this by providing a unified,
Laravel
-native routing layer for AI models. It allows you to switch providers by changing a single line of code while maintaining the fluid, expressive syntax that PHP developers expect from tools like
Eloquent
.

Prerequisites

To follow this guide, you should be comfortable with the following:

  • PHP 8.2+: Leveraging modern type-hinting and features.
  • Laravel Framework: Familiarity with Service Providers and Blade.
  • AI Fundamentals: Basic understanding of prompts, models, and API keys.

Key Libraries & Tools

  • Prism: The core package providing the unified API for AI providers.
  • Blade: Used as a sophisticated templating layer for dynamic prompt engineering.
  • Laravel Zero: Mentioned for building CLI-based AI agents.
  • MCP (Model Context Protocol): Supported via the Relay package for tool integration.

Implementation: Text and Structured Output

Standard text generation is the baseline. Prism uses a fluent interface to define the provider and the prompt. A standout feature is the use of Blade templates for prompts, which avoids the mess of concatenated strings.

$response = Prism::text()
    ->using("anthropic", "claude-3-5-sonnet")
    ->withPrompt(view('prompts.blog-post', ['topic' => $topic]))
    ->generate();

echo $response->text;

While text is useful, Structured Output is where the real power lies. Instead of using messy regex to parse markdown, you define a schema. This ensures the AI returns a predictable associative array, perfect for updating database columns directly.

$response = Prism::structured()
    ->using("openai", "gpt-4o")
    ->withSchema(Schema::object([
        'summary' => Schema::string(),
        'links' => Schema::array(Schema::string()),
    ]))
    ->generate();

Building Intelligent Agents with Tools

emphasizes that AI is most effective when it can act. Prism supports Tool Use (function calling) where the model can decide to invoke PHP methods. By defining a class-based tool, you give the model the ability to edit files, search databases, or interact with external APIs via the
Model Context Protocol
.

Syntax Notes and Best Practices

Prism adopts the Invocable Action pattern, keeping controllers lean. It also supports Multimodal inputs, allowing you to pass images or PDFs from any Laravel-defined disk (S3, local, etc.) simply by attaching them to the request chain. This integration with the Laravel storage abstraction makes document analysis trivial.

Practical Examples

  • Legal Tech: Automating contract analysis and passing summaries to human lawyers.
  • Content Creation: Enhancing sparse image descriptions into rich narratives using persona-based prompts (like the "Vivian" persona).
  • DevOps Agents: Using
    Laravel Zero
    to build CLI tools that can fix bugs or refactor code automatically.

Tips & Gotchas

  • Human in the Loop: Always verify AI output for critical tasks like legal or financial decisions.
  • Testing: Use Prism::fake() to mock responses. This prevents hitting expensive APIs during your CI/CD pipeline and allows you to assert that specific prompts were sent.
3 min read