The Technique: Write Generators, Not Repetitive Code
When you need to create multiple similar files, components, or data structures, don't ask Claude Code to write each one individually. Instead, prompt it to write a small generator script that produces the repetitive code for you.
This insight comes directly from developers on Hacker News who've discovered that while token usage "climbs fast" when Claude Code generates repetitive code directly, the model excels at writing the kind of "boring" code that visits ASTs, references patterns, and generates output programmatically.
Why It Works: Token Economics and Model Strengths
Claude Code's token usage scales with output length. Generating 20 similar React components might cost 5,000+ tokens. Writing a 200-line generator script that produces those same 20 components? Maybe 800 tokens.
The math is simple: (Generator Tokens + Generated Code Tokens) < (Direct Generation Tokens). Since you're not paying for the generator's output tokens (they're just file writes), you save dramatically.
This aligns with Claude Code's documented strengths. As noted in our March 24 coverage, "Claude Code agents average 25 navigation actions per code edit"—the model is particularly good at understanding code structure and patterns, making it ideal for writing generators.
How To Apply It: Prompt Patterns That Work
Instead of:
Create 10 API endpoint files for users, products, orders...
Use:
Write a Python script that generates FastAPI endpoint files. It should:
1. Read a YAML file defining resource names and fields
2. Generate CRUD endpoints for each resource
3. Create proper validation schemas
4. Output to appropriate directories
Start by asking me for the YAML structure you need.
Or for frontend work:
Create a React component generator that:
- Takes a JSON config with component names and props
- Generates TypeScript components with proper interfaces
- Adds Storybook stories automatically
- Places them in the correct src/components/ structure
Build Your Generator Library
As one developer reported: "I have already dozens of such small scripts." This is the key insight—once you have a generator for a common pattern (database migrations, API clients, UI components), you reuse it indefinitely.
Store these generators in a ~/dev-generators/ directory or a dedicated repo. When starting a new project, you can even have Claude Code help you adapt existing generators to new frameworks or patterns.
When To Still Use Direct Generation
This technique isn't for everything. Use direct Claude Code generation when:
- You need exactly one instance of something
- The output requires complex, non-repetitive reasoning
- You're exploring and don't yet know the pattern
- The generator would be more complex than the output
But for any task where you find yourself thinking "I need several of these," switch to generator mode immediately.
Complementary Token-Saving Tactics
While writing generators is the most impactful technique, combine it with:
- Use the new CLI integration (announced March 24) that saves "37% tokens vs MCP servers" for command execution
- Keep your
CLAUDE.mdlean—reference external docs rather than pasting them - Use
/compactmode when reviewing large outputs - Stay in the loop—as noted in the source, "You have to think and evaluate deeply" rather than letting Claude Code run unsupervised
The Bottom Line
Stop paying for repetitive token generation. Start building a library of generator scripts. The initial investment in creating a generator pays for itself after just a few uses, and you'll find Claude Code is remarkably good at writing the meta-code that makes your actual coding faster and cheaper.







