第一章 Claude Code
  AI

第一章 Claude Code

 次点击
117 分钟阅读

Claude Code

Claude Code 是 Anthropic 开发的一个强大的AI编程助手工具,它将Claude AI的智能直接集成到开发者的工作流程中,让AI编程变得更加自然和高效。

上面说的有些虚头巴脑,我想要推荐的是Claude Code构建AI Agent的能力,这也是我要写这系列文章的初衷。

Claude Code 是什么?

Claude Code 是一个命令行工具和IDE集成,它让开发者可以:

  • 在终端中直接与Claude AI对话

  • 让AI理解和修改你的代码库

  • 通过自然语言描述来生成、修改和优化代码

  • 获得基于上下文的智能编程建议

我的理解

我们都用过AI Code软件如:Cursor、Trae等,后来Google也发布了一个npm版本的gemini cli,qwen也有这样的一个cli,在后来Claude Code的cli也就是今天说的这个火了,大家都在推荐用Claude Code在生产项目上。

上面介绍的几点Claude Code能做什么,其实其他软件和cli都能做到,大家在IDE编辑器用的最多,你有的别人也有你能做的别人也有替代产品,用于生产的很多workflow或者Landgraf都是主力军。

那为啥还要介绍Claude Code?是因为Claude Code能以最简单的方式让我们构建一个AI Agent的能力。

大家想想我们本地用的IDE、CLI也只是在处理本机事务,IDE用于我们自己写代码,我们在工作上构建的AI Agent基本上都是用一些开发语言的能力或者平台如:MCP、Dify、LandFlow、LandGraf等,都是要写代码或者会工作流,都要写很多提示词,对开发者来讲尤其是我们刚开始入圈的开发者一直在探索很多做AI Agent的构建在工作中,都感觉好难好难,效果也难达到预期,要学的东西太多了。很多时候我们不得以先干起来再说,从而我们把很多工具、技术混着用。

Claude Code出来了以后它简化了我们构建AI Agent的能力,无需去写什么Python代码,画流程图。Claude Code提供了一种以md文档的描述方式来构建AI Agent,并且有Super Agent去调用Sub Agent的泛化能力。另外一个优点是它提供了命令行的执行方式,我们把md文档也就是Agent描述完毕后就等于Agent构建好了,命令行的方式我们可以在任何地方去运行它。所以Claude Code能代替其他的Agent工具,也是我目前推崇此AI工具的原因

主要解决的问题

那Claude Code能解决我们什么问题?我想写很多很多示例,但是我觉得我写不下去,好像我们在IDE或Dify、Landgraf能做的它都能做(当然,你一定是有过一段时间要构建AI Agent的工作才能理解)。当你用Claude Code的时候,你会发现你不用去依赖其他的平台部署或者写一些软件代码,你仅仅通过md的描述语言来极简单的方式实现,这就是已经很能降低成本了,无论是学习还是其他资源成本都降低。

所以我们非常高兴的去学习它,并将它用在我们的生产环境工作,构建生产级的AI Agent。

安装与使用

前提条件

Node.js 18 或更新版本

Claude Code是用node基座建立的,这个对于现在AI圈来讲,已经是通用技术了,安装成本很低。

安装方式

# 安装1 Claude Code
npm install -g @anthropic-ai/claude-code
​
# 安装2 国内推荐通过阿里提供的npm镜像安装:
npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.com
​​
# 查看是否安装完毕
claude -v
1.0.92 (Claude Code)

# 导航到您的项目
cd your-awesome-project
​
# 开始使用 Claude 编程
claude
​
# 我执行这个命令的时候报了这个错
Unable to connect to Anthropic services
Failed to connect to api.anthropic.com: ERR_BAD_REQUEST
​
# 解决方式
对于MAC来讲在你的用户下有一个.claude的文件夹,和一个.claude.json的配置文件
在.claude.json中加一句即可,再执行claude就正常了
{
  ...,
  "hasCompletedOnboarding": true
}
这个问题是中国区无法连接Anthropic的问题。

Hello World

# 安装完毕后我们得到一个cli的界面,如果之前玩过gemini cli的就不会陌生了
/help

