配置指南
配置文件
Claude Code 使用多个配置文件来定制你的开发体验:
- 全局配置:
~/.claude/config.json - 项目配置:
./claude.config.json或./CLAUDE.md - 环境变 量:用于 API 密钥等敏感信息
全局配置
基本设置
{
"apiKey": "your-api-key",
"model": "claude-3-5-sonnet-20241022",
"outputStyle": "concise",
"planMode": "auto",
"maxTokens": 8192
}
配置选项详解
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
apiKey | string | - | Anthropic API 密钥 |
model | string | claude-3-5-sonnet-20241022 | 使用的 Claude 模型 |
outputStyle | string | balanced | 输出风格:concise、balanced、verbose |
planMode | string | manual | 计划模式:auto、manual、off |
maxTokens | number | 4096 | 最大 token 数 |
temperature | number | 0.3 | 创造性程度 (0-1) |
项目配置
CLAUDE.md 文件
在项目根目录创建 CLAUDE.md 文件来配置项目特定设置:
# 项目:我的 Web 应用
## 项目描述
这是一个使用 React + TypeScript 的现代 Web 应用。
## 编码规范
- 使用 ESLint + Prettier
- 遵循 Airbnb 代码风格
- 组件使用函数式组件 + Hooks
- 优先使用 TypeScript
## 文件结构
- `src/components/` - React 组件
- `src/hooks/` - 自定义 Hooks
- `src/utils/` - 工具函数
- `src/types/` - TypeScript 类型定义
## 注意事项
- 所有 API 调用需要错误处理
- 组件必须包含 PropTypes 或 TypeScript 类型
- 新功能需要对应的单元测试
claude.config.json
或者使用 JSON 格式的项目配置:
{
"project": {
"name": "我的 Web 应用",
"type": "react-typescript",
"framework": "vite"
},
"codeStyle": {
"indentSize": 2,
"quotes": "single",
"semicolons": true,
"trailingComma": "es5"
},
"testing": {
"framework": "jest",
"coverageThreshold": 80
},
"hooks": {
"beforeCommit": ["npm test", "npm run lint"]
}
}
环境变量
必需的环境变量
# Anthropic API 密钥
export ANTHROPIC_API_KEY=your_api_key_here
# 可选:指定模型
export CLAUDE_MODEL=claude-3-5-sonnet-20241022
# 可选:自定义配置文件路径
export CLAUDE_CONFIG_PATH=/path/to/config.json
.env 文件支持
在项目根目录创建 .env 文件:
ANTHROPIC_API_KEY=your_api_key_here
CLAUDE_OUTPUT_STYLE=concise
CLAUDE_PLAN_MODE=auto
CLAUDE_MAX_TOKENS=8192
高级配置
自定义 Agents
{
"agents": {
"codeReviewer": {
"prompt": "作为代码审查专家,专注于代码质量、性能和安全性",
"model": "claude-3-5-sonnet-20241022",
"temperature": 0.1
},
"documentationWriter": {
"prompt": "专门编写清晰、全面的技术文档",
"model": "claude-3-haiku-20240307",
"temperature": 0.5
}
}
}
输出样式
{
"outputStyles": {
"development": {
"verbosity": "detailed",
"includeReasoning": true,
"showProgress": true
},
"production": {
"verbosity": "minimal",
"includeReasoning": false,
"showProgress": false
}
}
}
Hooks 配置
{
"hooks": {
"beforeEdit": "npm run format",
"afterEdit": "npm run lint",
"beforeCommit": ["npm test", "npm run build"],
"onError": "echo '错误发生,请检查日志'"
}
}
企业配置
Bedrock 集成
{
"provider": "bedrock",
"bedrock": {
"region": "us-east-1",
"modelId": "anthropic.claude-3-sonnet-20240229-v1:0",
"credentials": {
"accessKeyId": "your-access-key",
"secretAccessKey": "your-secret-key"
}
}
}
Vertex AI 集成
{
"provider": "vertex",
"vertex": {
"projectId": "your-gcp-project",
"location": "us-central1",
"modelName": "claude-3-sonnet@20240229",
"credentials": "/path/to/service-account.json"
}
}
故障排除
配置验 证
验证你的配置:
claude config validate
查看当前配置
claude config show
重置配置
claude config reset
配置模板
Web 开发模板
{
"project": { "type": "web", "framework": "react" },
"codeStyle": { "prettier": true, "eslint": true },
"testing": { "framework": "jest", "coverage": true },
"deployment": { "platform": "vercel" }
}
后端开发模板
{
"project": { "type": "backend", "framework": "express" },
"database": { "type": "postgresql", "orm": "prisma" },
"testing": { "framework": "mocha", "integration": true },
"deployment": { "platform": "heroku" }
}
最佳实践
- 版本控制配置:将
claude.config.json纳入版本控制 - 环境分离:为不同环境使用不同配置
- 团队标准化:团队成员使用相同的项目配置
- 定期更新:随着项目发展更新配置
继续到 核心机制 来深入了解 Claude Code 的工作原理。