Claude Skills Guide

Claude Code Keeps Adding Code I Did Not Ask For

If you’ve used Claude Code for any significant amount of coding, you’ve probably experienced this: you ask for a small change, and suddenly there are dozens of new lines you didn’t expect. Maybe you asked to fix a bug, and Claude Code refactored the entire module. Perhaps you wanted a simple function, and got a full file with error handling, logging, and tests. This behavior is one of the most common frustrations developers face when working with AI coding assistants. The good news is that understanding why it happens—and learning how to direct Claude Code more precisely—solves the problem in most cases.

Why Claude Code Over-Generates Code

Claude Code’s tendency to add unrequested code stems from how it was trained. The model learned from millions of codebases that follow best practices: comprehensive error handling, thorough documentation, proper type hints, and complete implementations. When Claude Code sees an opportunity to “improve” your code, it often does so proactively, even when you didn’t ask for improvements.

There are several common triggers for this behavior. First, when you ask for a specific change to an incomplete file, Claude Code often fills in missing pieces to make the code functional. Second, the model has strong preferences for following software engineering best practices, so it adds type hints, error handling, and validation that weren’t in your original request. Third, Claude Code sometimes anticipates related functionality you might need and includes it preemptively, especially when working with frameworks that have common patterns.

Understanding these triggers helps you craft prompts that explicitly limit scope.

Techniques to Control Code Generation

The most effective way to prevent unwanted code additions is to be extremely specific in your prompts. Instead of asking “add user authentication,” try “add a simple password check function that returns true or false, no more than 10 lines.” Specificity eliminates ambiguity about what you actually want.

Using constraints in your prompts works remarkably well. Tell Claude Code exactly what to avoid: “Add only the login function, no registration, no password reset, no database changes.” You can also specify file scope: “Only modify the auth.ts file, do not touch any other files.” These constraints dramatically reduce unwanted changes.

The claude-md file in your project is your secret weapon for controlling Claude Code’s behavior long-term. By creating or updating a claude.md file with explicit instructions about your coding preferences, you can establish boundaries that persist across sessions.

# Example claude.md instructions to limit scope
# When making code changes:
# 1. Only modify files explicitly mentioned in the request
# 2. Do not add tests unless specifically asked
# 3. Keep changes minimal and focused on the stated task
# 4. Do not refactor existing code unless it's broken
# 5. Ask before adding new dependencies

Using Claude Code Skills to Constrain Behavior

Claude Code skills provide another powerful way to control code generation. Skills like code-scope help maintain focus on specific tasks, while minimal-change skills explicitly instruct the model to make the smallest necessary modification.

The skill invocation pattern matters significantly. When you invoke a skill with specific parameters, you’re essentially setting boundaries for that session:

# Minimal invocation example
/minimal-change "Add a validateEmail function to utils.js"

Skills can also enforce rules through their configuration. When creating a custom skill, you can specify exactly what the skill should and should not do:

# In skill.md header
constraints:
  - "Never modify more than 3 files per task"
  - "Never add new dependencies without approval"
  - "Always ask before refactoring"

Practical Examples

Let’s look at real scenarios where better prompting eliminates unwanted code.

Problem: You ask Claude Code to “add user validation” and it creates an entire validation module with email, password, username, and phone validation, plus unit tests.

Solution: Be specific about exactly what you need:

# Instead of:
"Add user validation"

# Say:
"Add a single validateEmail(email) function that returns true/false. 
Do not add any other validation. Do not add tests. Do not create a module."

Problem: You want to fix a typo in a string, and Claude Code rewrites the entire function.

Solution: Explicitly limit the scope:

# Instead of:
"Fix the error message in the login function"

# Say:
"Change only the string 'Usr not found' to 'User not found' in login.ts line 42.
Do not modify any other lines."

Problem: You ask for a small helper function, and Claude Code adds JSDoc comments, TypeScript types, error handling, and exports everything.

Solution: Explicitly state what’s not needed:

# Instead of:
"Add a function to format dates"

# Say:
"Add a simple formatDate(date) function in plain JavaScript.
No TypeScript types needed. No JSDoc comments. No error handling.
Just the function, 3-5 lines max."

Configuring Claude Code for Minimal Changes

Your CLAUDE.md file (or claude.md) serves as a persistent instruction set for Claude Code. Here’s an effective configuration for developers who want minimal, focused changes:

# Project preferences
- Prefer smaller, focused changes over large refactors
- Never add new files without explicit permission
- Never modify more files than necessary
- Ask before adding error handling or validation
- Ask before adding tests
- Keep existing code style, don't reformat

# When fixing bugs:
- Only modify the specific line or function causing the issue
- Do not improve unrelated code
- Do not add new functionality

# When adding features:
- Implement only what's explicitly requested
- Ask if additional related functionality might be needed
- Keep implementations simple and minimal

This configuration tells Claude Code your preferences upfront, reducing the need to repeat constraints in every prompt.

When to Allow Extra Code

Sometimes the extra code Claude Code adds is actually beneficial. Error handling, type safety, and proper logging make code more robust. The key is knowing when to accept these additions and when to reject them.

Accept extra code when you’re building new features from scratch, when working on security-sensitive code where error handling matters, or when you’re learning and the additional context helps you understand patterns. Reject extra code when you’re making quick fixes, working in a code review pipeline where large diffs create friction, or when you have specific constraints like minimal bundle size.

The goal isn’t to eliminate all extra code—it’s to have control over what gets added. With practice, you’ll find the right balance between accepting helpful improvements and maintaining the focused scope you need.

Summary

Claude Code keeps adding code you didn’t ask for because it follows best practices learned from millions of codebases. To control this behavior, write specific prompts with explicit constraints, use claude.md to establish persistent preferences, use skills designed for focused changes, and clearly communicate scope limits in every request. The investment in crafting better prompts pays off quickly in cleaner, more predictable code generation.

Built by theluckystrike — More at zovo.one