You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(embedding): reject text over the model token limit - #20
Each model now accepts up to its own context length (nomic 8192, gemma 2048, bge-small 512, minilm 256), capped by a new EMBEDDING_MAX_TOKENS env var that defaults to 2048.
Text over the limit is rejected with a 400 before any embedding runs, instead of being silently truncated. Callers are responsible for chunking.
tokens no longer counts padding, which minilm's tokenizer.json turns on.
compute_sub_batch sizes batches from the configured max length instead of the fixed 100/40 MB per text.
Default cap
Peak memory for nomic on CPU, from cgroup memory.peak in a container limited to 5 GB:
Cap
1 text
4 texts
512
640 MB
898 MB
1024
762 MB
1379 MB
2048
1149 MB
2923 MB
4096
3031 MB
out of memory
8192
out of memory
out of memory
Sub-batching cannot split a single text, so 8192 cannot run within 5 GB. 2048 is the largest tested cap where a batch of 4 still fits. Self-hosters with more memory can raise it.
Testing
cargo fmt, cargo clippy, 26 unit tests and 10 ignored e2e tests pass.
Every "After" value was observed on the running service. "Before" values for the cosine, 20,002-token and padding rows were observed on master. The other "Before" rows follow from the 512 and 256 truncation limits and were not run separately.
Request
Before
After
nomic, two texts differing after 600 tokens (cosine)
1.0
0.998613
nomic, 2048 tokens
truncated to 512
200, tokens: 2048
nomic, 2049 tokens
truncated to 512
400
nomic, 20,002 tokens
200, tokens: 20002
400
minilm, 256 tokens
200
200, tokens: 256
minilm, 257 tokens
truncated to 256
400
minilm, "hi" plus 100 words, tokens
204
105
Example error: text at index 1 has 20002 tokens, the limit for nomic-embed-text is 2048
16 texts of 2048 tokens completed under each memory limit:
Limit
Peak
2 GB
1415 MB
3 GB
1786 MB
5 GB
2907 MB
Known limits
Breaking for callers sending long text. Appwrite's POST /v1/embeddings/text catches embedding errors and returns a generic error for the whole batch, so it needs to surface this 400 before it bumps to this image.
A single 2048-token nomic text peaks at about 1.15 GB, so containers below roughly 1.2 GB need a lower cap.
With EMBEDDING_POOL_SIZE above 1, sub-batches run concurrently and the budget does not account for that. Only a pool size of 1 was tested.
gemma and bge-small were not run, and the memory estimate is fitted to nomic.
The changed rejection path validates the full untruncated token count before inference and returns the intended 400 response.
The service now applies each model’s context limit, capped by EMBEDDING_MAX_TOKENS, and rejects over-limit input with a 400 before running inference. It counts unpadded input tokens, sizes inference sub-batches according to the configured limit, and documents the limits and memory guidance. Tests cover model-specific limits, rejection, and token counting.
Latest changes: The newest commits replace truncation with pre-inference token counting and rejection, map over-limit errors to HTTP 400, and update the documentation and integration tests accordingly.
Verdict
New comments
Fixed
Still open
✅ Approved
0
0
0
📂 Walkthrough · 6
File
Change
README.md
Documents the token cap, rejection behavior, model limits, and memory considerations.
src/embedding.rs
Configures model sequence lengths, counts unpadded input tokens before inference, rejects over-limit text, and scales sub-batches by sequence length.
src/error.rs
Adds a distinct error variant for text that exceeds the token limit.
src/main.rs
Maps over-limit errors to HTTP 400.
src/model.rs
Defines per-model context lengths and tests the supported model limits.
tests/embed_e2e.rs
Covers longer Nomic inputs, over-limit rejection, and token counts that exclude padding.
Reviewed the commits since e860c9c · Details · Comment @appwrite review to re-run, or mention @appwrite with a question.
HarshMN2345
changed the title
fix(embedding): embed up to the model context and count those tokens
fix(embedding): reject text over the model token limit
Nov 18, 2026
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
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.
Fixes #15
What changed
EMBEDDING_MAX_TOKENSenv var that defaults to 2048.400before any embedding runs, instead of being silently truncated. Callers are responsible for chunking.tokensno longer counts padding, which minilm'stokenizer.jsonturns on.compute_sub_batchsizes batches from the configured max length instead of the fixed 100/40 MB per text.Default cap
Peak memory for nomic on CPU, from cgroup
memory.peakin a container limited to 5 GB:Sub-batching cannot split a single text, so 8192 cannot run within 5 GB. 2048 is the largest tested cap where a batch of 4 still fits. Self-hosters with more memory can raise it.
Testing
cargo fmt,cargo clippy, 26 unit tests and 10 ignored e2e tests pass.Every "After" value was observed on the running service. "Before" values for the cosine, 20,002-token and padding rows were observed on
master. The other "Before" rows follow from the 512 and 256 truncation limits and were not run separately.200,tokens: 2048400200,tokens: 20002400200200,tokens: 256400"hi"plus 100 words,tokensExample error:
text at index 1 has 20002 tokens, the limit for nomic-embed-text is 204816 texts of 2048 tokens completed under each memory limit:
Known limits
POST /v1/embeddings/textcatches embedding errors and returns a generic error for the whole batch, so it needs to surface this400before it bumps to this image.EMBEDDING_POOL_SIZEabove 1, sub-batches run concurrently and the budget does not account for that. Only a pool size of 1 was tested.