A RAG agent over 500 sci-fi movies in ~60 lines of TypeScript

Reddit r/AI_Agents News

Summary

This article describes a reference RAG agent implementation using Mastra and Elasticsearch vector store, demonstrated on a corpus of 500 sci-fi movies, with the agent built in approximately 60 lines of TypeScript.

Elastic recently added native Elasticsearch vector store support to Mastra, and they put together a reference RAG agent on top of a corpus of 500 sci-fi movies. So we ran an experiment on how it'd work out. The agent itself is pretty small. It has: 1 agent 1 vector retrieval tool 1 memory store ~60 lines of TypeScript Elasticsearch as the vector backend openai/text-embedding-3-small for embeddings openai/gpt-5-nano for answer generation And the flow is: user question → agent decides whether to search → vectorQueryTool → Elasticsearch → retrieved chunks → answer We had retrieval exposed as a tool rather than hardcoded into every turn. So the agent can decide when it needs retrieval, use memory for follow-up questions, and keep the rest of the interaction as a normal agent loop. The core setup looks like this: const esVector = new ElasticSearchVector({ id: "elasticsearch-vector", url: process.env.ELASTICSEARCH_URL!, auth: { apiKey: process.env.ELASTICSEARCH_API_KEY!, }, }); const vectorQueryTool = createVectorQueryTool({ vectorStore: esVector, indexName: process.env.ELASTICSEARCH_INDEX_NAME!, model: new ModelRouterEmbeddingModel( "openai/text-embedding-3-small" ), }); export const elasticsearchAgent = new Agent({ id: "elasticsearch-agent", model: "openai/gpt-5-nano", tools: { vectorQueryTool }, memory: new Memory(), }); A couple of implementation details were interesting: Memory is one line. new Memory() gives the agent conversation context across turns. The retrieval layer is swappable. createVectorQueryTool works across Elasticsearch, PgVector, Pinecone, Qdrant, and other stores Mastra supports. The synthesis model is small. In this example, retrieval is doing most of the information lookup, so gpt-5-nano only has to turn the retrieved chunks into a useful answer. You can improve retrieval without rewriting the agent. The demo starts with vector search, but Elasticsearch can add hybrid search and reranking later while keeping the agent interface the same. And more importantly, the agent only knows that it has a retrieval tool. It doesn’t need to care whether the backend is doing pure vector search, hybrid lexical + vector retrieval, or reranking behind the scenes. For a basic RAG app, you could hardcode: embed → search → prompt → answer Once you want conversational memory and model-controlled retrieval, giving retrieval its own tool keeps the agent code much cleaner.
Original Article

Similar Articles

Building a RAG Pipeline for Semantic Code Search

Lobsters Hottest

JetBrains developers share field notes on building a production-grade RAG pipeline for semantic code search (Air Context), covering parsing, chunking, and vectorization as Part 1 of a series aimed at giving LLM coding agents precise, citable evidence from large codebases.