Originally published by Cyril Scetbon on June 16, 2025
Building a smart knowledge agent with SurrealDB and Rig.rs
In my previous post, I explored why SurrealDB is unlike any other database: it blends document, graph, relational, and vector models into one engine - and it's written in Rust.
That flexibility makes it a perfect match for Rig.rs, a toolkit for building LLM-native agents in Rust. Today, I’m going to show exactly how the two fit together by building a basic knowledge agent - one that embeds word definitions and answers questions using that content without having to manage the lookup yourself.
Let me rig this thing. 🔧
🧰 Use case: RAG that just works
I’m building a context-aware support agent that:
Ingests internal documents
Embeds them into vector space
Stores everything in SurrealDB
Finds relevant documents using cosine similarity
Passes those to an LLM to generate a response
🚀 Step 1: Initiate the project
The first thing to do is to create the project and add all the dependencies we gonna need
I’ll need an OpenAI key in my environment for the program to works
You can also use a different provider if you prefer. Rig makes it easy and supports many others. Check if your provider is supported at https://docs.rs/rig-core/latest/rig/index.html#model-providers.
📦 Step 2: Ingest & embed
To ingest documents, we need a structure that uses at least the derive macros Serialise and Embed. The Serialise derive macro allows us to serialise data to store in SurrealDB, while the Embed derive macro lets us mark the field description as the source for embeddings. We can now ingest and embed content using SurrealStore from Rig:
The code above:
Creates a WordDefinition struct, where embeddings (a vector of integers used to answer questions) are generated from the definition field.
Embeds the content using OpenAI embedding model text-embedding-3-small which is small and highly efficient
Stores these embeddings in SurrealDB
Ensures everything is searchable with metadata
🤖 Step 3: Ask the agent
Now, I connect a RAG agent and ask it a few questions:
The code above:
Creates an LLM agent using GPT-4.1 Nano, known as the fastest and most cost-effective GPT-4.1 model.
Uses the agent to find the meaning of a word it recognises.
Uses the agent to find a word based on a known definition.
Asks about an unknown word to ensure it doesn't make up an answer.
🤖 Full example
You can test the entire example by cloning the repository cscetbon/surrealdb-rig and running the program.
🕵️ Under the hood: how Rig stores data in SurrealDB
Internally, when documents are stored in SurrealDB, Rig uses a query similar to the following for each item:
See https://github.com/0xPlaygrounds/rig/blob/rig-surrealdb-v0.1.6/rig-surrealdb/src/lib.rs#L125-L132.
Then, when a prompt is sent to the linguist agent using dynamic_context(3, vector_store), Rig executes the following query:
See https://github.com/0xPlaygrounds/rig/blob/rig-surrealdb-v0.1.6/rig-surrealdb/src/lib.rs#L154-L163 and https://github.com/0xPlaygrounds/rig/blob/rig-surrealdb-v0.1.6/rig-surrealdb/src/lib.rs#L110-L112
🧠 Why Rig + SurrealDB rocks
🦀 Rust-native: first-class support, no wrappers
🧩 Unified data model: vector + graph + document in one queryable store
⚡ Fast + flexible: embedded or remote use, even in WebAssembly
🤝 LLM-friendly: Rig agents work out of the box with SurrealDB as a vector store
👉 TL;DR
Stop gluing tools together. Rig your RAG workflow with Rust + SurrealDB.
This post was inspired by [Joshmodev's article](https://x.com/joshmo_dev) on building RAG using Rig, SurrealDB, and DeepSeek on Cloudflare Workers._
