#5808: fix(memory): truncate oversized chunks before embedding
Cluster:
Timeouts and Memory Management Fixes
When a single chunk exceeds EMBEDDING_BATCH_MAX_TOKENS, it was pushed to the batch without truncation, causing OpenAI API 400 errors. Now truncates text to fit within the token limit.
Fixes #5696
🤖 AI-assisted: Built with Claude Code (claude-opus-4-5) Testing: Fully tested - lint, build, and all tests pass The contributor understands and has reviewed all code changes.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes a batching edge case in `MemoryIndexManager` where a single `MemoryChunk` larger than `EMBEDDING_BATCH_MAX_TOKENS` was being placed into its own batch without truncation, which could trigger OpenAI 400 errors. The change now truncates the chunk text before pushing it as a single-item batch, keeping batch construction aligned with the embedding API limits.
The change is localized to `src/memory/manager.ts` in the embedding batch-building logic used by both the non-batch embedding path and as the fallback path when remote batch embeddings are unavailable/fail.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and addresses a real failure mode, with a small remaining edge case around the truncation heuristic.
- The change is small and correctly targets the oversized-single-chunk path in batching, but the truncation is derived from an approximate chars/token constant; depending on how that constant is tuned, truncation may still produce an estimated token count over the max due to rounding, which could reintroduce the original 400s in a corner case.
- src/memory/manager.ts (batch truncation math)
<!-- greptile_other_comments_section -->
<sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#7810: fix: add fetch timeouts to prevent memory indexing hangs (#4370)
by Kaizen-79 · 2026-02-03
80.7%
#8309: fix: add emb_ prefix to batch embedding custom_id for OpenAI compli...
by vishaltandale00 · 2026-02-03
79.1%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
76.5%
#8675: fix: Gemini batch embeddings state path, enum values, and download URL
by seasalim · 2026-02-04
76.2%
#8888: fix: increase tool result/error context limits
by joetomasone · 2026-02-04
74.4%
#21242: fix(memory): add token budget limits for memory tools (#21187)
by Asm3r96 · 2026-02-19
74.0%
#20149: fix(memory): expose index concurrency as config option
by togotago · 2026-02-18
73.9%
#8890: fix: increase bash output chunk limit from 8KB to 32KB
by joetomasone · 2026-02-04
73.7%
#12737: feat: add maxLines option for memory chunk splitting
by fastroc · 2026-02-09
73.1%
#21217: fix: memory prune command to prevent unbounded MEMORY.md growth
by theognis1002 · 2026-02-19
73.1%