feat(embeddings): generic per-input token cap across all providers - #3160
Merged
Conversation
Unify the two provider-specific truncation knobs into one generic, provider-agnostic flag and apply the cap at the single choke point (`generate_embeddings_batch`) before any backend's `encode()` runs, so every provider and every path (retain, recall queries, consolidation, import) gets identical truncation. - New: HINDSIGHT_API_EMBEDDINGS_MAX_INPUT_TOKENS (config `embeddings_max_input_tokens`), off by default, applies to all providers. - Deprecated alias: HINDSIGHT_API_EMBEDDINGS_LITELLM_SDK_MAX_INPUT_TOKENS still honored (folded into the generic name at load time). - Move truncation out of LiteLLMSDKEmbeddings into embedding_utils; the `truncate_to_tokens` helper moves to token_encoding.py and returns a TokenTruncation dataclass (no tuple return). - Docs + .env.example (and bundled embed copy) updated; tests migrated to the central path plus config alias/precedence coverage.
This was referenced Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
There were two separate embedding-truncation knobs — a token-based
HINDSIGHT_API_EMBEDDINGS_LITELLM_SDK_MAX_INPUT_TOKENSwired only into theLiteLLM SDK provider (Bedrock Titan, #2501), and a proposed char-based
OpenAI-only cap (#3091). This unifies them into one generic,
provider-agnostic flag and moves the truncation to the single choke point
that every embedding path flows through.
What changed
HINDSIGHT_API_EMBEDDINGS_MAX_INPUT_TOKENS(configembeddings_max_input_tokens), off by default, applies to every provider(local, OpenAI-compatible/llama.cpp, LiteLLM SDK/Bedrock, Cohere, Gemini…).
Token-based (tiktoken
cl100k_base, approximate — set with headroom).HINDSIGHT_API_EMBEDDINGS_LITELLM_SDK_MAX_INPUT_TOKENSis kept as adeprecated alias and folded into the generic name at load time (generic wins
when both are set).
generate_embeddings_batch(the universal boundary for retain, recallqueries, consolidation, and import) instead of inside a single provider's
encode(). Removed the per-providermax_input_tokensfromLiteLLMSDKEmbeddings.truncate_to_tokensmoved totoken_encoding.pyand now returns aTokenTruncationdataclass (no tuple return).Why
Remote providers with a fixed input-token limit (Bedrock Titan V2's hard 8192
cap, or a self-hosted llama.cpp
/v1/embeddingsserver) reject an oversizedinput with a permanent 4xx rather than truncating server-side the way local
SentenceTransformers does. One oversized memory then fails the whole
retain/recall batch. The cap is the escape hatch — and it belongs to every
provider, not just LiteLLM SDK.
Validation
tests/test_embeddings_max_input_tokens.py(new): central truncation +warning, cap-applied-before-backend, no-cap-passes-verbatim, and config
parsing (generic env, deprecated alias, precedence, default-disabled).
test_litellm_sdk_embeddings.pyinto the central path.lint.sh✓,ty check✓, targeted tests green.Docs
configuration.mdmoves the row into the general Embeddings section, notes itapplies to all providers, and records the deprecated alias.
.env.example(and the bundled
hindsight-embedcopy) updated.