Generator Invocation
When a deterministic generator can produce an artifact, have the model emit the call rather than the artifact. The model writes a command; code writes the file.
The token cost of a generated file drops from the length of the file to the length of the invocation — a scaffolding command is tens of tokens where its output is thousands — and the result is correct by construction rather than correct by luck.
How It Works
- The model recognizes that the requested artifact has a generator: a scaffolder, a compiler plugin, a template engine, a codemod, a formatter.
- It emits a single tool call:
openapi-generator generate -i spec.yaml -g java,protoc --python_out=...,cargo new,django-admin startapp, ajscodeshifttransform,ruff --fix. - The generator writes the files.
- The model observes the exit code and the list of touched paths — not the contents.
The artifact exists on disk having passed through neither the model's output nor its context.
Why It Beats Generating the Text
- Cost. O(command) instead of O(file), and paid at input rates for the confirmation rather than output rates for the content.
- Correctness. A generator cannot hallucinate an API. Its output is a function of its input.
- Reproducibility. Re-running produces the same bytes; regeneration after an upstream schema change is one command, not a re-prompt.
- Maintainability. The generated code stays regenerable. Hand-written imitations drift from their source of truth immediately.
Where It Applies
- API clients and servers from an OpenAPI, gRPC, or GraphQL schema
- ORM models, migrations, and DB access layers from a schema
- project, module, and component scaffolding
- mechanical refactors expressible as a codemod or AST rewrite
- formatting and lint autofixes
- parsers, lexers, and serializers from a grammar
The Boundary
Generator invocation applies only where a generator exists and the request lands squarely inside what it produces. Three things push work back to the model:
- No generator. Most business logic has none.
- Post-generation customization. If the output needs edits afterwards, the cheap path covers only the first mile.
- Choosing the invocation. Picking the right generator, template, and flags is itself reasoning, and a wrong flag is expensive to discover.
Compared to Write-Path Delegation, this is the stricter and cheaper option: deterministic output, no second model, no verification anxiety — but far narrower coverage. Delegated LLM generation applies anywhere a reference file exists; generator invocation applies only where a generator already encodes the pattern.
Enabling It
Agents do not reach for generators unless the generators are visible and cheap to call:
- expose them as first-class tools or skills rather than hoping the model recalls the CLI syntax
- document the project's generators where the agent reads project instructions
- prefer one wrapper script per generator with named arguments, so the invocation is short and hard to get wrong
- make the tool return a summary — exit code, files touched — not the generated content, or the saving is given straight back
Anti-patterns
- Hand-writing a client the schema could generate, then maintaining it by hand.
- Emitting a 400-line boilerplate file token by token when
startappwould have produced it. - Having the generator print its output to stdout and letting it land in the transcript.
- Reading every generated file afterwards to "confirm" it worked, instead of trusting the exit code and the compiler.
- Reaching for a generator whose output then needs heavy editing — the generator becomes a source of noise rather than of truth.
See Also
- Write-Path Delegation — the stochastic sibling: a cheap model writes the file when no generator exists. Broader coverage, weaker guarantees.
- Context Isolation via Subagents — the same principle on the read side: keep bulk content out of the caller's context.
- Structured Tool Output — generator wrappers should return a tight result object, not their stdout.
- Operation Batching — one generator call replaces a long sequence of individual file writes.
- Fail Fast Validation — the exit code and a compile check replace reading the output.
- Dynamic Tool Loading — generators are exactly the kind of tool worth loading on demand rather than inlining.
- Progressive Disclosure — shipping a script instead of prose is the deepest disclosure level: the agent runs it without reading it.
Tools
- OpenAPI Generator — clients, servers, and docs from an OpenAPI specification.
- protoc — message and service code from
.protodefinitions. - jscodeshift — scripted AST refactors instead of model-written edits.
- ast-grep — structural search and rewrite driven by patterns.
- Cookiecutter — project and module scaffolding from templates.
- Ruff —
--fixapplies mechanical corrections that the model would otherwise rewrite by hand. - Claude Code skills — the usual mechanism for exposing a project's generators to the agent as short, named invocations.