RAG: Ground Any Agent's Answers in Your Data

Build retrieval-augmented generation for any AI agent in one database — embed your docs, retrieve the most relevant chunks by meaning, and generate a grounded answer in plain SQL. Works with Claude Code, OpenClaw, LangChain, or a voice agent.

All recipes· agents· 12 minutesintermediateen
Instance: localhost:8080

Opens your running SynapCores (RAG: Ground Any Agent's Answers in Your Data will be staged for a preview — nothing runs until you click Run). No instance yet? Install free in ~30s.

Share

Objective

An agent that answers from the base model alone makes things up about your product, your docs, your policies. RAG fixes that: retrieve the most relevant passages from your own data, then generate an answer grounded in them. Most teams wire up a vector store + an embedding service + an orchestration framework to do this. Here you'll do the whole RAG loop — chunk, embed, retrieve, generate — in one database with a few SQL statements. The same pipeline works from any framework or a voice agent — see Use it from your agent at the end.

Step 1: Create the knowledge-base store

One row per document chunk, with an embedding for retrieval.

CREATE TABLE IF NOT EXISTS recipe_rag_docs (
  chunk_id   INTEGER PRIMARY KEY,
  source     TEXT,                                   -- where the chunk came from
  content    TEXT,                                   -- the passage text
  embedding  VECTOR(384)
);

Step 2: Load your knowledge base

Realistic product-doc chunks an agent would need to answer from.

INSERT INTO recipe_rag_docs (chunk_id, source, content) VALUES
 (1,'refund-policy.md','Customers may request a full refund within 30 days of purchase, no questions asked.'),
 (2,'refund-policy.md','Refunds are issued to the original payment method and take 5-7 business days to appear.'),
 (3,'shipping.md','Standard shipping is free on orders over $50 and takes 3-5 business days.'),
 (4,'shipping.md','Express shipping costs $15 and delivers within 1-2 business days.'),
 (5,'account.md','To enable two-factor authentication, go to Settings > Security and scan the QR code.'),
 (6,'account.md','Account data can be exported as JSON from Settings > Privacy at any time.'),
 (7,'plans.md','The Pro plan includes unlimited projects and priority support for $29 per month.');

Step 3: Embed the knowledge base

The embedding model runs in-database — this builds your retrieval index in one line.

UPDATE recipe_rag_docs SET embedding = EMBED(content);

Step 4: Retrieve the most relevant chunks for a question

The "R" in RAG — pull the top passages by meaning for the user's question.

SELECT chunk_id, source, content,
       COSINE_SIMILARITY(embedding, EMBED('How long do I have to return something for a refund?')) AS relevance
FROM recipe_rag_docs
ORDER BY relevance DESC
LIMIT 3;

Step 5: Generate a grounded answer from the retrieved context

The "G" in RAG — stitch the top chunks into the prompt so the answer comes from your data, not the model's guess. First materialize the top-3 chunks (projecting the similarity so we can order by it), concatenate them into one context string, then generate the answer from that context.

CREATE TABLE IF NOT EXISTS recipe_rag_top (
  doc_id    INTEGER PRIMARY KEY,
  content   TEXT,
  relevance DOUBLE
);
INSERT INTO recipe_rag_top (doc_id, content, relevance)
SELECT chunk_id, content,
       COSINE_SIMILARITY(embedding, EMBED('How long do I have to return something for a refund?')) AS relevance
FROM recipe_rag_docs
ORDER BY relevance DESC
LIMIT 3;
CREATE TABLE IF NOT EXISTS recipe_rag_ctx (id INTEGER PRIMARY KEY, context TEXT);
INSERT INTO recipe_rag_ctx (id, context)
SELECT 1, GROUP_CONCAT(content, ' ') FROM recipe_rag_top;
SELECT GENERATE(
  'Answer the question using ONLY the context. If the context does not contain the answer, say you do not know. Context: ' ||
  context ||
  ' Question: How long do I have to return something for a refund? Answer:') AS grounded_answer
FROM recipe_rag_ctx;

Step 6: Watch grounding refuse to hallucinate

Ask something your knowledge base doesn't cover; the "only use the context" instruction makes the agent decline instead of inventing. We reuse the same retrieved context (it has no student-discount fact), so the model must say it does not know.

SELECT GENERATE(
  'Answer the question using ONLY the context. If the context does not contain the answer, say you do not know. Context: ' ||
  context ||
  ' Question: Do you offer a student discount? Answer:') AS grounded_answer
FROM recipe_rag_ctx;

Cleanup (Optional)

DROP TABLE IF EXISTS recipe_rag_docs;
DROP TABLE IF EXISTS recipe_rag_top;
DROP TABLE IF EXISTS recipe_rag_ctx;

Expected Outcomes

  • Step 4 retrieves the two refund-policy chunks for a refund question — by meaning, not keywords.
  • Step 5 answers "30 days, refunded to the original payment method in 5–7 business days" — straight from your docs.
  • Step 6 declines to answer the student-discount question, because the context doesn't cover it — grounding that refuses to hallucinate.

You now have the full RAG loop — chunk, embed, retrieve, generate — running in one database.

Use it from your agent (framework-agnostic — this is the whole point)

RAG is just a doc table + retrieve + generate, so any agent uses it with no framework lock-in:

  • REST / SDKPOST /v1/query/execute (any language), or @synapcores/sdk client.executeQuery(...). Your agent ingests docs once (Steps 1–3), then on each question runs the retrieve+generate query (Step 5) and returns the grounded answer.
  • MCP (native, on by default) — point any MCP client (Claude Code, Cursor, a custom loop, a voice runtime) at ws://<your-instance>/mcp?token=<jwt> (JWT from one POST /v1/auth/loginaccess_token). The query tool retrieves chunks and the execute tool ingests new docs — RAG becomes a tool call, no SDK required.
  • Any framework — OpenClaw, LangChain / LlamaIndex retrievers, a custom loop, or a voice agent all read and write the same knowledge base. The database is the brain; the framework is swappable.

Key Concepts Learned

  • RAG = retrieve relevant chunks by meaning, then generate an answer grounded in them — both in one engine.
  • COSINE_SIMILARITY is the retriever; GENERATE() with a "use only the context" instruction is the generator.
  • That instruction makes the agent decline unknowns instead of hallucinating — the core grounding guardrail.
  • Because it's plain data ops (SQL / REST / MCP), RAG works for any agent — the agent-agnostic backend pattern this cluster builds on.

Tags

ai-agentragretrieval-augmented-generationvectorembeddingsgroundingmcp

Run this on your own machine

Install SynapCores Community Edition free, paste the SQL or Cypher above into the bundled web UI, and watch it run.

Download Free CE