Write-Path Delegation
Hand a generation subtask to a separate model that writes its output directly to disk, and return only a confirmation to the orchestrator. The generated artifact never appears in the caller's context — not as a tool result, not even as a summary.
This is the output-side counterpart of Context Isolation via Subagents. Isolation keeps bulk material from entering the caller's context on the way in; write-path delegation keeps it from being produced by the caller on the way out.
Why It Saves Twice
A file the orchestrator writes itself costs on both sides of the ledger:
- the input tokens of the reference material it had to read in order to imitate existing conventions, and
- the output tokens of the file itself, billed at the generation rate — typically several times the input rate — and then resident in the transcript for the rest of the session.
Delegating the write eliminates both at once. This is also why the saving is awkward to quantify: there is no baseline in which the same bytes appear on both sides of the comparison.
How It Works
- The orchestrator issues a spec (what to produce), a reference (an existing file to match), and a target path.
- A worker model generates the file from the spec, imitating the reference.
- A wrapper script cleans the output — stripping markdown fences and any preamble the model added — and writes it to the target path.
- The orchestrator receives a confirmation: path and size. Nothing else.
The reference file is not optional. Without something to pattern-match against, a worker with no project knowledge produces code that compiles against an imagined codebase: wrong imports, wrong assertion library, wrong naming. The reference is the substitute for the context the worker does not have.
The worker's system prompt must forbid decoration explicitly — output only the code, no explanations, no markdown fences — or the caller pays to parse prose it did not want.
What Delegates Well
The boundary is predictability from existing patterns:
- unit tests that mirror an existing test for a sibling class
- configuration scaffolding, manifests, CI job definitions
- type stubs, DTOs, interface implementations
- repetitive translations such as i18n resource files
If the correct output follows mechanically from a reference, delegate it. If it requires knowing why the surrounding code is shaped the way it is, keep it.
Verification
The orchestrator has, by construction, not read what was written. Isolation is only safe when something other than the caller's eyes checks the result:
- compile, typecheck, or lint the target file after the write
- run the generated tests
- diff against the reference for structural sanity
Without a verification step, write-path delegation trades tokens for unreviewed code on disk — a bad trade in any codebase that outlives the session.
Trade-offs
- Unreviewed output. Nobody in the loop has seen the artifact. A hallucinated API call is indistinguishable from a correct one until something executes it.
- Latency and size caps. A single delegated generation is one round-trip and may be capped in duration, forcing large files to be split into several calls.
- Blind follow-ups. If the caller later needs to edit the file, it must read it — repaying part of what was saved.
- Reference drift. The worker imitates the reference it was given, not the project's current conventions. A stale reference propagates stale patterns.
Anti-patterns
- Delegating the write, then reading the file back "to check" — this pays the input cost that was just avoided; run a compiler instead.
- Delegating without a reference file.
- Delegating code whose correctness is not mechanically checkable.
- Returning the generated content through the caller "just for the record", which reintroduces the entire output cost.
- Using it for code that is subtle, safety-critical, or architecturally load-bearing.
See Also
- Context Isolation via Subagents — the read-side counterpart; the same isolation principle applied to input.
- Generator Invocation — the deterministic alternative: call a generator instead of having any model emit the file. Cheaper and correct by construction, but only where a generator exists.
- Model Routing — the worker is usually a cheaper model, but the saving here comes from the artifact bypassing the caller, not from the price per token.
- Reasoning Suppression — both remove tokens the caller never needed to see.
- Fail Fast Validation — the verification step that makes unreviewed writes acceptable.
- Subagents as Context Firewalls — the inbound counterpart: neither the parent's history flows down nor the artifact back up.
- Output Spooling to Disk — the read-side counterpart: verbose output goes to disk instead of into the transcript.
Tools
- shunt (Portal by Spotify) — its
code-writescript sends a spec and a reference file to acode-writermode backed by a cheap worker model, strips markdown fences from the reply, and writes straight to the target path; Claude never sees the generated code. Restricted to "tests, config scaffolding, type stubs or anything where the output is predictable from existing patterns". - Claude Code subagents — a subagent with write access can produce files whose contents never enter the parent transcript.
- Aider — applies model output to disk as edits rather than echoing full files back into the conversation.