Appearance
Claude Code
Claude Code 是 Anthropic 的代理式编程工具,可以在终端或 IDE 中阅读项目、修改代码、运行命令、查看 diff,并协助完成测试、重构、文档和代码审查。
本文说明如何将 Claude Code 接入词元 API。
准备工作
使用前请先确认:
- 已安装 macOS、Linux、Windows 或 WSL 环境。
- 推荐使用 Node.js 18+。
- 已在词元 API 控制台创建可用 API Key。
- API Key 所在分组包含 Claude Code 要使用的模型。
控制台入口:
text
https://code.ciyuanapi.xyz令牌管理:
text
https://code.ciyuanapi.xyz/tokenClaude Code API Host:
text
https://code.ciyuanapi.xyz注意:Claude Code 的 API Host 不要填写 /v1。它会自行请求 Anthropic 兼容路径,例如 /v1/messages。
安装 Claude Code
macOS / Linux / WSL:
bash
curl -fsSL https://claude.ai/install.sh | bashWindows PowerShell:
powershell
irm https://claude.ai/install.ps1 | iexWindows CMD:
cmd
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd也可以使用 npm 安装:
bash
npm install -g @anthropic-ai/claude-code安装后检查:
bash
claude --version
claude doctor更新:
bash
claude updateWindows 如果安装或运行时报权限错误,请用管理员身份打开 PowerShell 或 CMD 后重试。
配置词元 API
在终端里设置 Claude Code 使用词元 API 的 Host 和 Key。
macOS / Linux / WSL:
bash
export ANTHROPIC_BASE_URL="https://code.ciyuanapi.xyz"
export ANTHROPIC_AUTH_TOKEN="你的词元 API Key"Windows PowerShell:
powershell
$env:ANTHROPIC_BASE_URL="https://code.ciyuanapi.xyz"
$env:ANTHROPIC_AUTH_TOKEN="你的词元 API Key"Windows CMD:
cmd
set ANTHROPIC_BASE_URL=https://code.ciyuanapi.xyz
set ANTHROPIC_AUTH_TOKEN=你的词元 API Key如果希望长期生效,可以使用系统环境变量或 setx:
cmd
setx ANTHROPIC_BASE_URL "https://code.ciyuanapi.xyz"
setx ANTHROPIC_AUTH_TOKEN "你的词元 API Key"设置完成后重新打开终端,再运行:
bash
claude基础用法
进入项目目录后启动:
bash
cd your-project
claude一次性任务:
bash
claude "fix the build error"
claude -p "explain this function"继续最近一次会话:
bash
claude -c常用提示:
text
what does this project do?
where is the main entry point?
explain the folder structure
find the cause of this bug and propose a fix
review my current changes常用命令
| 命令 | 作用 |
|---|---|
/help | 查看命令列表 |
/clear | 清空当前上下文 |
/model | 切换模型 |
/init | 生成项目 CLAUDE.md |
/review | 审查当前改动 |
/security-review | 安全审查 |
/permissions | 管理权限 |
/memory | 管理记忆文件 |
/compact | 压缩长会话上下文 |
/plan | 进入计划模式 |
/resume | 恢复历史会话 |
/mcp | 管理 MCP 工具 |
/exit | 退出 |
特殊输入:
| 输入 | 作用 |
|---|---|
@文件路径 | 引用文件 |
!命令 | 在会话中运行命令 |
#内容 | 写入记忆 |
/命令 | 执行斜杠命令 |
权限与安全建议
Claude Code 可能会读取文件、修改代码、运行命令或访问网络。建议按任务风险选择权限模式:
| 模式 | 说明 |
|---|---|
default | 默认模式,有风险操作会请求确认 |
acceptEdits | 自动接受编辑,适合小范围、可回滚改动 |
plan | 只读规划,不直接改代码 |
dontAsk | 需要确认时默认拒绝,适合脚本或 CI |
bypassPermissions | 跳过权限确认,风险较高,不建议日常使用 |
建议:
- 陌生项目先使用 Plan 模式。
- 大改动前确认项目已纳入 Git 管理。
- 不要把
.env、密钥、凭据目录加入可读范围。 - 自动接受编辑后一定要检查 diff。
权限配置文件常见位置:
text
~/.claude/settings.json
.claude/settings.json
.claude/settings.local.json示例:
json
{
"model": "sonnet",
"language": "chinese",
"permissions": {
"allow": [
"Read(./src/**)",
"Bash(npm test)"
],
"deny": [
"Read(./.env*)",
"Read(./secrets/**)"
],
"defaultMode": "default"
}
}IDE 使用
VS Code
- 安装 Claude Code 扩展。
- 打开项目。
- 使用侧边栏 Claude Code 面板,或在集成终端运行:
bash
claudeJetBrains
- 在插件市场安装 Claude Code 插件。
- 重启 IDE。
- 在 IDE 终端运行:
bash
claude常用入口:
| 系统 | 快捷键 |
|---|---|
| macOS | Cmd + Esc |
| Windows / Linux | Ctrl + Esc |
如果需要让外部终端连接 IDE,可以在 Claude Code 中运行:
text
/ide推荐工作流
text
Explore -> Plan -> Code -> Review -> Test示例:
text
analyze the project structure
think step by step and propose a plan, do not edit files yet
implement step 1 and show me the diff
run tests and fix failures
review the final changes常见问题
claude 命令找不到
- 检查
claude --version。 - 检查 PATH。
- 运行
claude doctor。 - npm 安装异常时,优先改用官方安装脚本。
PowerShell 禁止执行 claude.ps1
可以改用 CMD、Git Bash,或使用官方 Windows 安装脚本。
Claude Code 不响应或网络错误
- 检查网络和代理。
- 确认
ANTHROPIC_BASE_URL为https://code.ciyuanapi.xyz。 - 确认
ANTHROPIC_AUTH_TOKEN是有效的词元 API Key。 - 确认 API Key 所属分组有可用 Claude 模型。
- 在纯终端运行
claude查看完整报错。
长会话变慢
可以使用:
text
/compact或:
text
/clear减少上下文体积后继续工作。