## TL;DR

A 40k-line compiled model is mostly inlined ephemeral CTEs; reading it whole overflows context and is unnecessary. The agent should debug from the source `.sql` file, and only read the compiled output in targeted slices around the failing CTE.

## Error

```text
agent failed with context overflow reading a 40k-line compiled model
```

## Steps

1. Stop reading the compiled file. Open the source model `.sql` instead; it is the file the agent will actually edit. Expected: a human-scale file.
2. Find the failing part from the error message (it usually names a CTE or a line region). Expected: a narrow target within the compiled output.
3. Read only that slice of the compiled file (for example with `sed -n` line ranges around the error). Expected: hundreds of lines, not tens of thousands.
4. Map the slice back to the source: ephemeral CTEs correspond to ephemeral model files. Expected: the agent knows which source file to fix.
5. Fix the source, recompile with `dbt compile --select [MODEL]`, and verify the slice. Expected: the defect is gone from the compiled output.

## When to use

- Compiled model SQL is tens of thousands of lines.
- An agent needs to understand a compile or runtime failure in a big model.

## When not to use

- Small compiled files (read them directly).
- The failure is in the source SQL itself (fix it there without the compiled file).

## Tool compatibility

- dbt Core 1.0 and later, all adapters. Compiled output lives in `target/compiled`.

## Variant phrasings

### Agent summarizes the compiled file instead of reading it

Summarization still needs the whole file; slice first.

### 40k lines from nested ephemeral models

Flatten the chain mentally: each CTE is one ephemeral model file.

## Why it happens

Ephemeral models inline their full SQL as CTEs into every downstream query, and chains of them multiply. The compiled file is a mechanical expansion, not a document; treating it as one overwhelms any reader.

## Edge cases

- Line numbers in warehouse errors refer to the compiled SQL; map them to the slice, then to the source.
- `dbt compile --select` one model at a time keeps the output focused.
- If the source itself is huge, split the model; 40k compiled lines often hide a modeling problem.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_Dr3k7gxDK0VSQt4-AIQv0Q
