Course contentsShow
← AI Engineering
Lesson 230 of 1,8866. Embeddings and Semantic SearchFree lesson

Batch Encoding Documents

Efficiently encoding multiple documents at once and managing embedding batches for performance.

Batch Encoding Documents

What you'll learn: How to efficiently convert multiple documents into embeddings at once, rather than one at a time.

Why Batch Processing Matters

When building semantic search systems, you often need to encode dozens, hundreds, or thousands of documents. Encoding them one-by-one is incredibly slow and wasteful. Think of it like doing laundry: you wouldn't run the washing machine for a single sock each time — you batch them together for efficiency.

How Batch Encoding Works

Instead of calling your embedding API or model for each document individually, you group documents into batches and process them together. Most embedding APIs and libraries accept lists of texts:

# Inefficient: one at a time
documents = ["First doc", "Second doc", "Third doc"]
embeddings = []
for doc in documents:
    embedding = get_embedding(doc)  # Separate API call each time
    embeddings.append(embedding)

# Efficient: batch encoding
documents = ["First doc", "Second doc", "Third doc"]
embeddings = get_embeddings(documents)  # Single API call

Managing Batch Sizes

APIs and models have batch size limits. If you have 10,000 documents but your API supports batches of 100, you split them into chunks:

batch_size = 100
for i in range(0, len(documents), batch_size):
    batch = documents[i:i + batch_size]
    batch_embeddings = get_embeddings(batch)
    all_embeddings.extend(batch_embeddings)

Performance Benefits

Batch encoding reduces network overhead, parallelizes computation, and can be 10-50x faster than sequential processing. It's essential for production systems where encoding speed directly impacts user experience.

Key Takeaway: Always encode documents in batches rather than individually — it dramatically improves performance by reducing API calls and leveraging parallel processing.