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:0xranx OpenContext Embedding Text Truncation

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

Overview

Embedding input text is truncated to 2000 characters to stay within API token limits across all languages, including CJK and Cyrillic.

Description

OpenContext's embedding client truncates each text input to 2000 characters before sending it to the embedding API. This conservative limit accounts for the fact that different languages have different character-to-token ratios: English is approximately 4 chars per token, while CJK and Cyrillic are approximately 1-2 chars per token. With most embedding models having an 8192 token limit, 2000 chars is a safe upper bound that works across all languages without risking API errors. The truncation uses character-based slicing (not byte-based) to preserve valid UTF-8.

Usage

Use this heuristic when dealing with long document chunks that exceed embedding API token limits. The truncation happens automatically in the `embed_batch` function — no manual intervention is needed. If you observe reduced search quality for long documents, consider reducing the chunk size instead of increasing the truncation limit.

The Insight (Rule of Thumb)

  • Action: Texts longer than 2000 characters are truncated to their first 2000 characters before embedding.
  • Value: `MAX_CHARS = 2000`
  • Trade-off: Truncation loses information from the end of long chunks. But this is preferable to API errors or silent failures.
  • Compatibility: Works safely for English (~500 tokens), CJK (~1000-2000 tokens), and Cyrillic (~1000-2000 tokens), all within the 8192 token limit.

Reasoning

The 2000-character truncation limit was chosen based on:

  1. Token limit safety: Most embedding models (OpenAI text-embedding-3, nomic-embed-text, DashScope text-embedding-v4) have an 8192 token limit. At the worst-case ratio of ~1 char/token (CJK), 2000 chars = 2000 tokens, well within limits.
  2. Language-agnostic: The limit uses character count (not byte count or token count), making it simple and predictable across all languages.
  3. Character-based slicing: `t.chars().take(MAX_CHARS).collect()` ensures no multi-byte character is split, producing valid UTF-8 output.
  4. Chunk size coordination: The default chunk size is 1500 characters, which is below the truncation limit. Truncation is a safety net for edge cases where chunks exceed expectations.

Code Evidence

Text truncation from `crates/opencontext-core/src/search/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
// nomic-embed-text and similar models have 8192 token limit
// 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();

Related Pages

Page Connections

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