Why my chunking still failed despite tokenization
I had already implemented text splitting, but the context error kept appearing. Only when I accounted for the token budget and special tokens did the cause become clear.

Contents
- The error persisted despite chunking
- Why 512 tokens are not the whole story
- What I take from this
- All articles in the series
The error persisted despite chunking
I had already changed the ingestion worker to split long texts into multiple token chunks. The log showed something that initially looked promising:
// Old worker log showing tight chunk sizes with no safety margin
[IngestWorker] captureId abc | Original tokens: 828 | Chunks: 2 | ChunkSize: 512 | Overlap: 64Yet the embedding provider kept returning the dreaded context overflow error. That was where I had to stop trusting the rough chunk count in the code. A theoretical number in a log is not proof that the vector database provider or local embedding model will actually accept the final input without errors.
Why 512 tokens are not the whole story
My first serious mistake with mxbai-embed-large was simple: I used the hard model limit of 512 tokens directly as the maximum chunk size in the code. That looks logical at first, but in practice leaves far too little room. During processing, a model often adds special tokens internally, such as <s> and </s> at the beginning and end of the text. A simple external token counter also often counts words differently from the embedding model’s internal tokenization pipeline.
If you cut text at exactly 512 tokens, those differences and additional special tokens inevitably push the actual API request beyond the context window’s hard limit. The result: the worker formally segments the input correctly, but leaves no practical safety margin.
That was the decisive diagnosis. Chunking itself was not the problem. The naive assumption that the maximum model limit was also a safe, usable working size for ingestion was.
What I take from this
We must never target the maximum model limit. We need to treat it as an absolute upper bound. Stable embeddings require a deliberate buffer. Only then does the worker remain stable when the model’s tokenizer adds special tokens or minor differences arise during re-encoding.
This diagnosis prompted two fundamental architectural decisions in our Memory Engine monorepo:
- Upgrade the model to
bge-m3: A more capable multilingual model with a much larger context window of 8192 tokens. - Use a conservative token budget: Set a controlled chunk size of 1500 tokens, well below the maximum, to maximize semantic focus and eliminate any risk of overflow.
All articles in the series
- Vision and system overview: Chrome Extension, RAG architecture and project background: Read article
- Building a RAG system: Qdrant, embeddings and cosine similarity in TypeScript: Read article
- AI provider abstraction: Ollama vs. OpenAI, interface design and no vendor lock-in: Read article
- Chrome Extension MV3: Three isolated runtime contexts, message passing and Strategy Pattern: Read article
- Docker Compose strategy: The override pattern, from local development to Azure: Read article
- Ollama locally vs. Docker: The decision and its consequences: Read article
- Ollama auto-pull entrypoint: Automated model setup at container startup: Read article
- tsconfig and Vite:
Node16vs.bundler, and why Vite has its own rules: Read article - Load complete Instagram captions with MutationObserver: Read article
- Chrome Extension foundation with health indicator and retry queue: Read article
- Phase 2 features: Shadow DOM overlay, Tailwind v4 and duplicate detection: Read article
- Race condition in platform detection: How a UI event breaks Instagram detection: Read article
- Extracting PostId in two Instagram layouts: querySelector vs. ancestor traversal: Read article
- Capture a complete Instagram carousel with MutationObserver: Lazy loading, observer-before-click and timeout fallback: Read article
- Notes and tags when saving screenshots: Read article
- Instagram keyboard shortcuts block Chrome Extension input: Read article
- Lowercase normalization and duplicate detection in tag input: Read article
- Zitadel Login V2 in Docker Compose with three hidden errors: Read article
- PKCE OAuth in a Chrome MV3 extension: Read article
- React frontend with react-oidc-context and Zitadel: Read article
- Vite build-time environment variables in Docker: Read article
- Event-driven ingestion with BullMQ and Redis: Read article
- MinIO instead of Azurite: S3-compatible object storage locally and on Hetzner: Read article
- access_token, id_token and the Userinfo endpoint: Which belongs where: Read article
- Qdrant multi-tenancy with one collection per user: Read article
- When backend and frontend know different types: Read article
- Removing Zitadel bootstrap: Host header bug and manual setup: Read article
- Backend code review with six problems fixed before launch: Read article
- Traefik instead of NGINX: Reverse proxy for a growing Docker Compose stack: Read article
- Two-layer rate limiting: Traefik and express-rate-limit with Redis: Read article
- Implementing GDPR Article 17 correctly: Promise.allSettled and export batching: Read article
- Embedding model lock-in: Why mxbai-embed-large is a permanent production decision: Read article
- Docker volumes in production: Named volumes, bind mounts and the Hetzner volume approach: Read article
- Two vulnerabilities before launch: Redis without authentication and an exposed Qdrant admin port: Read article
- Traefik as the single entry point in the Docker Compose stack: Read article
- Connecting Zitadel behind Traefik with issuer, JWKS and Login V2: Read article
- Fixing the frontend when the nginx healthcheck fails on localhost: Read article
- Observability for my Docker Compose stack with Bull Board and Dozzle: Read article
- Open the Qdrant dashboard securely with local Traefik and an SSH tunnel: Read article
- Diagnosis of chunking failures despite tokenization: (this article)
- Why I set the chunk size to 1500 tokens: Read article
- Splitting the embedding workflow into clear steps: Read article
- Bringing chunking, storage and search back together: Read article
- Capture Intake as a separate module, less orchestration in the Express endpoint: Read article
Working on a similar RAG system and want to evaluate the same structure for your project? Let us assess it together.



