skip to content

A Claude Agent SDK service on a shared host picks up a developer's CLAUDE.md, hooks and allow rules; why, and how do you isolate it?

level: seniorimportance: nice to knowfreq 4%

answer

  1. defaults match the CLI
  2. user, project and local
  3. an empty list opts out
  4. some inputs ignore the option
  5. old Python SDKs ignored []

basics

~20 s

Omitting settingSources loads user, project and local settings, CLAUDE.md, skills and hooks, exactly like the CLI. Pass settingSources: [], then switch off auto memory and claude.ai connectors separately; managed policy and ~/.claude.json are still read.

solid answer

~40 s

`query()` with no `settingSources` behaves like the CLI and loads all three filesystem sources: `user` (`~/.claude/settings.json`, `~/.claude/CLAUDE.md`, rules, skills, commands, subagents), `project` (`<cwd>/.claude/settings.json` and its hooks, `CLAUDE.md` in `cwd` and every parent, project skills and subagents) and `local` (`.claude/settings.local.json`, `CLAUDE.local.md`). On a shared host that means a developer's hooks and allow rules shape your service. `settingSources: []` (Python `setting_sources=[]`) turns all three off, leaving only what you configure in code. It does not cover everything: endpoint-managed policy and server-managed settings still apply, `~/.claude.json` is always read, auto memory loads unless disabled, and claude.ai connectors load with a claude.ai login unless you set `strictMcpConfig: true`. For multi-tenant work, give each tenant its own filesystem, pass `[]`, and set `CLAUDE_CODE_DISABLE_AUTO_MEMORY=1`. Python SDK 0.1.59 and earlier treated `[]` as omitted.

code

typescript · 16 lines
typescript
import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "Summarise the changes on this branch as release-note bullets",
  options: {
    cwd: "/srv/pr-summary/work/pr-482",
    settingSources: [],
    strictMcpConfig: true,
    systemPrompt: { type: "preset", preset: "claude_code" },
    allowedTools: ["Read", "Grep", "Glob"],
    permissionMode: "dontAsk",
    env: { ...process.env, CLAUDE_CODE_DISABLE_AUTO_MEMORY: "1" },
  },
})) {
  if (message.type === "result") console.log(message.subtype);
}

go deeper

for a junior

Recall the three setting sources, that omitting the option loads all of them like the CLI, and that an empty list turns them off.

for a middle

Explain what each source loads and from where, how cwd defines the project source, and why CLAUDE.md is collected from parent directories while project hooks are not.

for a senior

Isolate a service on a shared host: empty sources, auto memory off, strictMcpConfig, a merged env in TypeScript, a pinned cwd, and a Python SDK new enough to honour an empty list.

for a principal

Decide which configuration a fleet of SDK agents should inherit on purpose, such as managed policy and repository conventions, and which must be cut off, and where filesystem isolation has to do what options cannot.