# 按帮助看有哪些东西
Claude Code v1.0.92

 Always review Claude's responses, especially when running code. Claude has read access to files in the current directory
  and can run commands and edit files with your permission.

 Usage Modes:
 • REPL: claude (interactive session)
 • Non-interactive: claude -p "question"

 Run claude -h for all command line options

 Common Tasks:
 • Ask questions about your codebase > How does foo.py work?
 • Edit files > Update bar.ts to...
 • Fix errors > cargo build
 • Run commands > /help
 • Run bash commands > !ls

 Interactive Mode Commands:
  /add-dir - Add a new working directory
  /agents - Manage agent configurations
  /bashes - List and manage background bash shells
  /bug - Submit feedback about Claude Code
  /clear - Clear conversation history and free up context
  /compact - Clear conversation history but keep a summary in context. Optional: /compact [instructions for
  summarization]
  /config - Open config panel
  /context - Visualize current context usage as a colored grid
  /cost - Show the total cost and duration of the current session
  /doctor - Diagnose and verify your Claude Code installation and settings
  /exit - Exit the REPL
  /export - Export the current conversation to a file or clipboard
  /help - Show help and available commands
  /hooks - Manage hook configurations for tool events
  /ide - Manage IDE integrations and show status
  /init - Initialize a new CLAUDE.md file with codebase documentation
  /install-github-app - Set up Claude GitHub Actions for a repository
  /login - Switch Anthropic accounts
  /logout - Sign out from your Anthropic account
  /mcp - Manage MCP servers
  /memory - Edit Claude memory files
  /migrate-installer - Migrate from global npm installation to local installation
  /model - Set the AI model for Claude Code
  /output-style - Set the output style directly or from a selection menu
  /output-style:new - Create a custom output style
  /permissions - Manage allow & deny tool permission rules
  /pr-comments - Get comments from a GitHub pull request
  /release-notes - View release notes
  /resume - Resume a conversation
  /review - Review a pull request
  /security-review - Complete a security review of the pending changes on the current branch
  /status - Show Claude Code status including version, model, account, API connectivity, and tool statuses
  /statusline - Set up Claude Code's status line UI
  /terminal-setup - Install Shift+Enter key binding for newlines
  /upgrade - Upgrade to Max for higher rate limits and more Opus
  /vim - Toggle between Vim and Normal editing modes

 Learn more at: https://docs.anthropic.com/s/claude-code


 Press Enter to continue…

交互模式命令介绍

基础操作命令

命令

功能

使用场景

/help

显示帮助和可用命令

查看所有命令说明

/exit

退出REPL

结束当前会话

/clear

清除对话历史并释放上下文

重新开始对话

/compact [instructions]

清除对话历史但保留摘要

保持上下文的同时释放内存

/status

显示Claude Code状态

查看版本、模型、账户、API连接状态

配置管理命令

命令

功能

使用场景

/config

打开配置面板

修改Claude Code设置

/model

设置AI模型

切换不同的Claude模型

/output-style

设置输出样式

自定义输出格式

/output-style:new

创建自定义输出样式

创建个性化的输出格式

/permissions

管理工具权限规则

设置允许/禁止的操作

账户和认证命令

命令

功能

使用场景

/login

切换Anthropic账户

登录不同账户

/logout

退出Anthropic账户

安全退出登录

/upgrade

升级到Max版本

获得更高速率限制和Opus模型

/cost

显示会话总成本和时长

查看API使用费用

项目和代码库管理

命令

功能

使用场景

/add-dir

添加新的工作目录

扩展工作范围到其他目录

/init

初始化新的CLAUDE.md文件

创建代码库文档

/context

可视化当前上下文使用情况

查看上下文使用状态

/memory

编辑Claude记忆文件

管理AI的记忆内容

Agent和集成管理

命令

功能

使用场景

/agents

管理agent配置

配置AI助手行为

/mcp

管理MCP服务器

管理模型上下文协议服务

/hooks

管理工具事件的钩子配置

自定义工具事件处理

/ide

管理IDE集成和显示状态

与IDE编辑器集成

开发和调试工具

命令

功能

使用场景

/bashes

列出和管理后台bash shell

管理多个终端会话

/doctor

诊断和验证安装设置

排查安装和配置问题

/bug

提交Claude Code反馈

报告问题和建议

/terminal-setup

安装Shift+Enter换行键绑定

改善终端使用体验

/vim

在Vim和普通编辑模式间切换

启用Vim编辑模式

GitHub集成命令

命令

功能

使用场景

/install-github-app

为仓库设置Claude GitHub Actions

集成GitHub工作流

/pr-comments

获取GitHub PR评论

查看Pull Request反馈

/review

审查Pull Request

AI辅助代码审查

/security-review

对当前分支的待定更改进行安全审查

安全代码检查

会话管理命令

命令

功能

使用场景

/export

导出当前对话到文件或剪贴板

保存对话记录

/resume

恢复对话

继续之前的对话

/release-notes

查看版本说明

了解新功能和更新

/migrate-installer

从全局npm安装迁移到本地安装

升级安装方式

/statusline

设置Claude Code状态栏UI

配置状态显示

使用建议

  1. 新手入门:从 /help/status 开始,了解基本状态

  2. 项目配置:使用 /init 初始化项目,/add-dir 添加工作目录

  3. 性能优化:定期使用 /compact 清理上下文,用 /context 监控使用情况

  4. 团队协作:利用 /review/security-review 进行代码审查

  5. 故障排除:遇到问题时使用 /doctor 诊断,/bug 反馈问题

大模型设置和初始化项目

# 配置大模型,大模型支持很多种,有些用的是另外一个路由项目配置的,我这边是用Deepseek 3.1,它支持Anthropic的协议
export ANTHROPIC_API_KEY="sk-xxx"
export ANTHROPIC_MODEL="deepseek-v3.1"
export ANTHROPIC_BASE_URL="https://deepseek.xxx.com"


# 执行项目初始化,必须配置好大模型,否则init无法执行
/init 

执行完毕后会得到一个主CLAUDE.md的Agent描述文件,这里就算结束。

总结

Claude Code 代表了AI辅助编程的新一代工具,它不仅能够生成代码,更重要的是能够理解项目的整体架构和业务逻辑。对于开发团队来说,它是一个强大的生产力倍增器。后续我将用Claude Code构建AI Agent的工作,用于生产实践给出我自己的应用案例。

© 本文著作权归作者所有,未经许可不得转载使用。