Skip to content
stacks

Claude Code project structure, file by file

A Claude Code project structure puts shared instructions in CLAUDE.md, team permissions and hooks in .claude/settings.json, and personal overrides outside git.

Published 7 min read

Start your Claude Code project structure with a root CLAUDE.md for project instructions and .claude/settings.json for shared permissions and hook wiring. Keep personal overrides in the gitignored .claude/settings.local.json.

Add .claude/rules/ when instructions need their own scope, and skills when a repeated task needs a reusable procedure. Your application can keep its existing layout. The agent files explain how to work in it; creating every directory in .claude/ on day one adds maintenance before you have anything to put there.

What files belong in a Claude Code project structure?

CLAUDE.md should give a new session enough context to make its first change correctly. Write down the actual test command, identify the source directories, and state constraints the code cannot explain. An instruction such as "run tests with Node through the package script" is useful when another runtime would appear to work.

Claude Code's /init can inspect the repository and draft a starting CLAUDE.md. Review the result against the commands you run yourself. Generated instructions can describe what exists while missing a team decision, such as why migrations must remain backward compatible. Anthropic documents this starting point in setting up a project CLAUDE.md.

AGENTS.md can hold the shared instructions if you use several coding agents. A CLAUDE.md containing @AGENTS.md imports that file, with Claude-specific notes below it. Claude Code v2.1.277 and later read AGENTS.md directly when the project has no CLAUDE.md; once a CLAUDE.md exists, Claude reads only that by default, so the import is what loads the shared file. See Anthropic's instruction-file guidance and the comparison of AGENTS.md vs CLAUDE.md.

Keep the root instructions short enough to review when the project changes. A command that moved six months ago can send every new session down the same dead end. Put the command beside its purpose, such as "typecheck the workspace," so a maintainer can recognize when the instruction needs updating.

What goes in .claude/settings.json?

.claude/settings.json holds configuration the team shares, including permission rules and hook registrations. .claude/settings.local.json holds one person's project overrides. User-wide preferences belong in ~/.claude/settings.json. These scopes are described in Claude Code's settings reference.

Permission rules control tool use. allow permits a matching action without manual approval, ask requires confirmation, and deny blocks it. Deny rules take precedence over ask and allow rules. Use the permission rule reference when choosing patterns, especially before allowing a whole family of shell commands.

A small allow list is easier to inspect than a blanket Bash permission. This example permits two git inspection commands; it is an excerpt to merge into your settings, rather than a replacement for an existing file:

.claude/settings.json permission excerpt
{
  "permissions": {
    "allow": [
      "Bash(git status)",
      "Bash(git diff)"
    ]
  }
}

Add project commands after checking what their scripts execute. A script called test might launch services or contact a paid API. Its friendly name gives you little information about the permission you are granting. Keep deployment and publication approvals deliberate.

Which hooks are worth adding?

Hooks are useful for behavior that needs to happen at a particular event. Start with checks for mistakes you have actually seen: a destructive command before execution, or lint diagnostics after a file edit. Claude Code's hook reference defines the events and their input and output formats.

PreToolUse is the place for a guard that must reject an action before the tool runs. PostToolUse can report problems in the result, such as a linter finding in a newly written file. A lint hook that runs after an edit cannot prevent that edit. Decide whether each check blocks or advises, and explain that distinction in the repository's harness documentation.

.claude/hooks/ is a useful home for scripts, but putting a file there does not register it. The settings must name the event and the script to run. Test both a rejected input and a similar input that should pass. For example, a destructive-command guard should distinguish a force push from a command that merely mentions one in documentation.

Where do rules, commands and agents go?

Rules hold instructions that apply to a topic or part of the repository. Store Markdown under .claude/rules/; a rule with paths frontmatter can apply to matching files. A frontend rule might describe component tests while an API rule describes error responses. Anthropic's path-specific rules documentation covers the matching syntax.

Skills hold procedures you want to reuse. A project skill lives at .claude/skills/<name>/SKILL.md and can keep supporting files beside it. Give a release procedure its own skill when it has enough steps that people keep pasting it into chat. That keeps the root instruction file focused on project facts.

Commands in .claude/commands/ remain supported, although Claude Code has merged custom commands into skills. Both .claude/commands/check.md and .claude/skills/check/SKILL.md can provide /check. Prefer the skill directory for a new procedure that needs reference material. The skills documentation explains how these forms relate.

Custom subagents live in .claude/agents/. Their files define a specialist's instructions and available tools, and the subagent works in its own context. A reviewer can inspect changes with read-only tools and return findings. Configure that access explicitly using the subagent reference.

These directories solve different maintenance problems. Split a rule when its scope becomes distracting; add a skill when a procedure repeats. You can work with only a root instruction file until either problem appears.

What should be gitignored in .claude?

Commit shared instructions and the configuration needed to reproduce the team's setup. Keep machine-specific settings outside the shared diff. Claude Code's directory reference distinguishes committed project configuration from local state.

A .claude/.gitignore can keep personal settings and nested worktrees out of commits:

.claude/.gitignore
settings.local.json
worktrees/

Keep the directory itself tracked if it holds shared hooks or rules. Ignoring all of .claude/ would hide the scripts your committed settings depend on. Review a new hook as executable project code, including which programs it invokes and what files it can change.

How do I add this structure to an existing repo?

You can build this layout by hand or copy a reviewed setup from another repository. Adapt the commands to the current project before enabling its hooks. A copied formatter hook is useful only if it calls a formatter this project actually has.

