Chunking

The process of splitting long documents into smaller segments suitable for model processing. It keeps inputs within context length limits while preserving localized semantic coherence for LLM ingestion.

General Meaning

Long documents such as public policy circulars, legal acts, and lengthy web pages easily exceed the maximum context window of large language models. Furthermore, feeding excessively long passages into a model at once often causes attention degradation, leading the model to hallucinate or miss critical domain facts located in the middle.

Chunking divides long-form text into appropriately sized segments based on character counts, line breaks, structural sections, or semantic units. It serves as a foundational preprocessing step for retrieval-augmented generation (RAG) and structured batch extraction pipelines, balancing computational efficiency with contextual completeness.

Why It Matters

  • Preventing context window overflow: Keeps prompt size within model token budgets, eliminating abrupt text truncations.
  • Localizing extraction tasks: Constrains prompt instructions to isolated segments, reducing cognitive burden and keeping outputs strictly factual.
  • Providing units for checkpointing and resumption: Cached chunk-level results allow interrupted batch jobs to resume immediately without recomputing finished segments.

How the Site Factory Uses It

In the Site Factory, the deterministic extraction engine (engine/extract.py) splits public subsidy announcements and municipal regulations using configurable chunk boundaries. Each project specifies extract.chunk_chars in design.json (defaulting to 3,000 characters, schema constrained between 500 and 20,000 characters). The chunks(text, limit) function splits along newline boundaries to prevent splitting sentences midway.

When running on local models with smaller num_ctx limits, the piece_size helper dynamically subtracts prompt templates and response margins to shrink chunk thresholds. Each chunked prompt explicitly states segment progress (e.g., page 1/3), instructing the LLM to extract only verified details present in that segment. Results are cached by request hash under data/extracted/cache/ to ensure idempotency.

Caveats & Operational Rules

  • Do not slice arbitrarily by character counts: Mechanically truncating in the middle of words or sentences destroys linguistic context; splitting along newline boundaries is strongly preferred.
  • Handle cross-chunk information loss: Splitting table headers away from their data rows can cause extraction failures; chunk sizes must be configured large enough to retain structural context.

Related Terms

References

  • Reference: Standard RAG Chunking Strategies and Context Window Optimization in Large Language Models

Verified: 2026-11-08