How to Build a RAG Feature in Laravel 13 with pgvector

Laravel 13 ships a first party AI SDK and native pgvector search. A practical guide to designing a retrieval feature that holds up in production.

4 min read0 comments
How to Build a RAG Feature in Laravel 13 with pgvector

Laravel 13 shipped on March 17, 2026, and its headline feature is a first-party AI SDK. Combined with native vector search on PostgreSQL, it makes retrieval-augmented generation (RAG), where an AI answers questions from your own data, a normal part of building a Laravel app. This guide explains what shipped and how to design a RAG feature that holds up in production.

What shipped in Laravel 13

  • A first-party AI SDK with one API for text generation, image generation, audio, embeddings and vector store integrations, so you can swap model providers without rewriting application code.

  • Native vector search through PostgreSQL and pgvector, integrated with Eloquent.

  • First-class JSON:API resources and PHP attributes extended to middleware, authorization and job retry behavior.

  • PHP 8.3 as the minimum version, with no breaking changes for the upgrade itself.

How RAG works, briefly

  1. Index: split your content into chunks and turn each chunk into an embedding, a list of numbers that captures its meaning.

  2. Retrieve: when a user asks a question, embed the question and find the closest chunks by vector distance.

  3. Generate: send the question plus the retrieved chunks to the model and ask it to answer using only that material, citing the sources.

MacBook with lines of code on its screen on a busy desk
Most of the work in a RAG feature is in the data pipeline, not the API call. Photo by Christopher Gower on Unsplash.

Step 1: Prepare PostgreSQL

Enable the pgvector extension and add a vector column sized to your embedding model. In plain SQL it looks like this; the dimension must match the model you use:

CREATE EXTENSION IF NOT EXISTS vector;

ALTER TABLE document_chunks
  ADD COLUMN embedding vector(1536);

CREATE INDEX ON document_chunks
  USING hnsw (embedding vector_cosine_ops);

Check the Laravel 13 documentation for the migration helpers and the exact SDK methods before you write the application code, since the SDK evolved quickly after launch.

Step 2: Generate embeddings in the background

  • Run embedding as queued jobs so saving a record stays fast.

  • Chunk by meaning (headings, paragraphs), not by fixed character counts alone, and keep a little overlap between chunks.

  • Re-embed a chunk whenever its source changes, and store a content hash so you can skip unchanged chunks.

Step 3: Retrieve with Eloquent

Query the closest chunks for the embedded question, then add normal Eloquent constraints on top. For a multi-tenant app, filter by tenant first. A semantic search that leaks another customer's data is the worst possible bug in this feature.

Step 4: Answer with sources

  • Instruct the model to answer only from the supplied chunks and to say when the answer is not there.

  • Return the chunk references with the answer so users can verify it.

  • Keep the retrieved context small. More chunks add cost and often reduce answer quality.

Production lessons

  • Cost: cache embeddings and repeated answers, and log token usage per tenant.

  • Quality: build a small test set of real questions with known good answers, and re-run it whenever you change chunking, models or prompts.

  • Security: treat retrieved text as untrusted input. A document can contain instructions aimed at the model.

  • Fallbacks: decide what the user sees when the provider is down or the answer is not found.

Should you upgrade first?

Laravel 12 stopped receiving bug fixes on August 13, 2026 and gets security fixes until February 24, 2027. Laravel 13 is supported with bug fixes through Q3 2027 and security fixes through Q1 2028. If you are planning AI features, upgrading to 13 first is the simpler path.

Wrapping up

RAG in Laravel 13 is no longer an infrastructure project. The hard parts are chunking, tenant isolation and evaluation, not wiring up an API. If you want a RAG feature built into your product, MetaLinx works with Laravel, multi-tenant SaaS and AI integrations every day.

Sources

  • #Laravel
  • #Laravel 13
  • #Laravel AI SDK
  • #pgvector
  • #PHP

Share this article

Comments

No comments yet — be the first to share your thoughts.

Leave a comment