stacks (@steamfield/stacks) packages reusable project capabilities as fragments. Its agent-ops fragment supplies shared instructions and agent guardrails. From an existing repository's root, inspect the fragment and add it without launching an agent automatically:

Inspect and add the guardrails
stacks show agent-ops
stacks add agent-ops --no-launch

The scripted demo adds agent-ops to a Go repository with existing instructions and Claude settings. It inspects claude-harness, sees that the TypeScript/Bun fragment does not fit, and leaves it out. The demo ends with files written and conflicts waiting for a merge.

Agent + stacks
Demo

You: Add agent guardrails to this existing Go repo. Keep its project rules.

Agent: I’ll inspect the existing rules and check whether the TypeScript harness fits.

●Bash(cat AGENTS.md)
# Existing Go rules
Keep errors structured.
●Bash(stacks show claude-harness)
Claude Code TypeScript hooks (claude-harness), fragment, origin bundled, trusted
Adds tested guards and helper commands to Claude Code in a TypeScript project that uses Bun, a JavaScript runtime.
Not good at: Projects that do not use Bun, or that use Cursor instead of Claude Code.
Applies to: astro-blog, astro-fullstack, nextjs, react-router, sveltekit, tauri-react, typescript-api, typescript-cli, react-vite, hono-api, typescript-library, typescript-mcp, nuxt, nestjs-api
Output abbreviated

Agent: claude-harness targets Bun TypeScript projects. This is a Go CLI, so I’ll add agent-ops alone.

●Bash(stacks add agent-ops --no-launch)
Added agent-ops
Merge needed
.claude/settings.json: .stacks/incoming/.claude/settings.json
AGENTS.md: .stacks/incoming/AGENTS.md
Brief: .stacks/BRIEF.md
Output abbreviated
●Bash(find .claude -type f | sort)
.claude/.gitignore
.claude/README.md
.claude/hooks/detect-secrets.sh
.claude/hooks/format-changed.sh
.claude/hooks/lint-on-write.sh
.claude/hooks/session-start.sh
.claude/hooks/smoke.sh
.claude/settings.json
Output abbreviated
●Bash(cat .stacks/incoming/AGENTS.md)
# check-demo
## Overview
## Commands
## Architecture map
## Agent harness
## Guardrails
Output abbreviated
●Bash(cat AGENTS.md)
# Existing Go rules
Keep errors structured.

Agent: The hooks are written. My existing AGENTS.md is unchanged; stacks parked its template and Claude settings under .stacks/incoming/. I’ll merge those next, then verify.

Example agent conversation. Playback can be paused.

Adding agent guardrails writes hook files and parks conflicting instructions and settings for merging.

agent-ops writes a CLAUDE.md that imports AGENTS.md. The shared guide starts as a skeleton for the agent to fill from your project; stacks does not analyze your code to write it. Verification checks that the skeleton's fill markers have been replaced.

The two fragments provide the following selected paths when Claude is a target. The claude-harness entries are optional here and require a compatible project:

Selected files from agent-ops and claude-harness
AGENTS.md
CLAUDE.md
.claude/
  .gitignore
  README.md
  settings.json
  hooks/
    _destructive-patterns.sh
    _lib.sh
    block-destructive.sh
    detect-secrets.sh
    format-changed.sh
    lint-on-write.sh
    session-start.sh
    smoke.sh
    _lib.ts                  # claude-harness
    _shell.ts                # claude-harness
    guard-commands.ts        # claude-harness
    guard-generated.ts       # claude-harness
    typecheck-on-write.ts    # claude-harness
  commands/                  # claude-harness
    check.md
    test.md
    feature.md
  agents/                    # claude-harness
    architect.md
    reviewer.md
docs/harness.md              # claude-harness
tests/hooks/                 # claude-harness

agent-ops wires the shell hooks into Claude Code's lifecycle:

Hook Event Behavior
session-start.sh SessionStart Reports branch and working-tree context, with setup warnings.
block-destructive.sh PreToolUse Bash Denies destructive shapes including hard git reset, force push and infrastructure teardown.
detect-secrets.sh PreToolUse Edit or Write Denies credential-shaped strings and .env writes, with exceptions for example, sample and template files.
lint-on-write.sh PostToolUse Edit or Write Returns advisory lint diagnostics; Rust and Java have no per-write lint.
format-changed.sh Stop Formats files recorded as written by the session.

The shared settings start with read-only git allow rules and deny destructive command shapes, sensitive environment-file reads and edits to .stacks/manifest.json. The hooks-smoke verify step exercises denied inputs and near misses and checks hook configuration. Shared shell guards remain under .claude/hooks/ even for other agent targets because those adapters use them; the agent setup guide describes the differences in enforcement.

claude-harness adds generated-file guards for dist/, build/, coverage/ and bun.lock, plus advisory TypeScript checking after writes. Its command guard runs through the shell entry when Bun is available. It requires root-level Vitest and fits only the TypeScript/Bun stacks listed by stacks show claude-harness; it also brings agent-ops as a dependency. The composition concepts explain fragment dependencies.

Important

These guards catch accidents, not adversaries. The shell checks can miss commands hidden by quoting, eval or indirect scripts. stacks add preserves existing files and parks conflicting versions under .stacks/incoming/. It runs no install or verification steps itself. After the demo's stopping point, have the agent read .stacks/BRIEF.md, merge the parked settings and instructions while keeping your project's decisions, and run stacks verify. The existing-repository guide describes that handoff and how later additions retain the project's recorded checks.