Skip to content

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/token

Claude 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 | bash

Windows PowerShell:

powershell
irm https://claude.ai/install.ps1 | iex

Windows 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 update

Windows 如果安装或运行时报权限错误,请用管理员身份打开 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

  1. 安装 Claude Code 扩展。
  2. 打开项目。
  3. 使用侧边栏 Claude Code 面板,或在集成终端运行:
bash
claude

JetBrains

  1. 在插件市场安装 Claude Code 插件。
  2. 重启 IDE。
  3. 在 IDE 终端运行:
bash
claude

常用入口:

系统快捷键
macOSCmd + Esc
Windows / LinuxCtrl + 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_URLhttps://code.ciyuanapi.xyz
  • 确认 ANTHROPIC_AUTH_TOKEN 是有效的词元 API Key。
  • 确认 API Key 所属分组有可用 Claude 模型。
  • 在纯终端运行 claude 查看完整报错。

长会话变慢

可以使用:

text
/compact

或:

text
/clear

减少上下文体积后继续工作。