feat(embedding): route embeddings through OpenRouter via EMBEDDING_MODEL - #176
Merged
Merged
Conversation
EMBEDDING_BASE_URL and EMBEDDING_API_KEY were already wired through to the
AI SDK's createOpenAI({ apiKey, baseURL }), but the model name was hardcoded
to "text-embedding-3-small". OpenAI-compatible gateways namespace models
differently -- OpenRouter serves it as "openai/text-embedding-3-small" -- so
base URL + key alone fail with an unknown-model error.
Add EMBEDDING_MODEL, defaulting to the current constant so existing deploys
are unaffected. Routing through OpenRouter is then purely deploy-time config:
EMBEDDING_BASE_URL=https://openrouter.ai/api/v1
EMBEDDING_MODEL=openai/text-embedding-3-small
EMBEDDING_API_KEY=<OpenRouter key>
Verified against the live OpenRouter API that its vectors are bit-identical
to OpenAI's for this model (cosine similarity 1.0, max elementwise diff 0.0),
so stored embeddings stay valid -- no re-embedding or re-index needed.
dimensions stays hardcoded at 1536 on purpose: the column is halfvec(1536),
so making it configurable would only permit misconfiguration. The tokenizer
needs no change either -- it is pinned to cl100k_base, correct for
text-embedding-3-* regardless of the name prefix.
Also fix scripts/embed-query.ts, which hardcoded the same model and would
otherwise generate query vectors from a different model than the stored ones.
Refs: https://linear.app/tigerdata/issue/AGE-547/route-memory-engine-embeddings-through-openrouter
Contributor
There was a problem hiding this comment.
Pull request overview
Adds deploy-time configurability for the embedding model name so embeddings can be routed through OpenRouter (or any OpenAI-compatible gateway) using environment variables, without changing existing deployments’ behavior.
Changes:
- Introduces
EMBEDDING_MODEL(defaulting totext-embedding-3-small) inbuildEmbeddingConfig, alongside the existingEMBEDDING_BASE_URL/EMBEDDING_API_KEYplumbing. - Updates the
scripts/embed-query.tshelper to respectEMBEDDING_MODELto avoid query-vector/model mismatch. - Expands docs and adds unit tests covering defaulting, override behavior, empty-string fallback, fixed dimensions, and base URL forwarding.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
SELF_HOST.md |
Documents gateway routing via EMBEDDING_BASE_URL + EMBEDDING_MODEL and warns about model/vector-space compatibility. |
scripts/embed-query.ts |
Uses EMBEDDING_MODEL override to keep generated query embeddings aligned with stored embeddings. |
packages/server/start.ts |
Reads EMBEDDING_MODEL from env with a safe default via embeddingConstants.model. |
packages/server/start.test.ts |
Adds unit tests for model default/override behavior and base URL forwarding. |
packages/server/config.ts |
Clarifies embedding configuration constraints (model override, fixed 1536 dims, vector-space compatibility). |
.env.sample |
Documents EMBEDDING_MODEL and provides OpenRouter example configuration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Routes embedding requests through OpenRouter (or any OpenAI-compatible gateway) via deploy-time environment variables, so it can be controlled from our Helm charts.
Most of the plumbing already existed:
EMBEDDING_BASE_URLandEMBEDDING_API_KEYare already wired through to the AI SDK'screateOpenAI({ apiKey, baseURL }). The missing piece was the model name, which was hardcoded totext-embedding-3-smallinpackages/server/config.ts.OpenAI-compatible gateways namespace models differently — OpenRouter serves this one as
openai/text-embedding-3-small— so base URL + key alone would have failed with an unknown-model error.This adds
EMBEDDING_MODEL, defaulting to the existing constant, so current deploys are unaffected.Helm configuration
Existing embeddings stay valid — no re-index
The main risk was a vector-space mismatch, which would silently degrade search quality rather than error. Verified against the live OpenRouter API that its vectors for this model are bit-identical to OpenAI's:
1.00.0So stored vectors remain compatible; no re-embedding or re-index is needed.
Worth noting: OpenRouter's
/modelscatalog omits embedding models entirely (they live on a separate/embeddings/modelsendpoint), which initially made it look like embeddings weren't supported at all. They are.Deliberate non-changes
dimensionsstays hardcoded at 1536. The column ishalfvec(1536), so exposing this would only allow misconfiguration into a runtime failure. Documented inembeddingConstants.cl100k_base, which is correct fortext-embedding-3-*regardless of the name prefix.Also fixed
scripts/embed-query.tshardcoded the same model. Left as-is, it would have generated query vectors from a different model than the stored ones onceEMBEDDING_MODELwas set — a subtle correctness bug. It now mirrors the same override.Testing
buildEmbeddingConfig→generateEmbedding/generateEmbeddings) live against OpenRouter: 1536 dims, single and batch, no errors../bun run check:fullfully green: 1173 + 1551 + 45 pass, 0 fail.Refs: https://linear.app/tigerdata/issue/AGE-547/route-memory-engine-embeddings-through-openrouter