Heuristic:0xranx OpenContext Embedding Batch Size Tuning
| Knowledge Sources | |
|---|---|
| Domains | Search, Optimization |
| Last Updated | 2026-10-24 10:00 GMT |
Overview
Embedding batch size tuned to 10 to accommodate DashScope and other APIs with lower batch limits, with 2000-character text truncation for safety.
Description
OpenContext's embedding client processes texts in batches to balance API throughput and rate limit constraints. The default batch size is 10, which is deliberately conservative because DashScope and some other OpenAI-compatible APIs limit batch size to 10. Each text is truncated to 2000 characters before sending to avoid token limit errors across all languages. The client auto-detects actual embedding dimensions from the first API response and re-initializes the vector store if there is a mismatch.
Usage
Use this heuristic when configuring embedding batch size for different providers. OpenAI can handle larger batches (up to 100+), but if you use DashScope or other compatible APIs, keep the batch size at 10. Increasing batch size improves throughput but risks hitting API rate limits or batch size caps.
The Insight (Rule of Thumb)
- Action: Set `batch_size = 10` for multi-provider compatibility.
- Value: `batch_size = 10`, `MAX_CHARS = 2000` (text truncation limit)
- Trade-off: Smaller batches = slower indexing but safer with rate-limited APIs. Larger batches = faster indexing but may exceed API limits.
- Compatibility: DashScope limits to 10; OpenAI supports more. The `dimensions` parameter is only sent for `text-embedding-3-*` models.
Reasoning
The batch size of 10 was chosen as a conservative default because:
- DashScope compatibility: DashScope (Alibaba Cloud) limits embedding batch size to 10. Since OpenContext supports DashScope via `EMBEDDING_API_BASE`, the default must work with the most restrictive provider.
- Rate limit safety: Smaller batches reduce the risk of hitting per-request rate limits, especially during large index builds processing hundreds of documents.
- Text truncation: The 2000-character limit is a conservative approximation of the ~8K token limit of most embedding models, accounting for CJK/Cyrillic languages where the char-to-token ratio is lower (~1-2 chars/token vs ~4 for English).
Code Evidence
Default batch size from `crates/opencontext-core/src/search/config.rs:92-94`:
fn default_batch_size() -> usize {
10 // DashScope and some other APIs limit batch size to 10
}
Batch processing in embedding client from `embedding.rs:101-109`:
// Process in batches
let mut all_embeddings = Vec::with_capacity(texts.len());
for batch in texts.chunks(self.config.batch_size) {
let batch_embeddings = self.embed_batch(batch.to_vec(), &api_key, &url).await?;
all_embeddings.extend(batch_embeddings);
}
Text truncation from `embedding.rs:129-143`:
// Truncate texts that are too long (most embedding APIs have ~8K token limit)
// Using char count as approximation: ~4 chars per token for English, ~1-2 for CJK/Cyrillic
// Conservative limit: 2000 chars (safe for all languages including Cyrillic/CJK)
const MAX_CHARS: usize = 2000;
let texts: Vec<String> = texts
.into_iter()
.map(|t| {
if t.chars().count() > MAX_CHARS {
t.chars().take(MAX_CHARS).collect()
} else {
t
}
})
.collect();
Dimension auto-detection from `embedding.rs:198-213`:
if let Some(first) = data.first() {
let detected_dim = first.embedding.len();
if current == 0 {
self.actual_dimensions.store(detected_dim, Ordering::Relaxed);
log::info!("Auto-detected embedding dimensions: {}", detected_dim);
} else if current != detected_dim {
log::warn!("Embedding dimension mismatch: expected {}, got {}", current, detected_dim);
}
}