Magento 2 - tips and tricks every Developer should know
Magento 2 has a reputation for being complicated, and honestly that reputation is earned. The learning curve is steep, the documentation has gaps, and some of its architectural decisions only make sense once you have been burned by the alternative. I have been working with Magento 2 since its early releases and the tips in this post come directly from real projects, real mistakes, and things I wish someone had told me before I spent hours figuring them out the hard way.
This is not a list of things you will find in the official documentation. These are the practical things that make daily Magento 2 development faster, less frustrating, and more maintainable.
Development Workflow Tips
1. Always work in developer mode during development
This sounds obvious but I have seen teams develop in default mode and wonder why their template changes are not showing up. In developer mode, Magento disables most caching, enables full error reporting, and processes frontend assets on the fly. Switch modes from the command line:
php bin/magento deploy:mode:set developer
The flip side is that production mode compiles and minifies everything, which is why you must run the full deployment commands before pushing to production. Never deploy to production in developer mode. It is slower and exposes internal errors to visitors.
2. Know your cache types and clear only what you need
Running php bin/magento cache:flush clears everything and takes time. In most cases during development you only need to clear specific cache types. Clearing just the config cache after a config change, or just the layout cache after a layout XML change, is significantly faster.
# Clear only config cache
php bin/magento cache:clean config
# Clear layout and block cache after layout changes
php bin/magento cache:clean layout block_html
# Clear full page cache after template changes
php bin/magento cache:clean full_page
# See all cache types and their status
php bin/magento cache:status
Learn which cache type corresponds to which kind of change. It will save you minutes every time you test something.
3. Use n98-magerun2 for everything the CLI does not cover
n98-magerun2 is the Swiss Army knife for Magento 2 development. It extends Magento's CLI with hundreds of useful commands for database management, admin user creation, module management, and much more.
# Install globally
composer global require n98/magerun2
# Create an admin user without going through the UI
n98-magerun2 admin:user:create --username=devadmin --email=dev@example.com \
--password=Dev@12345 --firstname=Dev --lastname=Admin
# Run a database query directly
n98-magerun2 db:query "SELECT * FROM admin_user"
# Open a MySQL console connected to Magento's database
n98-magerun2 db:console
# Show all configuration values for a path
n98-magerun2 config:store:get catalog/search/*
The db:console command alone is worth installing it for. No more hunting for database credentials in env.php every time you need to run a query.
4. Setup Xdebug properly
Too many Magento developers rely on var_dump and log files for debugging. Xdebug with step-through debugging changes how you work entirely. You can inspect the full call stack, watch variable values change in real time, and understand exactly what Magento is doing at any point in its request cycle.
The most common Xdebug configuration issue with Magento is the request timeout. Set a generous timeout in your php.ini because Magento's bootstrap takes longer than a typical PHP application and Xdebug adds overhead:
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=host.docker.internal ; if using Docker
xdebug.client_port=9003
xdebug.max_execution_time=0 ; disable timeout during debug sessions
5. Use the Magento 2 console to run cron manually
Waiting for Magento's cron to run on its schedule during development is painful. You can trigger specific cron groups manually:
# Run all cron jobs
php bin/magento cron:run
# Run a specific cron group
php bin/magento cron:run --group=index
# Check cron schedule
php bin/magento cron:schedule
If something is not updating after you expect it to, a manual cron run is often the answer.
Performance Tips
6. Enable Redis for Cache and Session Storage
File-based cache storage is fine for development but it will not scale in production. Redis handles Magento's cache and session storage significantly better, especially under concurrent load. Configure it in app/etc/env.php:
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '0',
'password' => '',
],
],
'page_cache' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1', // separate database for full page cache
],
],
],
],
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'database' => '2',
],
],
Use a separate Redis database number for each type. Mixing the page cache and session data in the same database means a cache flush will wipe active sessions, which logs out all your customers. Learned that one the hard way on a live site.
7. Flat Catalog tables speed up large catalogues
By default Magento stores product and category attributes using the EAV (Entity-Attribute-Value) model. For catalogues with thousands of products and many attributes, this means dozens of JOIN operations per product list query. Flat tables consolidate all attributes into a single table per entity type, which is dramatically faster for reads.
# Enable flat catalog in admin: Stores > Configuration > Catalog > Catalog
# Then rebuild the flat tables
php bin/magento indexer:reindex catalog_category_flat
php bin/magento indexer:reindex catalog_product_flat
The tradeoff is that the flat tables need to be kept in sync with the EAV tables, which adds a small overhead to product saves. For read-heavy stores with large catalogues this is almost always worth it.
8. Optimise your indexers and set them to update on schedule
Magento has multiple indexers that rebuild data structures when products, categories, or prices change. Running them on "Update on Save" means every product edit triggers a full or partial reindex, which blocks the save operation and slows down the admin. On stores with large catalogues this can make product editing painfully slow.
# Set all indexers to update on schedule
php bin/magento indexer:set-mode schedule
# Or set a specific indexer
php bin/magento indexer:set-mode schedule catalogrule_rule
# Check current indexer status
php bin/magento indexer:status
With schedule mode, reindexing happens on cron runs rather than during save operations. Editors get fast saves, the index stays reasonably fresh, and you avoid timeout issues when making bulk changes.
9. Enable Varnish for full page cache in production
Magento's built-in full page cache is decent but Varnish is significantly faster for high-traffic stores. Magento generates a Varnish configuration file for you:
php bin/magento varnish:vcl:generate --export-version=6 > /etc/varnish/default.vcl
Set the caching application to Varnish in the admin under Stores, Configuration, Advanced, System, Full Page Cache. One thing that catches developers out: after enabling Varnish you need to set the HTTP port to 80 and let Varnish handle requests on that port, with Magento behind it on a different port. Your web server config needs to reflect this or you will get redirect loops.
10. Use Asynchronous and deferred indexing for imports
If you run regular product imports via CSV or API, synchronous indexing after each import will slow things to a crawl. Enable asynchronous reindexing and let the cron handle it:
# Enable async grid indexing
php bin/magento config:set dev/grid/async_indexing 1
For bulk imports using the Import API, make sure you call php bin/magento indexer:reindex after the import completes rather than relying on the save events to trigger reindexing. Batch imports at the end, not during.
Backend and Admin Tips
11. Customise Admin Grids Without Rewriting the Whole Grid
One of the most common admin customisations is adding a column to an existing grid, like the orders grid or the customers grid. The wrong way is to override the entire grid PHP class. The right way is to use a UI Component XML file to extend just what you need.
Create a layout XML file in your module at view/adminhtml/layout/sales_order_grid.xml:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="sales_order_grid">
<arguments>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="update_url" xsi:type="url" path="mui/index/render"/>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>
Then extend the UI component definition in view/adminhtml/ui_component/sales_order_grid.xml to add your column. This approach survives Magento upgrades far better than class overrides because you are extending, not replacing.
12. Use Virtual types to avoid unnecessary classes
Virtual types are one of Magento's most underused features. They let you create a variation of an existing class with different constructor arguments, without writing a new PHP file. This is useful when you need multiple instances of the same class configured differently.
<!-- di.xml -->
<virtualType name="Vendor\Module\Model\CustomLogger"
type="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="name" xsi:type="string">customModule</argument>
<argument name="handlers" xsi:type="array">
<item name="system" xsi:type="object">
Vendor\Module\Logger\Handler\Custom
</item>
</argument>
</arguments>
</virtualType>
<!-- Use the virtual type as a dependency -->
<type name="Vendor\Module\Model\SomeService">
<arguments>
<argument name="logger" xsi:type="object">
Vendor\Module\Model\CustomLogger
</argument>
</arguments>
</type>
This creates a logger instance with custom configuration without writing a new Logger class. Clean, upgrade-safe, and takes five minutes once you understand the pattern.
13. Use Plugins (Interceptors) Instead of Rewrites
Magento 1 developers often reach for class rewrites out of habit. In Magento 2, plugins are almost always the better choice. Plugins let you execute code before, after, or around a public method of any class without replacing the entire class.
<!-- di.xml -->
<type name="Magento\Catalog\Model\Product">
<plugin name="vendor_module_product_plugin"
type="Vendor\Module\Plugin\ProductPlugin"
sortOrder="10"
disabled="false"/>
</type>
<?php
namespace Vendor\Module\Plugin;
use Magento\Catalog\Model\Product;
class ProductPlugin
{
// Runs after getName() and can modify the return value
public function afterGetName(Product $subject, string $result): string
{
if ($subject->getTypeId() === 'bundle') {
return $result . ' (Bundle)';
}
return $result;
}
// Runs before setPrice() and can modify the arguments
public function beforeSetPrice(Product $subject, float $price): array
{
// Ensure price is never negative
return [max(0, $price)];
}
}
Multiple plugins can target the same method and the sortOrder attribute controls execution order. This means your customisation coexists with other modules' plugins rather than one rewrite blocking another entirely.
14. Log to custom files, not the default system log
Everything in Magento logs to var/log/system.log by default. On an active store this file grows fast and becomes almost useless for finding specific module issues. Create a custom logger for your module that writes to its own file:
<?php
namespace Vendor\Module\Logger;
use Monolog\Logger;
class Handler extends \Magento\Framework\Logger\Handler\Base
{
protected $loggerType = Logger::DEBUG;
protected $fileName = '/var/log/vendor_module.log';
}
Register this handler in your module's di.xml using a virtual type (see tip 12 above) and inject it wherever you need logging. Your module's debug output goes to its own file and does not get buried in the system log noise.
Frontend and Theming Tips
15. Extend Parent Themes Rather Than Modifying Them Directly
Never modify Luma or Blank theme files directly. Magento updates will overwrite your changes and you will have no way of knowing what was changed. Always create a child theme that declares the parent:
<!-- app/design/frontend/Vendor/mytheme/theme.xml -->
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
<title>Vendor My Theme</title>
<parent>Magento/luma</parent>
</theme>
To override a template, copy it from the parent theme into the same relative path in your theme and edit the copy. Magento's theme fallback system will use your version. To override a layout, create a layout XML file with the same name in your theme's layout directory and add only the changes you need.
16. Use the less variable override system properly
Magento 2 uses Less for stylesheets and has a variable system that lets you customise colours, fonts, and spacing without touching the source Less files. Override variables in your theme by creating web/css/source/_variables.less:
// Override the primary colour across the entire theme
@color-orange: #e84040;
// Change the default font
@font-family__base: 'Inter', sans-serif;
@font-family__serif: Georgia, serif;
// Button styling
@button__background: @color-orange;
@button__border-radius: 4px;
// Navigation
@navigation__background: #1a1a2e;
@navigation-level0-item__color: #ffffff;
This propagates through Magento's entire Less stack without you touching a single source file. Most visual customisations can be done entirely through variable overrides. Only write custom Less for things the variable system does not cover.
17. Understand static content deployment
The most common frontend issue I see in production deployments is forgetting to deploy static content, or deploying it for the wrong locales.
# Deploy for all themes and locales
php bin/magento setup:static-content:deploy
# Deploy for specific locales only (faster)
php bin/magento setup:static-content:deploy en_US en_GB
# Deploy a specific theme
php bin/magento setup:static-content:deploy -t Vendor/mytheme en_US
# Use parallel deployment for speed on large stores
php bin/magento setup:static-content:deploy --jobs=4 en_US
If your static content looks correct in developer mode but breaks in production, you almost certainly have a deployment issue. Check that all your locales are included in the deploy command and that the deployment ran successfully after the last code change.
18. Use RequireJS config to load JavaScript properly
Adding JavaScript in Magento 2 is not as simple as dropping a script tag in a template. The right way is through RequireJS, which manages dependencies and load order. Define your JavaScript modules in view/frontend/requirejs-config.js:
var config = {
map: {
'*': {
'customSlider': 'Vendor_Module/js/custom-slider',
}
},
shim: {
'customSlider': {
deps: ['jquery'],
}
},
paths: {
'slick': 'Vendor_Module/js/vendor/slick.min',
}
};
Then use it in a template or another JavaScript file:
require(['customSlider', 'jquery'], function(CustomSlider, $) {
var slider = new CustomSlider({
element: $('.product-gallery'),
autoplay: true,
});
});
The RequireJS approach means your JavaScript is only loaded when needed, dependencies are guaranteed to be available, and your code does not conflict with other modules' JavaScript.
Common Mistakes to avoid
19. Do Not use ObjectManager directly in your code
You will see ObjectManager used in old tutorials and even in some core Magento code. Do not follow that pattern in your own modules. Using ObjectManager directly bypasses dependency injection, makes code untestable, and creates hidden dependencies.
<?php
// Wrong. Do not do this.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
// Right. Inject dependencies through the constructor.
class MyService
{
public function __construct(
private \Magento\Catalog\Model\ProductFactory $productFactory
) {}
public function doSomething(): void
{
$product = $this->productFactory->create();
}
}
The only acceptable place to use ObjectManager directly is in factory classes and proxy classes, and even there Magento generates those automatically through its code generation system.
20. Do Not modify core files
Every Magento update will overwrite your changes. Use plugins, preferences, layout overrides, and template overrides instead. If you find yourself editing a file in vendor/magento/, stop and find the proper extension point instead. There is always one.
21. Run Code egneration after adding new classes
Magento generates proxy classes, factory classes, and interceptors automatically. When you add a new class that uses dependency injection, you may need to regenerate this code:
# Generate interceptors and factories
php bin/magento setup:di:compile
# Clear generated code if something seems wrong
rm -rf generated/code/*
php bin/magento setup:di:compile
If you are getting "Class does not exist" errors for factory or proxy classes, this is almost always the fix. In developer mode Magento generates these on the fly, but in production mode they must be compiled ahead of time.
22. Check your module's sequence in module.xml
If your module depends on another module being loaded first, declare that dependency in etc/module.xml. Without it, your di.xml or layout XML might be processed before the module it depends on, causing hard-to-debug errors.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_Sales"/>
</sequence>
</module>
</config>
One last thing worth mentioning
Magento 2 rewards developers who take the time to understand its architecture rather than fighting against it. The dependency injection system, the plugin architecture, the UI component framework, and the layout XML system all have learning curves but they exist for good reasons. Once you work with them properly instead of around them, your code is more maintainable, survives upgrades better, and plays well with other modules.
The most expensive Magento projects I have seen are the ones where developers skipped the architecture and went straight to hacking. The technical debt accumulates fast and the upgrade path becomes a rewrite. The extra time spent doing things the Magento way upfront is always worth it.
If there is a specific Magento 2 topic you want covered in more depth, drop a comment below.
Laravel and Prism PHP: The Modern Way to Work with AI Models
Every Laravel project that needs AI ends up with a different implementation. One project uses the OpenAI PHP client directly. Another one uses a wrapper someone wrote three years ago that is no longer maintained. A third one is tightly coupled to a specific model, so switching from GPT-4o to Claude requires rewriting half the service layer.
Prism PHP solves this properly. It is a Laravel package that gives you a single, consistent API for working with multiple AI providers. OpenAI, Anthropic Claude, Ollama for local models, Mistral, Gemini, and more, all through the same fluent interface. You switch providers by changing one line. Your application code does not care which model is behind it.
This post covers the full picture. All the supported providers and when to use each one, text generation with structured output, tool calling so your AI can actually interact with your application, and embeddings for semantic search. I will tie all three together with a real-world example at the end so you can see how they work as a system rather than isolated features.
What you need:
- Laravel 10 or 11
- PHP 8.1+
- Composer
- API keys for whichever providers you plan to use.
- Ollama requires a local install but is free to run.
Why Prism instead of the OpenAI Client directly
The openai-php/laravel client is solid and I have used it in several projects on this blog. But it locks you into OpenAI. If you want to try Claude for a specific task, or use a local Ollama model for development to avoid API costs, you are writing separate integration code for each one.
Prism is inspired by the Vercel AI SDK, which solved this same problem in the JavaScript world. The idea is simple: define a unified interface, write drivers for each provider, and let application code stay completely provider-agnostic. The practical benefits are real.
You can use GPT-4o for general generation, Claude for tasks where it performs better (long document analysis, nuanced writing), and a local Ollama model during development so you are not burning API credits on every test run. All through the same application code. That flexibility is genuinely useful once you start building production AI features.
Supported Providers
Prism currently ships with first-party support for these providers. Each has its own strengths and the right choice depends on the task.
| Provider | Best For | Key Models | Cost |
|---|---|---|---|
| OpenAI | General generation, embeddings, function calling | GPT-4o, GPT-4o-mini, text-embedding-3-small | Pay per token |
| Anthropic | Long documents, reasoning, nuanced analysis | Claude 3.7 Sonnet, Claude 3.5 Haiku | Pay per token |
| Ollama | Local development, privacy-sensitive data, zero API cost | Llama 3, Mistral, Phi-3, any Ollama model | Free (runs locally) |
| Mistral | Efficient generation, European data residency | Mistral Large, Mistral Small | Pay per token |
| Google Gemini | Multimodal tasks, audio and video input | Gemini 1.5 Flash, Gemini 1.5 Pro | Pay per token |
| xAI (Grok) | Real-time data awareness, alternative to GPT-4 | Grok-2 | Pay per token |
My general approach: OpenAI for embeddings and general tasks, Claude for anything involving long content or nuanced judgment, Ollama for local development. That combination covers most application needs while keeping costs reasonable.
Installation and Configuration
composer require prism-php/prism
Publish the config file:
php artisan vendor:publish --tag=prism-config
This generates config/prism.php. Add your provider credentials to .env:
# OpenAI
OPENAI_API_KEY=sk-your-openai-key
# Anthropic
ANTHROPIC_API_KEY=sk-ant-your-anthropic-key
# Mistral
MISTRAL_API_KEY=your-mistral-key
# Google Gemini
GEMINI_API_KEY=your-gemini-key
# xAI
XAI_API_KEY=your-xai-key
# Ollama runs locally, no API key needed
# Default URL is http://localhost:11434
The config/prism.php file maps these to the relevant providers. You can also set default models per provider here, which saves repeating the model name in every call.
Part 1: Text Generation and Structured Output
The core feature and the one you will use most. Prism's text generation API is chainable and reads naturally, which is one of the things that makes it feel like a proper Laravel package rather than a thin wrapper.
Basic Text Generation
Here is the same prompt sent to three different providers, with zero application code changes between them:
<?php
use Prism\Prism\Facades\Prism;
use Prism\Prism\Enums\Provider;
// OpenAI
$response = Prism::text()
->using(Provider::OpenAI, 'gpt-4o')
->withSystemPrompt('You are a helpful PHP development assistant.')
->withPrompt('Explain what a service container is in Laravel.')
->asText();
echo $response->text;
// Swap to Claude, same code
$response = Prism::text()
->using(Provider::Anthropic, 'claude-3-7-sonnet-latest')
->withSystemPrompt('You are a helpful PHP development assistant.')
->withPrompt('Explain what a service container is in Laravel.')
->asText();
echo $response->text;
// Or run it locally with Ollama during development
$response = Prism::text()
->using(Provider::Ollama, 'llama3')
->withSystemPrompt('You are a helpful PHP development assistant.')
->withPrompt('Explain what a service container is in Laravel.')
->asText();
echo $response->text;
That is the core value proposition right there. Same interface, different provider. If OpenAI has an outage or you want to A/B test Claude versus GPT-4o on a specific prompt, you change one line.
Structured Output with Schema Validation
Getting raw text back is fine for simple tasks. For anything that feeds into application logic, you want structured output. Prism handles this through schema definitions that map directly to PHP objects.
<?php
use Prism\Prism\Facades\Prism;
use Prism\Prism\Enums\Provider;
use Prism\Prism\Schema\ObjectSchema;
use Prism\Prism\Schema\StringSchema;
use Prism\Prism\Schema\IntegerSchema;
use Prism\Prism\Schema\ArraySchema;
$schema = new ObjectSchema(
name: 'article_analysis',
description: 'Analysis of a PHP tutorial article',
properties: [
new StringSchema('summary', 'One sentence summary of the article'),
new IntegerSchema('difficulty_level', 'Difficulty from 1 (beginner) to 5 (expert)'),
new StringSchema('primary_topic', 'The main topic of the article'),
new ArraySchema(
'key_concepts',
'Key technical concepts covered',
new StringSchema('concept', 'A technical concept mentioned in the article')
),
new ArraySchema(
'prerequisite_knowledge',
'What the reader should know before reading this',
new StringSchema('prerequisite', 'A prerequisite concept or skill')
),
],
requiredFields: ['summary', 'difficulty_level', 'primary_topic', 'key_concepts']
);
$articleContent = "Your article text goes here...";
$response = Prism::text()
->using(Provider::OpenAI, 'gpt-4o')
->withSystemPrompt('You analyse PHP and Laravel tutorial articles.')
->withPrompt("Analyse this article:\n\n{$articleContent}")
->withSchema($schema)
->asStructured();
// $response->structured is a fully typed PHP array matching your schema
$analysis = $response->structured;
echo $analysis['summary'];
echo $analysis['difficulty_level'];
echo implode(', ', $analysis['key_concepts']);
No more parsing freeform text. No more stripping markdown fences from JSON responses. Prism handles the structured output negotiation with the model and gives you a validated PHP array. If the model returns something that does not match the schema, Prism throws rather than silently returning garbage data.
Multi-Turn Conversations
<?php
use Prism\Prism\Facades\Prism;
use Prism\Prism\Enums\Provider;
use Prism\Prism\ValueObjects\Messages\UserMessage;
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
$history = [
new UserMessage('What is the difference between Laravel jobs and events?'),
new AssistantMessage('Jobs are queued tasks for deferred work. Events are for broadcasting that something happened in your application...'),
new UserMessage('Can you show me a code example of a job?'),
];
$response = Prism::text()
->using(Provider::Anthropic, 'claude-3-7-sonnet-latest')
->withSystemPrompt('You are a Laravel expert.')
->withMessages($history)
->asText();
echo $response->text;
Part 2: Tool Calling
Tool calling is where things get genuinely interesting. Instead of the AI just generating text, you give it tools it can call, functions that interact with your actual application. The model decides when to use a tool based on the user's request, calls it, gets the result, and incorporates it into its response.
Without tool calling, an AI assistant can only work with what it was trained on. With tool calling, it can check live database records, call external APIs, perform calculations, and do anything else you give it a tool for.
Defining a Tool
<?php
use Prism\Prism\Tool;
// A tool that looks up an order from your database
$orderLookupTool = Tool::as('get_order_status')
->for('Look up the status and details of a customer order by order number')
->withStringParameter('order_number', 'The order number to look up, e.g. ORD-2025-1234')
->using(function (string $order_number): string {
$order = \App\Models\Order::where('order_number', $order_number)
->with('items')
->first();
if (!$order) {
return json_encode(['error' => 'Order not found']);
}
return json_encode([
'order_number' => $order->order_number,
'status' => $order->status,
'placed_at' => $order->created_at->format('d M Y'),
'total' => '$' . number_format($order->total, 2),
'items' => $order->items->count(),
'tracking' => $order->tracking_number ?? 'Not yet assigned',
]);
});
The model sees the tool name, description, and parameter definitions. When a user asks something like "what is the status of my order ORD-2025-4821", the model recognises it needs the order lookup tool, calls it with the order number extracted from the message, gets the JSON result back, and uses it to form a natural language response.
Using Multiple Tools Together
<?php
use Prism\Prism\Facades\Prism;
use Prism\Prism\Enums\Provider;
use Prism\Prism\Tool;
$orderLookupTool = Tool::as('get_order_status')
->for('Look up order status by order number')
->withStringParameter('order_number', 'The order number')
->using(function (string $order_number): string {
// Your order lookup logic here
return json_encode(['status' => 'shipped', 'tracking' => 'TRK123456']);
});
$productSearchTool = Tool::as('search_products')
->for('Search the product catalogue by keyword')
->withStringParameter('keyword', 'Search term to find products')
->withIntegerParameter('limit', 'Maximum number of results to return')
->using(function (string $keyword, int $limit = 5): string {
$products = \App\Models\Product::search($keyword)
->take($limit)
->get(['name', 'price', 'in_stock']);
return json_encode($products->toArray());
});
$refundPolicyTool = Tool::as('get_refund_policy')
->for('Retrieve the current refund and returns policy')
->using(function (): string {
return "Orders can be returned within 30 days of delivery. " .
"Refunds process within 3 to 5 business days. " .
"Items must be unused and in original packaging.";
});
$response = Prism::text()
->using(Provider::OpenAI, 'gpt-4o')
->withSystemPrompt(
'You are a helpful customer support assistant for an online store. ' .
'Use the available tools to look up order details, products, and policies. ' .
'Always check actual data before making claims about orders or policies.'
)
->withPrompt("I ordered something last week, order number ORD-2025-4821. " .
"Has it shipped yet? Also, what's your return policy?")
->withTools([$orderLookupTool, $productSearchTool, $refundPolicyTool])
->withMaxSteps(5)
->asText();
echo $response->text;
The withMaxSteps(5) call is important. It limits how many tool calls the model can make in a single request, preventing runaway chains where the model keeps calling tools indefinitely. Five steps is plenty for most support interactions.
What happens behind the scenes: the model reads the user message, decides it needs to call get_order_status with order number ORD-2025-4821, Prism runs your PHP function, returns the result to the model, the model sees the shipping status, then calls get_refund_policy for the second question, gets that result, and writes a complete response covering both questions using real data from your application.
Part 3: Embeddings for Semantic Search
Embeddings convert text into a vector, a list of floating point numbers that represent the semantic meaning of the text. Two pieces of text that mean similar things will have vectors that are close together in that high-dimensional space, even if the actual words used are completely different.
Prism handles embeddings through the same clean interface as text generation.
<?php
use Prism\Prism\Facades\Prism;
use Prism\Prism\Enums\Provider;
// Generate an embedding for a piece of text
$response = Prism::embeddings()
->using(Provider::OpenAI, 'text-embedding-3-small')
->fromInput('How do I reset my account password?')
->create();
$vector = $response->embeddings[0]->embedding;
// $vector is an array of 1536 floats representing the semantic meaning
You can also embed multiple texts in a single API call, which is more efficient when indexing content:
<?php
$response = Prism::embeddings()
->using(Provider::OpenAI, 'text-embedding-3-small')
->fromInput([
'How do I reset my password?',
'Account recovery steps for locked accounts',
'Changing your email address in account settings',
'Two-factor authentication setup guide',
])
->create();
foreach ($response->embeddings as $index => $embedding) {
echo "Text {$index}: " . count($embedding->embedding) . " dimensions\n";
}
Real-World Example: Tying All Three Together
Here is where it gets practical. I will build a customer support assistant that uses all three Prism features together: embeddings to find relevant knowledge base articles, tool calling to look up live order data, and structured text generation to produce consistent responses.
The scenario is a support bot for a Laravel e-commerce application. The bot needs to answer questions about orders using real database data, find relevant help articles using semantic search, and produce responses that follow a consistent format.
Database Setup for Knowledge Base
php artisan make:migration create_knowledge_base_table
<?php
Schema::create('knowledge_base_articles', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->json('embedding')->nullable();
$table->string('category');
$table->timestamps();
});
The Knowledge Base Indexer
<?php
namespace App\Services;
use App\Models\KnowledgeBaseArticle;
use Prism\Prism\Facades\Prism;
use Prism\Prism\Enums\Provider;
class KnowledgeBaseIndexer
{
public function indexAll(): void
{
$articles = KnowledgeBaseArticle::whereNull('embedding')->get();
foreach ($articles->chunk(20) as $batch) {
$texts = $batch->map(fn($a) => $a->title . "\n\n" . $a->content)
->toArray();
$response = Prism::embeddings()
->using(Provider::OpenAI, 'text-embedding-3-small')
->fromInput($texts)
->create();
foreach ($batch as $index => $article) {
$article->update([
'embedding' => $response->embeddings[$index]->embedding,
]);
}
usleep(200000);
}
}
public function findRelevant(string $query, int $topK = 3): array
{
$queryResponse = Prism::embeddings()
->using(Provider::OpenAI, 'text-embedding-3-small')
->fromInput($query)
->create();
$queryVector = $queryResponse->embeddings[0]->embedding;
// Load articles and compute cosine similarity in PHP
// For production with large knowledge bases, use pgvector instead
$articles = KnowledgeBaseArticle::whereNotNull('embedding')->get();
$scored = $articles->map(function ($article) use ($queryVector) {
$articleVector = $article->embedding;
return [
'article' => $article,
'similarity' => $this->cosineSimilarity($queryVector, $articleVector),
];
})
->filter(fn($item) => $item['similarity'] > 0.75)
->sortByDesc('similarity')
->take($topK);
return $scored->pluck('article')->toArray();
}
private function cosineSimilarity(array $a, array $b): float
{
$dot = array_sum(array_map(fn($x, $y) => $x * $y, $a, $b));
$magA = sqrt(array_sum(array_map(fn($x) => $x ** 2, $a)));
$magB = sqrt(array_sum(array_map(fn($x) => $x ** 2, $b)));
return ($magA * $magB) > 0 ? $dot / ($magA * $magB) : 0.0;
}
}
The Support Assistant Service
<?php
namespace App\Services;
use Prism\Prism\Facades\Prism;
use Prism\Prism\Enums\Provider;
use Prism\Prism\Tool;
use Prism\Prism\Schema\ObjectSchema;
use Prism\Prism\Schema\StringSchema;
use Prism\Prism\Schema\ArraySchema;
class CustomerSupportAssistant
{
public function __construct(
private KnowledgeBaseIndexer $knowledgeBase
) {}
public function respond(string $customerMessage, string $customerId): array
{
// Step 1: Find relevant knowledge base articles using embeddings
$relevantArticles = $this->knowledgeBase->findRelevant($customerMessage);
$knowledgeContext = collect($relevantArticles)
->map(fn($a) => "### {$a->title}\n{$a->content}")
->join("\n\n---\n\n");
// Step 2: Define tools for live data access
$orderTool = Tool::as('get_order')
->for('Look up order details and status for a specific order number')
->withStringParameter('order_number', 'The order number, e.g. ORD-2025-1234')
->using(function (string $order_number) use ($customerId): string {
$order = \App\Models\Order::where('order_number', $order_number)
->where('customer_id', $customerId)
->with('items', 'shipment')
->first();
if (!$order) {
return json_encode([
'error' => 'Order not found or does not belong to this customer',
]);
}
return json_encode([
'order_number' => $order->order_number,
'status' => $order->status,
'placed_at' => $order->created_at->format('d M Y'),
'total' => '$' . number_format($order->total, 2),
'item_count' => $order->items->count(),
'tracking' => $order->shipment->tracking_number ?? 'Not yet assigned',
'carrier' => $order->shipment->carrier ?? null,
'estimated_delivery' => $order->shipment->estimated_delivery ?? null,
]);
});
$accountTool = Tool::as('get_account_info')
->for('Retrieve customer account information like email and membership status')
->using(function () use ($customerId): string {
$customer = \App\Models\Customer::find($customerId);
if (!$customer) {
return json_encode(['error' => 'Customer not found']);
}
return json_encode([
'name' => $customer->name,
'email' => $customer->email,
'member_since' => $customer->created_at->format('M Y'),
'membership_tier' => $customer->tier,
'total_orders' => $customer->orders()->count(),
]);
});
// Step 3: Define schema for structured response
$responseSchema = new ObjectSchema(
name: 'support_response',
description: 'A structured customer support response',
properties: [
new StringSchema('message', 'The response message to send to the customer'),
new StringSchema(
'escalation_level',
'Whether to escalate: "none", "agent", or "manager"'
),
new StringSchema('sentiment', 'Customer sentiment detected: "positive", "neutral", "frustrated"'),
new ArraySchema(
'follow_up_actions',
'Actions the support team should take after this interaction',
new StringSchema('action', 'A follow-up action item')
),
],
requiredFields: ['message', 'escalation_level', 'sentiment']
);
// Step 4: Generate the response using text generation, tools, and schema
$systemPrompt = "You are a helpful and empathetic customer support assistant.
Relevant knowledge base articles for this conversation:
{$knowledgeContext}
Guidelines:
- Use the knowledge base content above when it is relevant to the customer's question.
- Use the available tools to look up live order and account data when needed.
- Keep responses concise and clear, two to three sentences where possible.
- If the customer is frustrated, acknowledge their feelings first before solving the problem.
- Escalate to 'agent' if the issue requires human judgment or account changes.
- Escalate to 'manager' only if the customer is threatening to leave or requests a manager specifically.
- Never guess at order details, always use the get_order tool for specific order questions.";
$response = Prism::text()
->using(Provider::Anthropic, 'claude-3-7-sonnet-latest')
->withSystemPrompt($systemPrompt)
->withPrompt($customerMessage)
->withTools([$orderTool, $accountTool])
->withMaxSteps(4)
->withSchema($responseSchema)
->asStructured();
return $response->structured;
}
}
The Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Services\CustomerSupportAssistant;
class SupportController extends Controller
{
public function __construct(
private CustomerSupportAssistant $assistant
) {}
public function chat(Request $request)
{
$request->validate([
'message' => 'required|string|max:1000',
]);
$customerId = auth()->id();
$message = $request->input('message');
try {
$response = $this->assistant->respond($message, $customerId);
return response()->json([
'reply' => $response['message'],
'escalation_level' => $response['escalation_level'],
'sentiment' => $response['sentiment'],
'follow_up' => $response['follow_up_actions'] ?? [],
]);
} catch (\Exception $e) {
report($e);
return response()->json([
'reply' => 'Something went wrong. Please try again in a moment.',
], 500);
}
}
}
What This System Produces
Here is a realistic example of what the full pipeline returns for a frustrated customer asking about a delayed order:
Customer message: "My order ORD-2025-4821 was supposed to arrive three days ago
and I still haven't received it. This is really frustrating."
System flow:
1. Embeddings search finds "Shipping delays FAQ" and "How to track your order" articles
2. Claude reads the relevant knowledge base content
3. Claude calls get_order tool with order number ORD-2025-4821
4. Tool returns: { status: "shipped", tracking: "TRK987654", carrier: "FedEx",
estimated_delivery: "2 days ago" }
5. Claude generates structured response
Response:
{
"message": "I completely understand your frustration, and I am sorry your order
is running late. I can see ORD-2025-4821 shipped with FedEx and the
tracking number is TRK987654. FedEx is showing a delay on their end,
but the package is still in transit. You can track it directly at
fedex.com using that number for the most current status.",
"escalation_level": "none",
"sentiment": "frustrated",
"follow_up_actions": [
"Monitor order TRK987654 for delivery confirmation",
"If not delivered within 48 hours, initiate trace request with FedEx",
"Flag customer account for priority handling on next contact"
]
}
The sentiment flag lets your frontend show a different UI for frustrated customers. The escalation level drives routing logic. The follow-up actions can be stored and assigned to your support team automatically. This is not just a chatbot, it is a complete support workflow powered by three Prism features working together.
Testing Prism Code
One of the things that makes Prism genuinely production-ready is its testing utilities. You do not want real API calls firing during unit tests. Prism ships with response faking so you can test your application logic without hitting any external APIs.
<?php
use Prism\Prism\Facades\Prism;
use Prism\Prism\Testing\PrismFake;
use Prism\Prism\ValueObjects\TextResult;
it('generates a support response for order queries', function () {
$fakeResponse = new TextResult(
text: '{"message": "Your order has shipped.", "escalation_level": "none", "sentiment": "neutral"}',
finishReason: 'stop',
usage: ['prompt_tokens' => 100, 'completion_tokens' => 50]
);
Prism::fake([$fakeResponse]);
$assistant = app(CustomerSupportAssistant::class);
$result = $assistant->respond('Where is my order?', customerId: 1);
expect($result['escalation_level'])->toBe('none');
expect($result['sentiment'])->toBe('neutral');
Prism::assertCallCount(1);
Prism::assertLastCallUsedProvider('anthropic');
});
Prism::fake() intercepts all Prism calls and returns your predefined responses. Prism::assertCallCount() and Prism::assertLastCallUsedProvider() let you verify your code is making the right calls. Clean, straightforward, and no real API usage during tests.
Switching Providers Without Touching Application Code
One last thing worth showing explicitly, because it is the whole point of Prism. You can make your provider configurable through your .env file so you can switch without a code change:
# .env
AI_PROVIDER=anthropic
AI_MODEL=claude-3-7-sonnet-latest
<?php
// In your service or controller
$response = Prism::text()
->using(
config('ai.provider', 'openai'),
config('ai.model', 'gpt-4o')
)
->withPrompt($prompt)
->asText();
Change the provider in .env, restart the queue workers, done. No code changes, no redeployment of application logic. That is the practical benefit of a unified interface. You build once against Prism's API and gain the flexibility to move between providers as your needs evolve, pricing changes, or a new model comes along that performs better on your specific tasks.
Prism is still relatively young but it is actively maintained and the API has stabilised enough to build production features on. For any new Laravel project that involves AI, it is the first package I reach for now. The alternative, direct API clients for each provider, creates the kind of fragmented codebase that becomes a maintenance problem fast.
No more posts to load.
- Steps to create a Contact Form in Symfony With SwiftMailer
- Building a RAG System in Laravel from Scratch
- Build a WhatsApp AI Assistant Using Laravel, Twilio and OpenAI
- Laravel and Prism PHP: The Modern Way to Work with AI Models
- CIBB - Basic Forum With Codeigniter and Twitter Bootstrap
- Drupal 7 - Create your custom Hello World module
- Build an AI Code Review Bot with Laravel — Real-World Use Case
- Create Front End Component in Joomla - Step by step procedure
- Symfony Framework - Introduction
- A step by step procedure to develop wordpress plugin