New Tool Radar

AI Concepts

How to Split a Document Longer Than the 8192-Token Embedding Limit

Split a document longer than the 8192-token embedding limit into chunks under the limit before embedding it.

Schematic illustration of split a document longer than the 8192-token embedding limit: step one count the tokens, step two split under the limit, step three embed each chunk.

All OpenAI embedding models enforce a maximum of 8192 tokens per input, and a request that exceeds it fails. A document longer than that must be split before embedding, with each chunk under the limit, and each chunk embedded separately. The split should follow the document’s structure so each chunk still makes sense on its own.

Prerequisites

  • The document’s token count, measured with the model’s tokenizer.
  • A splitter that follows the document’s structure.
  • A way to store several chunks per document, with the document’s name on each chunk.

Steps

Step 1: Count the tokens

Count the document’s tokens with the cl100k_base encoding used by third-generation embedding models. If the count is under 8192, embed it whole; if it is over, split it.

Step 2: Split under the limit

Split the document into chunks of at most 5000 tokens, following headings or paragraphs, so each chunk is well under the limit even after you add a title. Two chunks of 5250 tokens cover a 10,500-token document.

Step 3: Embed each chunk

Embed each chunk separately with the same model, and store the document’s name and the chunk number with each vector. When the document is retrieved, return the matching chunk, not the whole document.

Worked example: Harbor Light library annual report

The fictional Harbor Light library embeds its 10,500-token annual report. The embedding limit is 8192 tokens per input.

Chunk Tokens Under 8192 Stored as
1: report, part 1 5250 Yes annual-report, chunk 1
2: report, part 2 5250 Yes annual-report, chunk 2
Total 10500 Split done 2 chunks

The 10,500-token report becomes 2 chunks of 5250 tokens each, both under the 8192-token limit, and both are embedded.

Expected result. Every chunk is under the 8192-token limit, and each chunk carries the document name and its chunk number. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • A chunk cut mid-sentence loses context; split on structure, not on a fixed token count alone.
  • The 8192-token limit is per input; a request also has a 300,000-token total limit across all inputs.
  • This guide does not cover chunk overlap, which is a separate setting.

Sources

See all