Brifing

Loop Engineering for RAG Query Parsing

A small but powerful loop design that resolves ambiguities

Towards Data Science3 min

1Problem Definition

When asking 'what is the premium?' on an insurance policy, traditional systems might scan the entire document and return irrelevant sections. The actual premium details could be on page 3 under General Information. This article examines a small loop designed to resolve such ambiguities in document analysis.

2Loop Engineering Concept

Following prompt engineering (2023) and context engineering (2025), loop engineering emerged in 2026 focusing on iterative processes around LLM calls. This discipline aims to close the feedback gap by enabling systems to operate cyclically until achieving their goals.

3Small Loop Example

The small loop exemplified in the article activates when query terms don't match document section names. The system asks one clarifying question like 'I don't see a Premium section, where should I look?' Even brief user responses like 'General Info' effectively guide the next retrieval step.

4Fixed Schema Design

The system pre-defines fields the loop will populate: keywords, intent detection, section hints etc. These fields are used deterministically throughout the pipeline. The loop only completes what the parser can't fill alone, never inventing new fields.

5Six-Step Workflow

1) Parse raw query, 2) Add document context, 3) Detect missing field, 4) Ask user clarifying question, 5) Re-parse enriched query, 6) Continue pipeline. The loop only occurs between steps 3-4, with other steps progressing linearly.

6Code Example

The Python example shows parse_question initially returning empty section_hint. After topic_looks_sectioned check, the system prompts the user. When response includes 'look under General Info', the query automatically enriches and reprocesses.

7Generative AI Integration

The system dynamically generates LLM's 'what's missing?' questions based on document profiles. Testing on arXiv corpus and broker contracts demonstrates how the same loop mechanics adapt across document types. Public repo notebooks allow observing this behavior.

8Container Design Decisions

System fields aren't arbitrary JSON but fixed building blocks. Changes to docintel.question schema affect the entire pipeline. This intentional design ensures consistent data modeling rather than random modifications.

9Big vs Small Loops

While big loops (agentic RAG) involve plan-act-observe cycles, small loops limit to single Q&A turns. This article examines the smallest useful loop on question parsing. Other articles (6A-6C) detail its mechanical implementation.

10Industrial Application

Enterprise Document Intelligence solutions build on four pillars: document parsing, query parsing, retrieval, and generation. This article reads the second pillar through loop engineering lens, showing how production quality is achieved. Integration with other disciplines like context engineering is detailed in article 6quater.

11Experiential Examples

Accompanying Jupyter notebooks demonstrate the small loop working on arXiv academic papers and broker contracts. Users can observe how LLM's clarifying questions vary across document profiles. The doc-intel/notebooks-vol1 repo enables this hands-on experience.

12Critical for Production Quality

Loop engineering becomes key for production-grade LLM systems. This minimally-scoped pattern proactively resolves ambiguities, improving retrieval accuracy. Subsequent articles (like 6bis) expand on mechanics like default values and caching.

Original article: Towards Data Science — Döngü Mühendisliği ile RAG Sorgu Ayrıştırma