One CLAUDE.md Doesn't Fit Every Project: Split Your Templates by Situation, Not by Stack
Most CLAUDE.md templates you'll find are organized by stack: one for React, one for Laravel, one for Rails. When I started using Claude Code for freelance client work, I collected a few of those - and then barely used them.
The problem wasn't the stack sections. It was that the working rules I actually needed had almost nothing to do with the framework. They depended on the situation the project was in:
- Am I building something new, where the conventions are mine to set?
- Am I maintaining code I didn't write, where the priority is not breaking a live system?
- Is this billable client work, where scope, approvals, and handover matter as much as the code?
Same stack, three completely different rule sets. So I reorganized my templates around situations instead. This post walks through what changes between them, and then covers a second, more mechanical safeguard: a hook that stops .env files from leaking into the session through Bash.
Situation 1: Greenfield - the rules are yours, so state them
On a new project, Claude Code will happily invent conventions if you don't provide any - a different error-handling style in every file, tests for some features and not others. The template's job is to be opinionated early, while decisions are still cheap:
- Set the conventions explicitly. Directory layout, naming, how errors are raised and logged, configuration only through environment variables. Vague rules ("write clean code") do nothing; concrete ones get followed.
- Decide what's reversible and what isn't. My greenfield template tells Claude: reversible decisions, just make them and note what you chose. Lasting ones - data model, public API shape, auth approach - present the options and trade-offs and ask first.
- Testing is part of "done." Every new feature ships with tests for its core behavior, and the full suite runs before anything is called finished.
- Keep the file alive. When a convention changes, the
CLAUDE.mdchanges in the same commit. A stale rule is worse than a missing one, because it teaches the agent to ignore the file.
Situation 2: Legacy maintenance - assume everything weird is weird for a reason
Inherited codebases flip most of that on its head. Here the expensive mistake isn't inconsistency, it's breaking something that was working in production. The rules get defensive:
- Assume every odd-looking piece of code is odd for a reason until proven otherwise. Before "cleaning up" something strange, find out what breaks without it - history, comments, the people who use the system.
- Change one thing at a time. Bundled changes make incidents impossible to attribute.
- No opportunistic refactoring. If a fix requires touching shared code, say so and get the scope agreed before starting.
- Production is approval-gated. Present the scope of impact and the rollback plan first. Verify in staging, confirm today's backup exists, then deploy.
- Understand before touching. For an unfamiliar area, the first deliverable is a map - what calls this, what it writes to, what depends on it - and ideally a characterization test that captures current behavior before anyone changes it.
Notice that none of this mentions a framework. A legacy Laravel app and a legacy Next.js app need almost identical rules, because the risk profile is the same.
Situation 3: Client projects - the engagement rules live here too
Billable work adds a layer that has nothing to do with code at all, and it's the layer where freelancers actually lose money. My client-project template carries it alongside the technical rules:
- Scope discipline. When a request arrives that isn't in the estimate, don't quietly absorb it. Summarize the request, estimate its size, and draft the message asking for approval - before starting. (Drafts only: the template also says never send client messages from a tool. Sending is a human action.)
- Surface size problems early. If something in scope turns out to be much larger than estimated, stop and report as soon as that's clear - not at the deadline.
- Keep a work log as you go. Date, hours, what was done. Reconstructing hours at invoice time is where billable time quietly evaporates.
- Define "delivered" up front. What the client receives at handover - code, a handover summary, account and ownership info, renewal dates for domains and certificates - is written down at the start, not negotiated at the end.
- Client material stays in the folder. Anything the client provided is confidential and doesn't get reused in other projects or examples.
Making the templates usable
A few habits that made this work in practice:
- Fill in every placeholder. Anything left in
<angle brackets>is a rule Claude can't act on yet. Project name, actual build/test commands, where backups live - the specifics are the point. - Delete sections that don't apply. A shorter
CLAUDE.mdthat matches reality beats a complete one that doesn't. Stale rules get ignored along with the good ones. - Layer your personal rules separately. Claude Code also reads
~/.claude/CLAUDE.md. Rules that are about you - communication style, commit habits, "always ask before deploying" - belong there, not copied into every project file. Project files should hold project facts.
The mechanical half: keeping .env out of Bash
CLAUDE.md is instruction, not enforcement - it guides judgment, but it isn't a lock. Secrets are the clearest example of why that distinction matters.
The obvious safeguard is permission rules in .claude/settings.json: deny Read and Edit on .env files. That covers the file tools. It does not cover Bash. Secrets leak through shell commands all the time:
cat .envgrep -r "API_KEY" .cp .env /tmp/backup/
A PreToolUse hook can watch the Bash tool specifically and block any command that touches .env-style files. Here's the one I use, in full:
#!/usr/bin/env bash
# PreToolUse hook: keep .env files and credential files out of Claude's hands.
#
# Why a hook in addition to the Read/Edit deny rules in settings.json:
# Permission rules govern the file tools, but secrets also leak through Bash -
# `cat .env`, `grep -r KEY .`, `cp .env somewhere`. This hook watches Bash commands
# that touch .env-style files and blocks them.
# How .env.example is handled:
# The example file (dummy values, meant to be shared) is stripped from the command
# string before matching, so `cat .env.example` passes. Anything left that still
# mentions a real .env file is blocked - including `cp .env.example .env`. That one
# is a legitimate first-run setup step; run it manually in your own terminal.
# Creating the real secrets file is a human moment anyway.
# How it works:
# Same pattern as block-dangerous-bash.sh - JSON on stdin, .tool_input.command,
# exit code 2 blocks execution.
# Requires: jq must be installed.
# How to adjust: if your project stores secrets under a different name (.envrc,
# credentials.json, service-account.json), add it to the pattern list below.
set -euo pipefail
input=$(cat)
tool=$(printf '%s' "$input" | jq -r '.tool_name // empty')
# Only Bash needs this hook; the file tools are covered by settings.json deny rules.
[ "$tool" = "Bash" ] || exit 0
command=$(printf '%s' "$input" | jq -r '.tool_input.command // empty')
# Remove the shareable template name before matching, so it alone never triggers a block.
rest="${command//.env.example/}"
case "$rest" in
*".env"*|*".envrc"*|*"credentials.json"*|*"service-account"*.json*|*".npmrc"*|*".aws/credentials"*)
echo "Blocked by hook (possible secret access): ${command}" >&2
echo "Commands that touch .env / credential files are blocked. If you need a value from one, ask the user to provide it another way; if you were creating .env from .env.example, run that one command manually." >&2
exit 2
;;
esac
exit 0
Wire it up in .claude/settings.json like this:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard-env-files.sh"
}
]
}
]
}
}
Save the script under .claude/hooks/, chmod +x it, and make sure jq is installed. Exit code 2 from a PreToolUse hook blocks the tool call and feeds the stderr message back to Claude, so it understands why it was stopped and can suggest an alternative instead of retrying blindly.
Two design choices worth pointing out:
.env.examplestays readable. It's the shareable template with dummy values, so the hook strips that exact name from the command string before matching.cat .env.examplepasses.cp .env.example .envis blocked on purpose. It's a legitimate setup step - but creating the real secrets file is a moment a human should own, so you run that one command yourself in your own terminal.- And when you genuinely need to know what's in an
.env- usually it's which keys exist, not their values - read it yourself and paste the key names into the conversation. The values rarely need to enter the chat at all.
If your project keeps secrets under different names (.envrc, credentials.json, a service-account file), add them in both places: the settings.json deny list and the hook's pattern list. They guard different doors - file tools versus Bash - and a secrets file needs both doors watched.
Where this setup lives
These templates and hooks are the core of a kit I put together for my own client work and now sell as the Claude Code Freelance Field Kit ($29 on Gumroad): the four situation-based CLAUDE.md templates, five skills (bug investigation, code review, refactor planning, test-adding, handover documents), three hooks including the one above and a dangerous-command blocker, plus the client-work paperwork - estimate research, progress update, and scope clarification prompts, delivery/security/handover checklists, and work-log and estimate templates.
Full disclosure: I'm the seller. The two ideas in this post - situation-based templates and the .env guard hook - are complete as they stand, whether or not the kit is useful to you.
https://osakadxlabo.gumroad.com/l/claude-code-freelance-field-kit
Disclosure: This post was drafted with AI assistance and reviewed and edited by me. The configurations and hook script are from my own working setup.
Comments
No comments yet. Start the discussion.