You have a 50-page report. You need structured data out of it. The obvious move: write one big prompt that says "extract A, B, C, D, E, F" and dump the whole document in.

This works on short inputs. It quietly breaks on long ones.

The fat prompt problem

When you hand a long document to an LLM with a single extraction prompt, three things go wrong.

Quality drifts. The model forgets mid-document. Later sections get summarized worse than earlier ones. Fields go missing.

One bad field poisons the whole call. If "acceptance criteria" hallucinates, you don't lose that one field. The whole record gets quarantined for review.

Latency climbs, parallelism drops. A single 30k-token call takes as long as it takes. You can't shard it across instances. You wait.

You can fight this with longer prompts, more examples, stricter formatting rules. That buys maybe 10% more reliability and costs a lot of prompt engineering time. The structural problem stays.

The accordion: expand, then compress

The pattern works like an accordion that expands and contracts in two stages.

Stage 1: Segment. One prompt, one job. Read the whole document and return a clean array of segments. Sections, paragraphs, line items, whatever the right unit is for your task.

Stage 2: Extract. One prompt, runs per segment. Take each segment and extract the structured fields you actually want.

Two prompts. Each does one thing.

[ document ]
     │
     ▼
[ Stage 1: segment ] ← one prompt: produce a list
     │
     ▼
[ array of segments ]
     │
     ▼ (fan out)
[ Stage 2: extract ] ← one prompt, runs per segment
     │
     ▼
[ structured records ]

Why this works better

Each prompt has a single job. Stage 1 finds boundaries. Stage 2 extracts the schema. Neither prompt holds both ideas at once. You write each one tightly. Examples are shorter and more relevant.

Errors localize. If Stage 2 fails on segment 7, you re-run segment 7. You don't redo the whole document. Bad fields get isolated to one record instead of contaminating the whole batch.

Stage 2 parallelizes naturally. The output of Stage 1 is an array. Fan it out. Run 50 small extractions in parallel instead of one big one. Total wall-clock time drops. Variance drops too.

Cache hits go up. If the same segment shows up twice, templates, standard headers, repeated forms, Stage 2 sees the same input and you can cache it. The fat-prompt version sees the entire document as one unique input every time.

Long documents stop being scary. The hard limit on a fat prompt is the model's context window. The accordion pattern doesn't have that ceiling. Stage 1 still reads the whole document, but its output is small. Stage 2 only ever sees one segment.

What it costs

It's not free.

You make more LLM calls. One for Stage 1 plus N for Stage 2 instead of one. On short inputs, that's wasteful. The accordion pattern is for documents long enough that fat prompts start failing, not for two-paragraph emails.

You need to think harder about what a "segment" is. Sometimes it's a section heading. Sometimes it's a row in a table. Sometimes it's a logical unit that doesn't map to any visible boundary. That's a design decision and it matters. Get the segment boundary wrong and Stage 2 works with garbage.

When to use it

Reach for the accordion when:

  • The document is long enough that you've seen the model lose the thread mid-way
  • The output schema has more than about 5 fields that don't all care about the same context
  • You need to retry failed records without redoing successful ones
  • You want parallelism

Stick with one fat prompt when:

  • The input is short and the schema is small
  • The fields are tightly coupled, extracting one needs context from another
  • You're prototyping and don't care yet

A concrete example

Stage 1 call:

{
  "model": "google/gemini-3-flash-preview",
  "system_prompt": "Split this document into logical sections. Return one JSON record per section.",
  "example_output": { "section_title": "...", "section_text": "..." }
}

Stage 2 call (one per segment, run in parallel):

{
  "model": "google/gemini-3-flash-preview",
  "system_prompt": "From this section, extract: purpose, mentioned services, acceptance criteria.",
  "example_output": { "purpose": "...", "mentioned_services": [], "acceptance_criteria": [] }
}

Two calls. Each focused. One returns segments. The other turns each segment into structured fields.

The deeper pattern

The accordion pattern is really about separation of concerns applied to prompt engineering. The segmentation prompt handles the structural problem of "where are the boundaries?" The extraction prompt handles the semantic problem of "what does this piece mean?"

Mixing both concerns into one prompt means the model has to hold two different cognitive tasks in its attention window simultaneously. On short inputs, that's fine. On long inputs, it's not. The model's attention budget gets spread thin.

Splitting the tasks means each prompt gets 100% of the model's attention for its specific job. That's why it works.

The same principle applies beyond document extraction. Any task where you need to first understand structure then fill in details benefits from the accordion approach. Data pipeline design. Multi-step analysis. Anything where the "what to look at" decision is separate from the "what to extract" decision.

If your structured extraction is getting flaky on long inputs, the answer probably isn't a longer prompt. It's two prompts.


Suggested title: The Accordion Pattern: Two Prompts Beat One Fat Prompt on Long Documents

Word count: 932

Citations:

Fact-check notes:

  1. The DEV.to article is the primary source for the Accordion Pattern — verify author name "Hideki Mori" and LDX hub references
  2. Verify the StructFlow/LDX hub API examples are accurately represented from the source article
  3. The "10% more reliability" claim is from the original article — verify this is the author's stated figure, not my interpolation
  4. Verify the Gemini-3-flash-preview model name used in the examples — this comes from the source article
  5. The Accordion-Thinking paper on arXiv (2602.03249) is a different technique (step summaries for reasoning) — make sure the article doesn't conflate the two
  6. The "more than 5 fields" heuristic is my extrapolation from the article's guidance — flag this as editorial judgment rather than empirical finding