Operate Claude Code: components, sessions, headless runs
Pick the Claude Code component and session command for a situation, and write the command line for a non-interactive run whose output a script can read.
Scenario
Your team's CLAUDE.md has grown to 520 lines: build commands, a 35-step release procedure, migration conventions for one folder, and a note to "read every file before reviewing a PR". Claude now follows it less reliably, and every session starts heavier.
Meanwhile, the nightly CI job that should summarize failing tests hangs until it times out. Someone wrote claude "summarize the failures", which opens an interactive session that waits for a person.
Both problems come from putting the work in the wrong place.
Explanation
Where knowledge goes. Claude Code has several places for instructions. Each loads at a different moment, so each fits a different kind of content.
| Component | Where | Loads | Use it for |
|---|---|---|---|
| CLAUDE.md | ~/.claude/CLAUDE.md, ./CLAUDE.md |
every session | facts needed every time: build and test commands, conventions |
| Rules | .claude/rules/*.md |
at start, or with paths: only when Claude touches matching files |
topic or folder-specific instructions |
| Skills | .claude/skills/<name>/SKILL.md |
the body only when used | multi-step procedures, checklists, reference material |
| Subagents | .claude/agents/<name>.md |
when a task matches, in their own context window | noisy side work: big searches, reviews |
Three details matter in practice:
- Custom commands are skills now.
.claude/commands/deploy.mdand.claude/skills/deploy/SKILL.mdboth create/deploy. Claude can also load a skill by itself when its description matches; adddisable-model-invocation: trueif only a person should start it (a deploy, say). - Keep CLAUDE.md short. The docs warn that files over 200 lines may reduce adherence. Move procedures to skills and folder-specific rules to path-scoped rules.
- CLAUDE.md is context, not enforcement. To block an action whatever Claude decides, use a hook.
Managing the session.
/clearstarts a new conversation with an empty context. Use it when you switch to unrelated work./resumecan bring the old one back./compact [instructions]summarizes the conversation so far to free space while you continue the same task. Pass focus instructions for what the summary must keep. The project-root CLAUDE.md is re-read after compaction; instructions given only in chat may not survive./resume(orclaude --resume <id>,claude --continue) picks up an earlier conversation.
Running headless. claude -p "<prompt>" runs once without a person and exits: 0 on success, non-zero on failure.
--output-format text(default),json(one object at the end, withresult,session_idand cost), orstream-json(one JSON event per line as the run goes; the docs pair it with--verbose).--json-schema '<schema>'withjsonoutput puts schema-conforming data instructured_output.--allowedTools "Read,Bash(npm test *)"pre-approves tools, since nobody can answer a prompt.--max-turnscaps the run.--resume "$session_id"continues a specific earlier run.--bareskips the discovery of hooks, skills, subagents, plugins, MCP servers, auto memory and CLAUDE.md, so CI gets the same result on every machine. It doesn't use a subscription login: setANTHROPIC_API_KEY(or anapiKeyHelper). The docs recommend it for scripted calls and say it will become the default for-pin a future release.
Worked example
Before: the hanging CI step.
claude "Summarize the failing tests" > summary.txt
No -p, so Claude Code waits for a person. Even if it finished, the output would be terminal text with no fields to check.
After:
npm test 2>&1 | tail -n 200 | claude --bare -p \
"Summarize the failing tests and likely causes" \
--allowedTools "Read" \
--max-turns 5 \
--output-format json > summary.json
jq -r '.result' summary.json
The log arrives on stdin, so Claude needs no Bash permission to run the tests itself. Pre-approving only Read and capping the turns keep the run bounded, and jq reads the result field.
And the 520-line CLAUDE.md: keep the build commands and conventions. Move the release procedure to .claude/skills/release/SKILL.md with disable-model-invocation: true, so it loads only when someone types /release. Move the migration conventions to .claude/rules/migrations.md with paths: ["db/migrations/**"]. Replace "read every file before reviewing" with a review subagent, so the file reads stay out of the main conversation.