Knowledge base (RAG)

Overview

With a knowledge block in voicethere.llm.json, the LLM agent gets one more tool, search_knowledge. The model calls it with a question and gets back the best-matching chunks of your documents, each with a title and a score, and answers from them. Nothing else changes in your agent code.

There are two ways to keep the documents. The Redis store indexes Markdown files from your project into Project Redis every time the content changes. The Supabase store searches a table in your own Supabase project. If you would rather search a prebuilt, read-only docs index, use the docs-rag-mcp project service instead; it needs no embeddings key.

Choose a store

knowledge.store is redis, supabase or off. topK sets how many chunks a search returns, from 1 to 10.

{
  "knowledge": {
    "store": "redis",
    "topK": 4,
    "embeddings": {
      "provider": "openai",
      "dimensions": 512,
      "apiKeySecret": "AGENT_OPENAI_API_KEY"
    },
    "redis": { "namespace": "default" }
  }
}

Use Redis for a few hundred to a few tens of thousands of chunks that change when you deploy, such as product docs, an FAQ or course material. Use Supabase when the data is large or changes outside deploys.

Embeddings

Search compares vectors, so both your documents and every question are turned into vectors by an embeddings API. embeddings.provider is openai, gemini or openrouter. Anthropic has no embeddings API, so choosing it is an error, even when your chat model is from Anthropic.

  • model is optional, except for OpenRouter where you must name one.
  • dimensions is a whole number from 64 to 3072. Smaller vectors use less memory. The agent default is 512.
  • apiKeySecret is the name of an AGENT_* project secret. You can leave it out when the embeddings provider is the same as the chat provider; the chat key is used then. Otherwise it is required.

Register the embeddings provider module in the providers list of createLlmAgent, the same way as for chat.

Project Redis

The Redis store needs Project Redis to be enabled for the project; the agent reads AGENT_REDIS_URL. List the Markdown files that make up the knowledge in agent.ts under knowledge.documents. The agent splits them on headings into chunks of about 300 words and builds the vector index in the background when it starts.

The index is rebuilt only when the documents, the embeddings model or the dimensions change. Unchanged content is not embedded twice, also when several workers start at once. Use one redis.namespace (1 to 40 characters from a-z, 0-9, _ and -) per knowledge set.

Your Supabase pgvector table

With store: "supabase" the agent searches a table in your own Supabase project. VoiceThere never stores your vectors. Run this SQL in your project and fill the table yourself. It follows the Supabase guides Semantic search and Vector columns.

create extension if not exists vector;

create table documents (
  id bigint primary key generated always as identity,
  content text not null,
  embedding vector(512)
);

create or replace function match_documents (
  query_embedding vector(512),
  match_threshold float,
  match_count int
)
returns table (id bigint, content text, similarity float)
language sql stable
as $$
  select
    documents.id,
    documents.content,
    1 - (documents.embedding <=> query_embedding) as similarity
  from documents
  where 1 - (documents.embedding <=> query_embedding) > match_threshold
  order by documents.embedding <=> query_embedding asc
  limit least(match_count, 200);
$$;

The vector size must match embeddings.dimensions. Store the project URL and a key that may only run this function as project secrets, and name them in the config:

{
  "knowledge": {
    "store": "supabase",
    "embeddings": {
      "provider": "openai",
      "dimensions": 512,
      "apiKeySecret": "AGENT_OPENAI_API_KEY"
    },
    "supabase": {
      "urlSecret": "AGENT_SUPABASE_URL",
      "keySecret": "AGENT_SUPABASE_KEY",
      "rpc": "match_documents",
      "matchThreshold": 0.5,
      "cacheTtlSeconds": 600
    }
  }
}

Use a restricted role, not a service-role key. With Project Redis enabled, identical questions are answered from a cache for cacheTtlSeconds (10 to 86400 seconds).

Limits

Project Redis holds 256 MB. The index refuses to grow past 80 % of the memory limit and reports KB_MEMORY_LIMIT in the agent log instead of losing data silently. Two versions of an index side by side during a deploy both count.

Measured on Redis 8.10.3 with Project Redis settings (256 MB, volatile-lru), with chunks of about 300 words: around 24,000 chunks fit under the 80 % guard at 512 dimensions and around 12,000 at 1536. The cost per chunk falls as the index grows, so a small index looks more expensive per chunk than a large one.

DimensionsChunks indexedBytes per chunkChunks that fit in 205 MB
5121,00011,23919,107
5125,0008,83024,320
15361,00025,0958,557
15365,00017,59812,202

Project Redis evicts with volatile-lru: only keys that have an expiry can be evicted, so cache entries go first and index vectors, which have no expiry, stay. When memory is full, writes fail with an error instead of dropping old keys. Above the figures in the table, or when the data changes outside deploys, use the Supabase store.

Dashboard settings

The LLM tab of a project has a Knowledge (RAG) section with the same fields: store, chunks per search, embeddings provider, model, dimensions and key secret, the Redis namespace, and for Supabase the two secrets, the function name, the match threshold and the cache time. Limits are checked as you type. Changes are saved to voicethere.llm.json; publish to apply them.

← All documentation