Set up Claude Code for a data project, with guardrails
Give the agent a short CLAUDE.md with your layers, naming and test command. Then add what actually stops it: a reviewer subagent limited to read-only tools, deny rules for .env and secrets, and a PreToolUse hook for hard rules. CLAUDE.md is context the agent follows, not enforcement. Permissions and hooks enforce.
Data engineers · ·
The problem
You paste SQL into a chat, get an answer, and start from zero next time. The assistant does not know your layers, your naming or your environments. Once it works inside the repository it can also read your .env or run a DROP TABLE. This Pattern is for data engineers who want an agent that knows the project and has limits.
Steps
Four layers around the agent. The first one asks, the other three stop.
All files below go in the repository, so the whole team gets them. Nothing here was loaded by Claude Code in a test. The Files to copy section says what was checked.
-
Write a short
CLAUDE.mdat the repository root. Put facts the agent cannot guess: layers, names, environments, how to run tests. Anthropic recommends under 200 lines per file, because longer files use more context and are followed less well. Concrete lines such as “noSELECT *” work better than “write clean SQL”.# Project: sales-lakehouse ## Layers - bronze: raw data, never edited. silver: typed and deduplicated. gold: reporting tables. - Names: snake_case with the layer as prefix, for example silver_orders. ## Environments - Work in dev only. Never run anything against prod. ## Commands - Tests: `pytest -q` ## SQL style - Name the columns. No SELECT *. ## Rules - Never read .env or anything under secrets/. - Ask before any write outside this repository. - Show the command and its real output before saying a task is done. -
Move rules that apply to one kind of file into
.claude/rules/. A rule file with apathsfield loads only when Claude works with a matching file, so the main file stays short.--- paths: - "sql/**/*.sql" --- # SQL rules - Every join states its key and the expected cardinality in a comment. - Filter on the partition column in every query on a gold table.Save it as
.claude/rules/sql.md. An@pathimport inside CLAUDE.md organizes a long file but does not save context: imported files load with it. -
Create a reviewer subagent with a short tool list. Only
nameanddescriptionare required.toolsis the allowlist. A subagent starts with a fresh context and returns only its summary to the main conversation. The words “use proactively” in the description encourage Claude to delegate to it.--- name: sql-reviewer description: Reviews SQL written for this project before it runs. Use proactively for any new or changed query. tools: Read, Grep, Glob model: sonnet --- You review SQL. You never run it. Check join keys and row fan-out, missing filters, SELECT *, implicit casts on filtered columns, and anything that writes. Return numbered findings with file, line, risk and a corrected snippet.Save it as
.claude/agents/sql-reviewer.md. Bash is not in the list on purpose: with Bash, a subagent can still change files. -
Deny the secrets. Add deny rules for the
Readtool in.claude/settings.json. Deny rules are checked before ask and allow rules, and the first match wins.{ "permissions": { "deny": ["Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"] } } -
Add a hook for the rules you cannot leave to chance. A
PreToolUsehook runs before a tool call and receives the call as JSON on stdin. For Bash, the command is intool_input.command. Exit code 2 blocks the call and sends stderr to Claude. Exit code 0 only means “no objection”: the normal permission flow still decides. The script needsjq.Save as
.claude/hooks/block-destructive-sql.shand runchmod +xon it:#!/bin/bash INPUT=$(cat) COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty') if echo "$COMMAND" | grep -Eiq 'drop[[:space:]]+(table|schema|database)|truncate[[:space:]]+table|delete[[:space:]]+from|--env[ =]prod'; then echo "Blocked: destructive SQL or a prod target. Ask the user first." >&2 exit 2 fi exit 0Register it by adding this key next to
permissionsin the samesettings.json:{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-destructive-sql.sh" } ] } ] } } -
Check the setup in a session. Run
/permissionsto see which rules loaded and from which file. Ask the agent to read.envand to drop a table in dev. Both must be refused. If one is not, the rule is not loaded or the pattern does not match: fix that before you trust the setup. -
Keep a short memory in the repository. Put decisions and next steps in a
notes/file or at the end of CLAUDE.md, so a new session continues where the last one stopped. Ask for real output before accepting “done”: the command, then what it printed.
Files to copy
Organise the files from the steps above in this layout:
your-repo/
├── CLAUDE.md
└── .claude/
├── settings.json (deny rules from step 4, hooks key from step 5)
├── rules/sql.md
├── agents/sql-reviewer.md
└── hooks/block-destructive-sql.sh
What was checked by hand: settings.json parses as JSON, the subagent file has name and description, the hook script has valid bash syntax, and the script exits 0 for two harmless commands and 2 for four commands it should block. That proves the files are well formed and the script logic works. It does not prove that Claude Code loads them or applies them.
Real output of the checks and of the hook script fed with sample input, 2026-11-29. Claude Code was not part of this run.
Copy this prompt
Try the configuration on a small repository change. Paste this into Claude Code with the project open, then supply the problem and the test command. The response should give you a plan before any edits, a focused change and the actual test output.
Read CLAUDE.md. Plan first: list the files you will touch and the checks you will run.
Make the smallest change that solves <problem>.
Run <test command> and paste the real output.
If anything fails twice, stop and tell me instead of trying a third patch.
Do not read .env or anything under secrets/.
Check the answer for a plan before any edit, a pasted command with its real output, and a stop after two failures.
Test result
The local hook test allowed a read query and blocked a delete command and a production target. The image shows the commands and exit codes.
Limits
- CLAUDE.md is advice. Anthropic’s documentation says Claude treats CLAUDE.md files and auto memory as context, not enforced configuration. To block an action regardless of what Claude decides, use a hook or a permission rule.
- Long files are followed less. Anthropic recommends staying under about 200 lines per file. Vague instructions are followed less than checkable ones.
.claudeignoredoes nothing. UseReaddeny rules.- Read deny rules have a stated reach. They apply to the built-in file tools and to Bash file commands Claude Code recognizes, such as
cat,head,tail,sedandtee. For Grep and Glob the documentation says Claude makes a best-effort attempt. A path pattern starting with a single/is not an absolute path: use//for the filesystem root. - Bash is the wide door. A Bash command you approve can still write anywhere your user can. For limits at operating-system level, turn on sandboxing. A Bash deny rule matches the command text as written, so it is not a security boundary around a program.
- The sample hook is crude. It matches text. SQL stored in a file and run by a script does not contain the words, so it passes. A hook is only as good as its pattern. It also needs
jq: without it the script finds no command and exits 0, and an exit code other than 0 or 2 lets the action proceed on most events. - A hook cannot override a deny rule. Claude Code evaluates deny and ask rules whatever a hook returns. When several hooks answer, the most restrictive result wins: deny, then defer, ask, allow.
- Subagents cost requests. Each one sends its own requests, which count toward the same usage limits as the main conversation. By default a subagent can spawn subagents up to three layers deep, and with 20 running, a new one fails.
- Untrusted text is data. An issue, an email, a web page or a query result can carry instructions meant for the agent. Anthropic’s security page advises reviewing commands before approval and not piping untrusted content straight to Claude.
- Not covered: skills, plugins, MCP servers, Claude Code on the web, and team-wide managed settings.
Sources
- Create custom subagents, accessed 2026-11-29
- How Claude remembers your project, accessed 2026-11-29
- Configure permissions, accessed 2026-11-29
- Automate actions with hooks, accessed 2026-11-29
- Security, accessed 2026-11-29
Author
Rodrigo Lombana Reina, data engineer. Personal opinions. Checked against the documentation on 2026-11-29.
Questions
What should I put in CLAUDE.md for a data engineering project?
Put facts the agent cannot guess: your layers, naming rules, environments and test command, for example bronze, silver, gold, snake_case names and pytest -q. Anthropic's Claude Code documentation recommends under 200 lines per file, because longer files are followed less well. Concrete lines such as "no SELECT *" work better than "write clean SQL".
Is CLAUDE.md enough to keep Claude Code away from production?
No. Anthropic's documentation says Claude Code treats CLAUDE.md files as context, not enforced configuration. To block an action whatever the agent decides, use a PreToolUse hook or a permission rule. The article's four layers are CLAUDE.md as context, then a read-only subagent, deny rules and a hook, which enforce.
How do I make a Claude Code subagent that can only read?
List its tools in the subagent's front matter, for example tools: Read, Grep, Glob, and leave Bash out. Only name and description are required, and tools works as an allowlist. Anthropic's documentation notes that a subagent with Bash can still change files through the shell, so a short list is what keeps it read-only.
How do I stop Claude Code from reading my .env file?
Add Read deny rules in .claude/settings.json, for example Read(./.env), Read(./.env.*) and Read(./secrets/**). Claude Code checks deny rules before ask and allow rules, and the first match wins. Then run /permissions to see which rules loaded, and ask the agent to read .env: it must be refused.
Does a .claudeignore file hide secrets from Claude Code?
No. The permissions page of the Claude Code documentation says a .claudeignore file has no effect. Move its entries into Read deny rules in .claude/settings.json, such as Read(./.env). A path pattern starting with a single slash is not an absolute path, so use a double slash for the filesystem root.
How can a Claude Code hook block a destructive SQL command?
Register a PreToolUse hook on Bash. It receives the call as JSON on stdin, with the command in tool_input.command, and exit code 2 blocks the call and sends stderr to Claude. Exit code 0 only means no objection. The sample script needs jq and matches text, so SQL stored in a file and run by a script passes.
Can deny rules and hooks fully protect my secrets in Claude Code?
Not completely. Read deny rules apply to the built-in file tools and to Bash file commands Claude Code recognizes, such as cat, head, tail, sed and tee. For Grep and Glob the documentation says Claude makes a best-effort attempt. A Bash command you approve can still write anywhere your user can, so consider sandboxing.
Was this Claude Code setup tested in a Claude Code session?
No. The files were syntax-checked on macOS with jq 1.7.1 and Python 3.14.8, and the hook script exited 0 for two harmless commands and 2 for four it should block. Claude Code 2.1.296 was installed, but no session loaded these files. The documentation pages do not give a version number for these features, so none is claimed.
