Claude Code

Auto mode 策略参考

Claude Code Auto mode 的启用条件、classifier 配置、trusted infrastructure、allow/soft_deny/hard_deny、classifyAllShell、denial 复盘和缓存影响。

Auto mode 用 classifier 减少日常工具审批,但它不是“完全自动且安全”的许可。它运行在普通 permissions 之后:显式 ask/deny 规则先判断,再由 classifier 判断剩余动作是否超出用户意图、是否触达未知基础设施、是否像被 hostile content 引导。

必须永远禁止的动作放进 permissions.deny,不要只放进 autoMode.hard_denypermissions.deny 是 classifier 之前的硬门。

#可用性速查

条件要求
Claude Code 版本Auto mode 需要较新的 Claude Code,企业 rollout 前固定最低版本
Anthropic API默认可用,仍受账号、模型和组织策略影响
Bedrock / Vertex / Foundry / Claude apps gateway需要设置 CLAUDE_CODE_ENABLE_AUTO_MODE=1 后才进入 Shift+Tab 模式循环
LLM Gateway / Passion8如果通过 ANTHROPIC_BASE_URL 走 Anthropic API 兼容路径,可能不需要 enable 变量,但要按实际 upstream 验证
默认启动defaultMode: "auto" 应放在用户或 managed settings,项目 settings 中的 auto 会被忽略
禁用managed settings 可用 disableAutoMode: "disable" 阻止用户启用

启用云 provider 路径:

~/.claude/settings.json
{
  "env": {
    "CLAUDE_CODE_ENABLE_AUTO_MODE": "1"
  },
  "permissions": {
    "defaultMode": "auto"
  }
}

#Classifier 读哪些配置

来源适合放什么
CLAUDE.md项目约定、禁止 force push、发布流程、生产环境说明
~/.claude/settings.json个人可信服务、个人常用 bucket、默认模式
.claude/settings.local.json个人在某项目的本地例外
Managed settings组织级 source control、internal domains、sensitive targets、hard policy
--settings 或 Agent SDK inline settings一次性自动化任务的边界

Auto mode 不从共享项目 .claude/settings.json 读取 autoMode,避免仓库自己给自己放宽规则。

#Trusted infrastructure

autoMode.environment 是最重要的字段。它告诉 classifier 什么是公司内部和可信目标,也告诉它哪些目标是敏感目标。

{
  "autoMode": {
    "environment": [
      "$defaults",
      "Organization: acme-corp. Primary use: software development and internal automation",
      "Source control: github.example.com/acme-corp and all repos under it",
      "Cloud provider(s): AWS and GCP",
      "Trusted cloud buckets: s3://acme-build-artifacts, gs://acme-ml-datasets",
      "Trusted internal domains: *.corp.example.com, api.internal.example.com",
      "Key internal services: Jenkins at ci.example.com, Artifactory at artifacts.example.com",
      "Internal package registry: npm.corp.example.com and pypi.corp.example.com",
      "Sensitive remote targets: prod Kubernetes namespaces, production databases, prod Redis",
      "Protected IaC scopes: terraform/prod and pulumi production stacks"
    ]
  }
}

这些条目是自然语言,不是正则。写法应该像给新同事解释基础设施边界。

#allow、soft_deny、hard_deny

字段作用例子
allow对 soft block 的例外允许写 staging namespace、允许写临时 scratch bucket
soft_deny需要用户明确意图才能放行安装依赖、访问外网、创建工单、修改 staging 配置
hard_denyclassifier 内无条件阻断外发源码到第三方 review API、直接修改 production
permissions.denyclassifier 前硬阻断读取 .env、删除目录、读取私钥、生产数据库写操作

保留官方默认规则时,每个数组都要包含 "$defaults":

{
  "autoMode": {
    "allow": [
      "$defaults",
      "Writing to s3://acme-scratch/ is allowed. It is an ephemeral bucket with a 7-day lifecycle policy"
    ],
    "soft_deny": [
      "$defaults",
      "Do not run database migrations outside the migrations CLI, even against dev databases"
    ],
    "hard_deny": [
      "$defaults",
      "Never send repository contents to third-party code review APIs"
    ]
  }
}

省略 "$defaults" 表示替换整组默认规则。只有你完整接管默认列表时才这么做。

#所有 shell 都进 classifier

默认情况下,窄的 Bash allow 规则,例如 Bash(npm test),可能先于 classifier 生效。要让每条 Bash/PowerShell 都经过 classifier,打开:

{
  "autoMode": {
    "classifyAllShell": true
  }
}

这会增加 classifier 调用和延迟,但适合企业强管控环境。低风险个人项目可以不打开。

#复查 effective config

命令用途缓存影响
claude auto-mode defaults打印官方默认 environment、allow、soft_deny、hard_deny本地配置检查,不应调用主模型
claude auto-mode config打印合并后的 effective config本地配置检查,不改变 prompt cache
claude auto-mode critique让模型审查规则是否模糊、重复或容易误伤会调用模型,按当前 provider 的 5m/1h TTL 观察
/permissions Recently denied查看最近被 classifier 或权限系统拒绝的动作UI/会话内操作,重试后工具结果会进入上下文

反复被拒的目标通常缺 autoMode.environment。先补清楚目标,再跑 claude auto-mode config 看是否进入 effective config。

#企业 rollout 模板

{
  "env": {
    "CLAUDE_CODE_ENABLE_AUTO_MODE": "1"
  },
  "permissions": {
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Read(./**/*private_key*)",
      "Bash(curl * | sh)",
      "Bash(rm -rf *)",
      "Bash(git push *)",
      "mcp__prod_database__write_*"
    ],
    "disableBypassPermissionsMode": "disable"
  },
  "autoMode": {
    "environment": [
      "$defaults",
      "Source control: github.example.com/acme-corp and all repos under it",
      "Trusted internal domains: *.corp.example.com",
      "Sensitive remote targets: prod Kubernetes namespaces and production databases"
    ],
    "classifyAllShell": true,
    "soft_deny": [
      "$defaults",
      "Installing dependencies from public registries requires explicit user intent"
    ],
    "hard_deny": [
      "$defaults",
      "Never send source code, logs, or customer data to unapproved external services"
    ]
  }
}

#5m / 1h 缓存影响

动作影响
切到 Auto mode模式变化本身不等于 cache miss,但后续工具执行更少停顿,上下文增长更快
修改 autoMode.environment后续 classifier 决策会变化,主会话 prompt cache 仍按内容和 provider 判断
claude auto-mode critique调用模型,短间隔可复用当前 provider 的缓存窗口
classifyAllShell每条 shell 都可能多一次 classifier 判定,但不等于主模型 response 缓存
denial 后重试批准后的工具调用和结果会进入会话,可能刷新后续缓存前缀
Passion8/custom gateway以 gateway 实际透传的 usage/cache 字段为准,不要假设与 claude.ai 完全一致

#官方参考

#相关页面

Support / 支持

Need help? / 需要帮助?

接入、计费与模型异常可邮件联系;服务可用性以状态页为准。For setup, billing, or model issues, email us. Check the status page for uptime.

也可使用右下角微信 / QQ 客服 · WeChat / QQ support is available at the bottom right