Batch size and what the embedding server actually holds #
Recorded 2026-09-14, against models/Qwen3-Embedding-0.6B-Q8_0.gguf on the
machine eval/model-comparison.md used: 16 vCPU AMD EPYC Rome, no GPU, 30 GB
RAM, no swap. Measured with eval/batch-size-sweep.py, which runs its own
llama-server on port 8178 with its own CODE_INDEX_DATA, so the shared server on
8177 and the shared index were untouched throughout.
README.md recorded the server at ~7.8 GB resident when Qwen3-Embedding became
the default, against ~600 MB under nomic-embed-text-v1.5. That reading was taken
straight after model load, which is where it is lowest. A server that has
embedded a few hundred chunks settles at 18.4 GB — 60% of this machine's RAM,
held for as long as the server runs, on a box with no swap to fall back on.
Two settings account for all of it, and neither is the model.
What BATCH_SIZE sets #
embed.py start() derives three llama-server arguments from BATCH_SIZE, and
embed() also sends exactly that many chunks per request:
| argument | value | at BATCH_SIZE 32 |
|---|---|---|
--ctx-size |
CONTEXT * BATCH_SIZE |
65536 |
--batch-size |
CONTEXT * BATCH_SIZE |
65536 |
--ubatch-size |
CONTEXT |
2048 |
The whole request has to fit in --ctx-size at once, so context size cannot be
reduced without reducing the request: the two are one decision. At BATCH_SIZE 32
the context is also twice what the model was trained on, which llama-server
warns about — n_ctx_seq (65536) > n_ctx_train (32768).
Batch size changes no embedding: each input is a separate sequence, pooled on its
own last token. eval/model-comparison.md's retrieval numbers describe any
setting here.
Memory at load, and where it settles #
Both readings are VmRSS from /proc/<pid>/status, and they differ enough that
the choice of moment decides the answer. The sweep embedded the first 184 chunks
of this repository, in path order; the plateau runs embedded 500-600 chunks of a
second, larger repository, since the plateau needs more chunks than this one
holds.
| BATCH_SIZE | --ctx-size |
after load | after one batch | after 184 chunks | plateau | plateau reached by |
|---|---|---|---|---|---|---|
| 1 | 2048 | 902 MB | 941 MB | 6517 MB | — | — |
| 2 | 4096 | 1125 | 1410 | 7109 | — | — |
| 4 | 8192 | 1574 | 2391 | 7202 | 12.1 GB | ~300 chunks |
| 8 | 16384 | 2470 | 3345 | 8037 | 13.0 GB | ~300 chunks |
| 16 | 32768 | 4262 | 5497 | 9818 | 14.9 GB | ~300 chunks |
| 32 | 65536 | 7846 | 9920 | 13411 | 18.4 GB | ~320 chunks |
Subtracting the key/value cache — measured at 112 KB per token of --ctx-size,
from the load column — leaves the same number at every setting:
| BATCH_SIZE | plateau | less 112 KB x ctx | remainder |
|---|---|---|---|
| 4 | 12.1 GB | 0.9 GB | 11.2 GB |
| 8 | 13.0 | 1.8 | 11.2 |
| 16 | 14.9 | 3.7 | 11.2 |
| 32 | 18.4 | 7.3 | 11.1 |
So resident memory is a fixed 11.2 GB plus 112 KB per token of context. Batch size only moves the second term, and no batch size reaches below about 11.4 GB.
Memory readings are repeatable to the megabyte. The sweep ran BATCH_SIZE 32 twice, 40 minutes apart, and both runs reported 7846 / 9920 / 13411 MB at the three sample points.
The fixed 11.2 GB is a prompt cache, and it is useless here #
The server log names it while the plateau is forming:
srv alloc: - making room for prompt cache entry, removing oldest entry (size = 27.019 MiB)
llama-server --cache-ram defaults to 8192 MiB. It caches key/value state per
past prompt so a repeated prompt can skip recomputation. Indexing never repeats a
prompt — every chunk is different text — so the cache fills with entries that
will never be read, and stops growing only when it starts evicting its own
oldest.
Setting LLAMA_ARG_CACHE_RAM=0 removes it, and removes 8.2 GB at both batch
sizes tried, leaving the key/value arithmetic above intact:
| configuration | plateau |
|---|---|
| BATCH_SIZE 32, cache 8 GiB (the previous default) | 18.4 GB |
| BATCH_SIZE 32, cache disabled | 10.2 GB |
| BATCH_SIZE 8, cache 8 GiB | 13.0 GB |
| BATCH_SIZE 8, cache disabled | 4.8 GB |
Disabling it costs no throughput; the section below measures that on a quiet machine.
Search is the one caller that repeats prompts, when the same query is issued twice. That would save one query embedding, about 90 ms, at a standing cost of 8 GB — which is why disabling it is not a trade.
This also explains why nomic-embed-text-v1.5 sat at ~600 MB with identical arguments: measured live, the shared server on port 8177 running nomic holds 362 MB. It is an encoder, computing embeddings without a key/value cache to store, so neither the cache nor the context size costs it anything. The memory here is a property of running a decoder-only model, not of this configuration being wrong before.
Throughput #
Each setting measured twice, in two interleaved passes — 32, 16, 8, 4, then 32, 16, 8, 4 again — so that drift in machine state appears as disagreement between the two readings of one setting rather than as a slope across settings. Every reading is the same 400 chunks of that larger repository, with the prompt cache off, which is the shipped configuration.
| BATCH_SIZE | pass 1 | pass 2 | spread | mean | plateau | extrapolated 37,704-chunk rebuild |
|---|---|---|---|---|---|---|
| 32 | 1.03 | 1.11 | 8% | 1.07 | 10.2 GB | 9.8 h |
| 16 | 1.03 | 1.16 | 13% | 1.10 | 6.6 GB | 9.5 h |
| 8 | 1.09 | 1.07 | 2% | 1.08 | 4.8 GB | 9.7 h |
| 4 | 0.96 | 0.94 | 2% | 0.95 | 3.9 GB | 11.0 h |
32, 16 and 8 cannot be told apart. Their means differ by 3%, while the two readings of BATCH_SIZE 16 differ from each other by 13% — more than the gap between any two of those three settings. Nothing here says 32 is faster than 8.
BATCH_SIZE 4 is the exception, and it is a real signal rather than noise: both of its readings fall below every reading of the other three settings, and they agree with each other to 2%. The ~12% it costs against 8 is about 1.3 hours on a full rebuild. So the batch can be halved twice from 32 for nothing, and the third halving starts to cost.
The plateau column is a sanity check rather than a new measurement, and it reproduced exactly: 10214, 6612, 4797 and 3883 MB, identical to the megabyte across both passes. With the 8 GiB prompt cache gone the same law holds with a smaller fixed term — about 2.95 GB plus 112 KB per token of context — and it predicts all four of those figures to within 40 MB.
The first attempt at this table was discarded #
An earlier pass, taken while two other agents ran test suites on this machine, is not in this document. It measured one configuration at 1.40 and then 0.96 chunks/s — a 46% spread inside a single setting, wider than the spread across every setting — which left every between-setting comparison meaningless. Those runs measure the machine's other occupants rather than the setting, so they were replaced rather than averaged in.
The machine was not perfectly quiet for the replacement either: one unrelated process held about 0.8 of the 16 cores throughout, with the box otherwise 88% idle. A steady load is the case interleaved repeats are meant to handle.
Why these rates sit below 7.4 hours #
eval/model-comparison.md measured 1.42 chunks/s and extrapolated 7.4 hours for a
full rebuild. Rate is per chunk, and this sample's chunks are larger:
the sampled repository averages 321 tokens per chunk against 258 for this one,
which accounts for most of the difference. The rows above are comparable to each
other; the figure to quote for a full rebuild is still model-comparison's.
Where a request actually fails #
An over-long request fails as a 4xx, which embed.py treats as fatal — correctly,
since retrying it would fail identically. That is the risk that makes shrinking
the context worth measuring rather than assuming. It was reproduced, and the
bound is the whole request against --ctx-size, not a single input against
--ubatch-size:
| probe | BATCH_SIZE 32, 16, 8, 4 | 2 | 1 |
|---|---|---|---|
| full batch, dense ASCII at MAX_INPUT_CHARS | ok | ok | ok |
| full batch, CJK at MAX_INPUT_CHARS | ok | ok | ok |
| one ASCII input at 4x MAX_INPUT_CHARS (3878 tokens) | ok | ok | HTTP 400 |
| one CJK input at 4x MAX_INPUT_CHARS (7201 tokens) | ok | HTTP 400 | HTTP 400 |
The error states the arithmetic: request (7201 tokens) exceeds the available context size (4096 tokens). A 7201-token input succeeded at --ctx-size 8192
with --ubatch-size 2048, so an input longer than the physical batch is not by
itself a problem — llama-server splits it across physical batches. All four
probes also pass with the prompt cache disabled, at BATCH_SIZE 32 and 8, so
disabling it does not narrow what a request may contain.
Does the character cap already make an oversized chunk impossible? #
Measured with the server's own tokenizer. The three synthetic inputs are exactly
MAX_INPUT_CHARS = 1800 characters long; the real chunk is whatever the chunker
produced, capped at MAX_CHUNK_CHARS = 1600.
| input | characters | tokens | tokens per character |
|---|---|---|---|
| worst real chunk in the 184-chunk sample | <= 1600 | 455 | <= 0.28 |
| dense ASCII punctuation | 1800 | 969 | 0.54 |
| base64 | 1800 | 1050 | 0.58 |
| CJK | 1800 | 1800 | 1.00 |
A request of BATCH_SIZE inputs is therefore at most about 1800 x BATCH_SIZE
tokens against a context of 2048 x BATCH_SIZE — 12% of headroom, identical at
every batch size, and every full-batch-at-the-cap probe passed. Chunking caps a
chunk at MAX_CHUNK_CHARS = 1600 characters before the 1800-character input cap
applies, so real input keeps more headroom than that.
The margin is empirical rather than structural. The tokenizer works on UTF-8 bytes and a token can be a single byte, so 1800 characters of an unfamiliar three-byte script could in principle exceed 2048 tokens. The densest text tried reached exactly 1.00 tokens per character.
Recommendation #
Ship BATCH_SIZE = 8 with --cache-ram 0, which takes the server from 18.4 GB
resident to 4.8 GB.
The prompt cache is the larger half of that and costs nothing to give up: 8.2 GB for a cache that indexing can never hit, on a machine where the embedding server is shared, long-lived, and cannot be swapped out. Disabling it left every failure probe passing and throughput at the top of the measured band.
Dropping the batch from 32 to 8 is the smaller half, 5.4 GB, and on a quiet machine it is free: the two settings' means differ by 1%, against a repeat-to-repeat spread of up to 13% within one setting. The safety margin against an over-long request is unchanged at 12%, because context and request shrink together.
This is not a trade between memory and hours. There are no hours on the other side of it: 5.4 GB is saved for a throughput difference that two passes over an identical sample could not resolve. Going one step further would be a trade — BATCH_SIZE 4 saves another 0.9 GB and costs a reliable 12%, about 1.3 hours on a full rebuild — which is why 8 is the floor rather than 4.
Reproducing #
export CODE_INDEX_PORT=8178
export CODE_INDEX_LLAMA_BIN=<repo>/vendor/llama-b10483 # a worktree has no vendor/
# memory at load, and the failure probes
uv run python eval/batch-size-sweep.py \
--sizes 32,16,8,4,2,1,32 --sample 256 --out <path>
# where memory settles
uv run python eval/batch-size-sweep.py --mode growth \
--sizes 32,16,8,4 --sample 600 --every 50 \
--repo <a repository of 600+ chunks> --out <path>
# throughput, two interleaved passes
uv run python eval/batch-size-sweep.py --mode growth \
--sizes 32,16,8,4,32,16,8,4 --sample 400 --every 100 \
--repo <a repository of 600+ chunks> --out <path>
The memory figures from before this change was committed need
LLAMA_ARG_CACHE_RAM=8192 to reproduce, since embed.py now passes
--cache-ram 0 itself.
--sample 256 against this repository yielded 184 chunks at the time of the
sweep — it holds fewer than 256, and the count grows as the repository does, so
that column is tied to the commit it was measured at. The growth runs draw on a
larger repository because the plateau needs more chunks than this one contains.
Anything measured after the model swap needs the growth mode, not the sweep: a reading taken before ~300 chunks is still on the ramp and understates the plateau by several gigabytes.
Left unmeasured #
--ubatch-size is the remaining lever on the part of memory that batch size
cannot reach. One run at --ubatch-size 512 against an unchanged --ctx-size of
32768 was climbing through 9.6 GB at 400 chunks where the 2048 setting had
already settled at 14.9 GB, but it had not plateaued and was abandoned in favour
of the prompt cache, which turned out to explain 8.2 GB of the same total.
Whether a smaller physical batch is worth anything once the cache is off is
untested, and it is not free to change: nomic-embed is an encoder, and an encoder
needs its whole sequence inside one physical batch.