DevelopmentGlossary
Embedding
Embeddings convert text into float vectors, typically with hundreds to a few thousand dimensions. "king" and "queen" land near each other; "king" and "banana" don't. This makes similarity a math operation (cosine distance, dot product) instead of a string match.
Every semantic search, RAG system, and recommendation engine built on LLMs depends on embeddings. Options include hosted embedding APIs from the major model providers and open-weight models you can run yourself; compare them on your own data. Embedding costs far less per token than text generation. The harder problems are chunking strategy and reranking, not the embedding call itself.
Example
# Embed a chunk, store in pgvector
from openai import OpenAI
client = OpenAI()
def embed(text: str) -> list[float]:
return client.embeddings.create(
model=EMBEDDING_MODEL, # the embedding model you chose
input=text
).data[0].embedding
# Later, at query time:
query_vec = embed(user_question)
# SELECT slug, content FROM docs ORDER BY embedding <=> $1 LIMIT 10When to use it
- Building semantic search or RAG
- Clustering / deduplication / recommendation
- Similarity-based routing (find the closest FAQ answer)
When not to use it
- Keyword / lexical match is what you actually need; BM25 is simpler and often better
- Domain is super specialized and generic embeddings miss the nuance (fine-tune or use a specialist embedder)
Articles that use this
- Grounding Prompts With Retrieved Context: The RAG Prompt Patterns That Stop Invented AnswersTechniques
- Adaptive Few-Shot Prompting: Use Semantic Retrieval to Pick Your Examples Instead of Hardcoding ThemTechniques
- Red-Teaming Your Own AI App: A Prompt Injection Test Suite You Can Run TodaySecurity
- Prompt Injection Testing for Production Apps: A Red Team PlaybookSecurity

