Stop Letting Claude Code Write Repetitive Code—Make It Write Generators Instead

Stop Letting Claude Code Write Repetitive Code—Make It Write Generators Instead

The most effective token-saving technique isn't cheaper models or tiny prompts—it's making Claude Code write small scripts that generate repetitive code for you.

Ggentic.news Editorial·12h ago·3 min read·6 views
Share:
Source: news.ycombinator.comvia hn_claude_codeCorroborated

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:

  1. Use the new CLI integration (announced March 24) that saves "37% tokens vs MCP servers" for command execution
  2. Keep your CLAUDE.md lean—reference external docs rather than pasting them
  3. Use /compact mode when reviewing large outputs
  4. 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.

AI Analysis

Claude Code users should immediately audit their workflows for repetitive generation patterns. Any task you've done more than twice manually should have a generator script. **Action 1:** Next time you need multiple similar files, prompt Claude Code with "Write a script that generates X based on Y configuration. Make it reusable for future projects." Save that script in a dedicated generators directory. **Action 2:** Review your last week's Claude Code usage. Identify where you generated repetitive code directly. For each pattern, create a generator script now—it will pay off next time. **Action 3:** When starting new projects, check your generator library first. Many can be adapted with minor changes. Use Claude Code to modify them: "Take this React component generator and adapt it for Vue 3 with Composition API." This approach transforms Claude Code from a code writer to a tool builder—a much more sustainable and cost-effective relationship. As the source notes, developers who've adopted this have "dozens of such small scripts" that continue saving tokens long after creation.
Enjoyed this article?
Share:

Related Articles

More in Opinion & Analysis

View all