## What setting sources are Claude Code reads configuration from **settings files** and from Markdown files on disk. The Agent SDK groups these into three **setting sources**, chosen with `settingSources` in TypeScript or `setting_sources` in Python: | Source | What it loads | Where from | |---|---|---| | `user` | User `settings.json`, `CLAUDE.md`, rules, skills, commands, subagents | `~/.claude/` | | `project` | Project `settings.json` and its hooks, `CLAUDE.md` and `.claude/rules/`, skills, commands, subagents | `<cwd>/.claude/` for settings and hooks; `<cwd>` and every parent for `CLAUDE.md` and rules | | `local` | `.claude/settings.local.json`, `CLAUDE.local.md` | `<cwd>/.claude/`; `<cwd>` and parents for `CLAUDE.local.md` | **Omitting the option equals `["user", "project", "local"]`**, the same as the CLI. That default is deliberate (a short-lived change at the SDK rename that loaded nothing was reverted), and it is why an SDK service on a developer machine or a shared runner quietly inherits that machine's configuration. `cwd` decides what "project" means. Project `settings.json` and hooks load only from `<cwd>/.claude/`, with no fallback to parent directories, while `CLAUDE.md` files are collected from `cwd` upward. ## How a shared host leaks into the service Consider a pull-request summary service that checks each branch out under a work directory on a build host where engineers also run Claude Code interactively: - A developer's `~/.claude/settings.json` allow rule for `Bash` pre-approves shell commands, so the service's approval callback never sees them. - Their user-level hooks run inside every service session. - A `CLAUDE.md` in a parent directory of the work tree adds instructions nobody on the service team wrote. - A checked-out branch's own `.claude/settings.json` contributes hooks and rules from the code under review. None of this raises an error; the service simply behaves differently on that host. ## Turning the sources off `settingSources: []` disables user, project and local settings. The agent then has only what you pass programmatically: `systemPrompt`, `allowedTools`, `hooks`, `mcpServers`, `agents`. A middle ground such as `["project"]` keeps the repository's conventions and hooks while ignoring personal and local files. In Python, `claude-agent-sdk` **0.1.59 and earlier treated an empty list the same as omitting the option**, so a service pinned to an old release that "isolates" itself with `setting_sources=[]` loads everything. Upgrade before relying on it. ## What the option does not control Several inputs are read regardless of `settingSources`: 1. **Managed policy.** Endpoint-managed settings (an MDM profile, registry policy or managed settings file) load from the host, and server-managed settings are fetched when the session authenticates with a qualifying organisation credential. This is by design: administrators can constrain SDK agents too. 2. **`~/.claude.json`**, the global config, always read; relocate it with `CLAUDE_CONFIG_DIR` in `env`. 3. **Auto memory** under `~/.claude/projects/<project>/memory/`, loaded into the system prompt at session start; disable it with `autoMemoryEnabled: false` in settings or `CLAUDE_CODE_DISABLE_AUTO_MEMORY=1` in `env`. 4. **claude.ai MCP connectors**, loaded when the session uses a claude.ai login. `mcpServers: {}` does not suppress them; `strictMcpConfig: true`, `disableClaudeAiConnectors: true` or `ENABLE_CLAUDEAI_MCP_SERVERS=false` does. 5. **Sandbox credential restrictions** in `~/.claude/settings.json`, which are kept because they only narrow what sandboxed commands can reach. ## An isolation checklist - Run each tenant or job in **its own filesystem**: a container or a dedicated work directory with its own home. - Pass `settingSources: []`, or the explicit subset you intend. - Set `CLAUDE_CODE_DISABLE_AUTO_MEMORY=1`. In TypeScript, `env` **replaces** the subprocess environment, so spread `process.env` first; Python's `env` merges onto the inherited environment. - Set `strictMcpConfig: true` so only the servers you pass in `mcpServers` load. - Pin `cwd` to the work directory, so the project source, if you keep it, means the repository you intend. - Expect organisation-managed settings to apply anyway, and treat them as policy rather than a leak. The docs are explicit that default `query()` options are not a multi-tenant isolation boundary. ## Verifying the result - In TypeScript, register an `InstructionsLoaded` hook callback to log every `CLAUDE.md` or rules file that enters context; an isolated run should log none you did not intend. - Log `PreToolUse` calls and check that no hook or approval you did not write takes part. - Run the same job on a clean host and on the shared host and compare the transcripts; differences point at inherited configuration.

  • The service sets settingSources: [] yet still shows tools from a claude.ai connector. Why?
    claude.ai connectors load whenever the session authenticates with a claude.ai login, independent of setting sources, and `mcpServers: {}` does not suppress them. Set `strictMcpConfig: true`, or `disableClaudeAiConnectors: true` in settings, or `ENABLE_CLAUDEAI_MCP_SERVERS=false` in `env`.
  • Why might CLAUDE_CODE_DISABLE_AUTO_MEMORY in a TypeScript env option break the agent's PATH?
    In TypeScript, `env` replaces the subprocess environment instead of merging with `process.env`, so passing only that variable drops `PATH` and everything else. Spread `process.env` first. Python's `env` merges onto the inherited environment, so the same mistake does not arise there.

  • Leaving settingSources unset gives an agent with no filesystem settings.
  • settingSources: [] also switches off managed policy and auto memory.
  • Passing mcpServers: {} keeps claude.ai connectors out of the session.
  • Project hooks are found in the nearest .claude/ directory above cwd.
  • Python setting_sources=[] has meant no filesystem settings in every release.

Assumes current Agent SDK releases checked against the Claude Code 2.1.286 docs; Python claude-agent-sdk 0.1.59 and earlier treated setting_sources=[] as if the option were omitted.