OpenAI Embeddings in Practice: Semantic Search and RAG Implementation

A systematic look at the OpenAI embeddings workflow and engineering best practices for semantic search and RAG.
Drawing on a Reddit practical guide to creating OpenAI embeddings, the article outlines embeddings' foundational role in semantic search and RAG. It covers text preprocessing and chunking, batch embedding API calls, and vector storage with indexed retrieval, while highlighting engineering pitfalls: query and document vectors must come from the same model version, and switching models requires rebuilding the vector store; embeddings are billed by token, so caching and incremental updates matter; and the similarity metric must match how vectors are normalized. It concludes that embeddings are only one link in the RAG chain—ranking, filtering, and prompt organization of retrieval results equally determine final answer quality.
Semantic search and RAG (Retrieval-Augmented Generation) have become core components of AI application development, and OpenAI's embedding models are among the most widely used tools for this purpose. A practical guide on Reddit systematically breaks down how to create OpenAI embeddings. This article builds on the core ideas of that guide and adds details that are easily overlooked in engineering practice.

The Role of Embeddings in Semantic Search and RAG
The essence of embeddings is to map text into high-dimensional vectors so that semantically similar content ends up closer together in vector space. This is the foundation of semantic search: a user query is encoded into a vector, and the system retrieves the most relevant document chunks through similarity computation.
In a RAG architecture, embeddings handle the "retrieval" stage, determining what context the downstream LLM will receive. Retrieval quality directly affects the accuracy and trustworthiness of the final answer, so how embeddings are created, the chunking strategy, and the similarity metric all deserve careful attention.
RAG (Retrieval-Augmented Generation) is a technical architecture that combines external knowledge retrieval with large language model generation. Its basic workflow: first, split the document library and encode it into vectors stored in a vector database; when a user asks a question, the question is likewise encoded into a vector, and the semantically closest document chunks are retrieved; these chunks are then assembled as context into the prompt and handed to the LLM to generate an answer. Compared with relying directly on the memory baked into model parameters, RAG lets answers be grounded in the latest, private materials and can reduce hallucination by citing retrieved results. The embedding model plays the role of a "semantic indexer": it does not participate in generation, yet it determines what information the model can see, and is therefore often regarded as the ceiling on RAG quality.
Key Steps for Creating Embeddings
Text Preprocessing and Chunking
The guide stresses that before calling the embedding API, text needs reasonable cleaning and chunking. Chunk granularity is a parameter that requires trade-offs: chunks that are too large dilute specific semantics in the vector, while chunks that are too small lose context. In practice, you usually need to iterate based on document type and data characteristics rather than applying a fixed value.
Chunking is the process of splitting long documents into short text fragments suitable for embedding. Because embedding models have a fixed input length limit and a single vector struggles to represent multiple topics at once, long text must be split. Common strategies include sliding-window splitting by a fixed character count, splitting along natural boundaries such as paragraphs or headings, and splitting with overlap (adjacent chunks retain some duplicated content to maintain contextual coherence). Chunk size is usually measured in tokens and requires balancing retrieval precision against contextual completeness. For well-structured documents, splitting along semantic boundaries often outperforms mechanical fixed-length splitting; for question-answer corpora, splitting by QA pairs is another option.
Calling the Embedding API
OpenAI provides an embeddings API that converts text into vectors. The guide recommends submitting texts in batches for efficiency while normalizing the returned vectors for later comparison using cosine similarity. Note that different embedding models produce vectors of different dimensions, so switching models means the existing vector store must be rebuilt.
Vector Storage and Retrieval
After generating embeddings, you need persistent storage that supports efficient retrieval. The guide discusses choosing a vector database and the necessity of building indexes for similarity search. Retrieval typically returns Top-K results, and the choice of K also affects RAG recall quality.
Vector databases are systems specifically designed to store high-dimensional vectors and support approximate nearest neighbor (ANN) search. Common options include Pinecone, Weaviate, Milvus, Qdrant, and pgvector. Because computing exact distances across all vectors is prohibitively expensive at scale, these systems generally adopt approximate index structures such as HNSW and IVF, trading a small loss in recall for order-of-magnitude speed gains. Index parameters (such as HNSW's connectivity M and candidate list size ef) affect both retrieval speed and recall, and need tuning based on data scale and latency requirements. When choosing a vector database, you should also evaluate its filtering capabilities, persistence approach, horizontal scalability, and integration cost with your existing stack.
Common Pitfalls in Engineering Practice
Model Version and Dimension Consistency
The guide specifically warns that query vectors and document vectors must be generated by the same embedding model. Mixing different models or different versions of embeddings breaks similarity computation—one of the easiest pitfalls to hit in real projects.
Cost and Caching
Embedding calls are billed by token, and the initial embedding cost for a large document library is not negligible. The guide recommends caching already-generated embeddings to avoid repeated calls; when documents are updated, you should also design an incremental embedding strategy rather than recomputing everything.
Choosing a Similarity Metric
Cosine similarity is the most commonly used metric in semantic search, but the guide also notes that you should choose between dot product and cosine depending on whether vectors are normalized, to avoid bias introduced by a mismatch between the metric and vector processing.
Cosine similarity measures the angle between two vectors in terms of direction, ranging from -1 to 1. Values closer to 1 indicate more aligned directions, and it is insensitive to vector magnitude. Dot product (inner product) is affected by both direction and magnitude. Once vectors are L2-normalized (magnitude 1), the dot product is numerically equivalent to cosine similarity, so computing with the dot product saves the normalization step and is slightly faster. OpenAI's embeddings API returns normalized vectors by default, so dot product and cosine give identical results; but if you apply post-processing such as truncation or weighting that breaks normalization, you should switch to cosine similarity—otherwise long vectors will be systematically overestimated in similarity due to their larger magnitude.
The Full Chain from Embeddings to RAG
The guide treats embeddings as one link in the RAG chain rather than the endpoint. A complete RAG system also needs to consider ranking and filtering of retrieval results, how context is assembled, and prompt design for the LLM generation stage. No matter how good the embedding quality is, if retrieval results are not reasonably organized into the prompt, the final answer may still deviate from expectations.
For developers, the value of this guide lies in reframing embedding creation from an isolated API call into a fundamental engineering problem within semantic search and RAG systems: chunking, model consistency, storage indexing, and cost control—each link needs validation on real data.
Summary
OpenAI embeddings offer a low-barrier starting point for semantic search and RAG, but building a usable system still requires engineering effort in chunking strategy, model version management, similarity metrics, and caching mechanisms. The practical path laid out in this Reddit guide is worth using as an implementation reference, while specific parameters should be determined iteratively on your own dataset.
Related articles

From 0 to 1: Vibe Coding an AI Knowledge Base — A Practical Retrospective and Tech Stack Breakdown
A retrospective on building the AI knowledge base OilBook from scratch with Vibe Coding: Next.js+Neon stack, Oil Dev Skill workflow, Stripe paywall security, interview-style writing, AI-flavor control, and Vercel deployment.

GLM-5.4 Hands-On: Native Vision Meets a New Vibe Coding Workflow
Hands-on with Zhipu GLM-5.4: native vision tested across a 3D product demo, pure-code video generation, and remote-control Vibe Coding, compared with GPT-6.2 and Gemini Neon.

AI and Humans Claim to Prove the Riemann Hypothesis: Machine-Verified, Yet Nobody Can Understand It
Anthropic mathematician and AI Claude claim to prove the Riemann Hypothesis with 17 million lines of Lean code, machine-verified but unreadable by humans. This article analyzes the event, the zero proportion record from 41.6% to 67.2%, and the gap between machine verification and human understanding.