Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:1Panel dev MaxKB Embedding Batch Size Tip

From Leeroopedia
Knowledge Sources
Domains RAG, Optimization
Last Updated 2026-10-10 00:00 GMT

Overview

Batch embedding documents in chunks of 10 paragraphs via `sub_array()` to balance memory usage and database write throughput.

Description

MaxKB's vector store splits document paragraphs into fixed-size batches before calling `embed_documents()` and `bulk_create()`. The `sub_array()` utility function chunks a list into sub-lists of at most `item_num=10` items. Each batch is embedded as a single API call and written to PostgreSQL via Django's `bulk_create`. This prevents loading all paragraphs into memory at once and avoids overwhelming the embedding model API or PostgreSQL's query buffer.

Usage

Use this heuristic when tuning embedding throughput for large knowledge bases. If you encounter memory pressure during document vectorization or embedding API rate-limit errors, the batch size of 10 is the key parameter to adjust. This applies to any RAG workflow that triggers `batch_save()` or `save()` on the vector store.

The Insight (Rule of Thumb)

  • Action: The batch size is controlled by the `item_num` parameter in `sub_array()`, defaulting to 10.
  • Value: `item_num=10` (default). Increase to 20-50 for local embedding models with abundant RAM; decrease to 5 for rate-limited API-based embeddings.
  • Trade-off: Larger batches reduce API round-trips but increase peak memory. Smaller batches are gentler on rate limits but slower overall.
  • Compatibility: Works with any `Embeddings` implementation (OpenAI, local sentence-transformers, etc.) because batching happens before the embedding call.

Reasoning

The `sub_array()` function at `apps/common/utils/common.py:205-215` splits a list into chunks of `item_num` (default 10). The `batch_save()` method in `base_vector.py:126-141` iterates over these chunks, calling `_batch_save()` for each. Inside `_batch_save()` (pg_vector.py:79-110), `embedding.embed_documents(texts)` is called on the batch, then `QuerySet(Embedding).bulk_create(embedding_list)` writes all rows in a single SQL INSERT. This means each batch of 10 paragraphs results in exactly one embedding API call and one database INSERT. For a 500-paragraph document, that is 50 API calls and 50 INSERTs — a manageable load for most providers. Increasing the batch size can reduce total API calls but risks timeouts on slow embedding endpoints.

Code Evidence

Batch splitting with `sub_array()` from `apps/common/utils/common.py:205-215`:

def sub_array(array: List, item_num=10):
    result = []
    temp = []
    for item in array:
        temp.append(item)
        if len(temp) >= item_num:
            result.append(temp)
            temp = []
    if len(temp) > 0:
        result.append(temp)
    return result

Batch save loop from `apps/knowledge/vector/base_vector.py:126-141`:

def batch_save(self, data_list: List[Dict], embedding: Embeddings, is_the_task_interrupted):
    self.save_pre_handler()
    chunk_list = chunk_data_list(data_list)
    result = sub_array(chunk_list)
    for child_array in result:
        if not is_the_task_interrupted():
            self._batch_save(child_array, embedding, is_the_task_interrupted)
        else:
            break

Single-save also uses sub_array from `apps/knowledge/vector/base_vector.py:120-124`:

chunk_list = chunk_data(data)
result = sub_array(chunk_list)
for child_array in result:
    self._batch_save(child_array, embedding, lambda: False)

Batch embedding + bulk_create from `apps/knowledge/vector/pg_vector.py:79-109`:

def _batch_save(self, text_list, embedding, is_the_task_interrupted):
    texts = [normalize_for_embedding(row.get("text")) for row in text_list]
    embeddings = embedding.embed_documents(texts)
    embedding_list = [Embedding(...) for index in range(0, len(texts))]
    if not is_the_task_interrupted():
        QuerySet(Embedding).bulk_create(embedding_list) if len(embedding_list) > 0 else None

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment