RAG Agent with Mastra and Infinispan

What You Will Learn

How to build an agentic RAG (Retrieval-Augmented Generation) application using Infinispan as the vector store and Mastra as the TypeScript AI framework. The agent queries a corpus of 500 sci-fi movies stored in Infinispan using vector search, then answers questions using the retrieved context.

Prerequisites

  • Node.js 22+

  • An Infinispan Server running on localhost:11222

  • An OpenAI API key

Start an Infinispan Server with Docker or Podman:

docker compose up -d
Tip
You can replace docker with podman in the command above if you use Podman.

Step 1: Install Dependencies

npm install

The project uses the following key dependencies:

  • infinispan-mastra — Infinispan vector store for Mastra

  • @mastra/core — Mastra AI framework

  • @mastra/rag — RAG tools including createVectorQueryTool

  • @mastra/memory — Agent memory for conversation history

Step 2: Configure Environment

Copy the example environment file and add your OpenAI API key:

cp .env.example .env

Edit .env:

OPENAI_API_KEY=sk-...
INFINISPAN_HOST=127.0.0.1
INFINISPAN_PORT=11222
INFINISPAN_USERNAME=admin
INFINISPAN_PASSWORD=password
INFINISPAN_INDEX_NAME=scifi-movies

Step 3: Ingest the Dataset

The ingestion script reads 500 sci-fi movies, chunks each one using MDocument, generates embeddings with OpenAI’s text-embedding-3-small model, and stores them in Infinispan with cosine similarity indexing.

npm run ingest

The script creates an Infinispan index with 1536-dimensional vectors (matching the embedding model output) and indexes each movie’s title and description.

Step 4: Define the Infinispan Agent

The agent uses createVectorQueryTool to perform semantic search over Infinispan.

import { Agent } from "@mastra/core/agent";
import { InfinispanVector } from 'infinispan-mastra';
import { createVectorQueryTool } from '@mastra/rag';
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
import { Memory } from "@mastra/memory";

const infinispanVector = new InfinispanVector({
  id: 'infinispan-vector',
  host: '127.0.0.1',
  port: 11222,
  username: 'admin',
  password: 'password',
});

const vectorQueryTool = createVectorQueryTool({
  vectorStore: infinispanVector,
  indexName: 'scifi-movies',
  model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small")
});

export const infinispanAgent = new Agent({
  id: "infinispan-agent",
  name: "Infinispan Agent",
  instructions: `You are a helpful assistant that answers questions
    based on the provided context...`,
  model: 'openai/gpt-5-nano',
  tools: { vectorQueryTool },
  memory: new Memory(),
});

The createVectorQueryTool connects the agent to Infinispan:

  1. The user’s question is converted into an embedding

  2. The embedding is used to query Infinispan via kNN search

  3. Infinispan returns the most relevant movies

  4. The LLM generates an answer using the retrieved context

Step 5: Run the Agent

npm run dev

This starts Mastra Studio at localhost:4111. Open Agents > Infinispan Agent and ask questions like:

  • "Find 5 movies about UFOs"

  • "What sci-fi movies involve time travel?"

  • "Recommend movies similar to Back to the Future"

Why RAG with Infinispan

The RAG architecture allows using a smaller, cheaper LLM (like gpt-5-nano) for the final answer because Infinispan handles the retrieval of relevant context. The LLM only needs to synthesize the retrieved chunks into a coherent answer, not store all the knowledge in its parameters.

Infinispan provides:

  • kNN vector search with cosine, L2, and inner product similarity

  • Hybrid queries combining vector search with metadata filtering

  • Distributed indexing that scales across cluster nodes

  • In-memory performance with optional persistence

Cleanup

docker compose down