AAI Agentic Handbook
Embeddings & Encoders

Encoder

Mid levelReviewed Oct 2026In 1 interview question

A model that reads input text and outputs vector representations of it, rather than generating new text.

In plain English

An encoder is a model that reads text and produces numbers that capture its meaning, instead of writing a reply. In search, encoders turn queries and documents into embeddings, or score how well a query matches a document.

Chat LLMs are decoders (they generate text); encoders are their read-only cousins, usually much smaller and faster.

Analogy

A reader who summarises each document onto a compact index card, but never writes a new document.

Common pitfall

Feeding text longer than the encoder's max length and not noticing it was silently truncated, so the end of every long chunk is invisible to search.

How it actually works

Classic encoders (BERT-style) use bidirectional Attention: every token sees every other token, which suits understanding tasks. The output is one contextual vector per token; for retrieval these are pooled (mean pooling or a [CLS] token) into one embedding (Sentence-BERT), or kept per token for late-interaction models (ColBERT). Several current top embedding models are decoder LLMs adapted with contrastive training, so the distinction is functional rather than architectural.

Two retrieval configurations: the Bi-encoder (query and document encoded independently; fast, indexable) and the Cross-encoder (pair encoded together; accurate, slow). Sizes run from 22M (all-MiniLM-L6-v2) to several billion parameters; maximum input runs from 256–512 tokens on older models to 8,192 on bge-m3 and nomic-embed-text-v1.5. Input beyond the maximum is silently truncated, which is the most common hidden bug in Chunking.

What I'd ship on day one

bge-m3 for multilingual or long chunks; all-MiniLM-L6-v2 when CPU-only and chunks are short; served with Text Embeddings Inference or Sentence-Transformers, batch 32–128, FP16 on GPU; an ingest assertion that token length ≤ max_seq_length.

Worked example

Legal clause search: 2M contract clauses averaging 350 tokens embedded with bge-m3 on one L4 GPU. Assuming ~400 clauses/s at batch 64, the full embed takes ~1.4 hours; a single query embeds in ~8 ms. Swapping in bge-large-en-v1.5 (512-token limit) would have truncated the 14% of clauses longer than 512 tokens.

In the interview

Commit: encoders do the reading in retrieval, and their max sequence length sets your chunk ceiling. Pick by golden-set recall, latency on your hardware and max length, in that order. Follow-up: 'why not use the chat LLM to embed?': cost and latency per vector, and a purpose-trained encoder usually wins on retrieval at 1/100th the size.

Input textHow do I reset my password?
Encodertransformer
vector · e.g. 768 numbers
Text inFixed-length list of numbers outSimilar meaning → nearby vectors
Text in, one vector out: a single bidirectional pass, pooled.
ModelParamsMax tokensDims
all-MiniLM-L6-v222M256384
bge-large-en-v1.5335M5121,024
nomic-embed-text-v1.5137M8,192768 (Matryoshka)
bge-m3568M8,1921,024
Open encoders spanning the size/length trade-off.

Numbers worth knowing

Encoder sizes
22M–7B parametersproduction embedding models, Oct 2026
Max input
256–8,192 tokensmodel-specific; beyond it input is truncated without error
Query encode latency
≈5–15 ms100M–600M model on a GPU, single short query

How to decide

WhenDo this
Chunks exceed 512 tokens or the corpus is multilingualUse an 8k-context multilingual encoder such as bge-m3.
CPU-only and latency budget under 20 msSmall encoder (MiniLM class), shorter chunks.
Golden-set recall gap above 5 points vs a larger modelPay for the larger encoder; ingestion is a one-off cost.

Cost maths

Embedding time = chunks ÷ throughput. 2M chunks at an assumed ~400/s on one L4 (~$0.80/hour, illustrative) ≈ 1.4 hours ≈ $1.10 per full re-embed. Query-side: one GPU serving ~1,000 short queries/s covers most products; the cost driver is keeping a GPU warm 24/7 (~$580/month), so small CPU-served encoders win at low QPS.

Operating it

Time to scale up when…
  • Any non-zero truncation count at ingest
  • Query encode p95 above 30 ms
  • Recall gap to a larger encoder above 5 points on the golden set
What to monitor
  • Chunks over max_seq_length at ingest: alert on any
  • Encoder throughput (chunks/s) during backfills: alert below 50% of baseline
  • Query encode p95: alert above 30 ms

When it goes wrong

What users saw

Questions about indemnity carve-outs in long contracts returned the right contract but never the right clause.

How it was caught

Golden-set slice of 'long clause' questions at recall@10 0.38 vs 0.81 overall.

Root cause

Chunks averaged 900 tokens; the encoder's limit was 512, so the second half of every long clause, where carve-outs sit, was never embedded.

Fix

Switched to an 8,192-token encoder and section-aware chunks capped at 800 tokens, plus an ingest assertion on token length.

Sources

  1. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding — Devlin et al., 2018paper
  2. Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks — Reimers & Gurevych, 2019paper

Check yourself

Three questions. Every option is explained once you answer.

1ConceptWhy does the page call the encoder's maximum input length 'the most common hidden bug in chunking'?
2ScenarioContract clauses average 900 tokens and the corpus mixes English and Spanish. The current encoder is bge-large-en-v1.5 (512-token limit). What should you use?
3CalculationYou re-embed 3.6M chunks at about 400 chunks/s on one L4 GPU costing $0.80/hour. How long does it take and what does it cost?