Sponsored by Manus AI, Manus AI - Turn Ideas Into Actions

如何有效使用 /clear 命令

阅读时间:3 分钟快速教程

使用 Claude Code 的 /clear 命令掌握成本优化的上下文管理。

理解 /clear 命令

/clear 命令是 Claude Code 用于管理上下文和优化令牌使用的最重要实用工具之一。正确使用时,它可以显著降低成本并提高性能。

/clear 命令的作用

/clear 命令从 Claude 的当前上下文中删除对话历史记录,同时保留:

  • 当前文件上下文
  • 项目结构信息
  • 配置设置
  • 活动工具和功能

何时使用 /clear

  • 切换到完全新的任务时
  • 完成主要功能或错误修复后
  • 遇到响应时间慢时
  • 出现令牌使用警告时
  • 开始性能密集型操作之前

成本优化策略

实施这些策略以最大化效率并最小化成本:

策略 1:基于任务的清除

在不同任务之间清除上下文以防止上下文膨胀:

# After finishing feature implementation
claude "Great work on the authentication system!"

# Before starting a new task
claude /clear

# Start fresh with new task
claude "Let's implement the user profile page now"

策略 2:定期清除

为长时间编码会话实施定期清除间隔:

  • 在密集编码会话中每小时清除一次
  • 每进行 3-5 次重要代码更改后清除
  • 在调试会话之前和之后清除

策略 3:战略性总结

在清除之前,要求 Claude 总结关键见解:

# Before clearing, ask for a summary
claude "Summarize what we've learned so far about the authentication bug"

# Save the summary (if needed)
# Then clear
claude /clear

# Reference the summary in new context
claude "Based on our findings about the authentication bug, let's implement a fix"

高级技术

掌握这些高级技术以实现最佳上下文管理:

选择性上下文刷新

清除后仅选择性地重新加载最相关的上下文:

# Clear everything
claude /clear

# Reload only the specific files needed
claude "Please analyze src/auth/validator.js and src/auth/tokens.js"

使用 CLAUDE.md 实现上下文持久化

/clear 与 CLAUDE.md 文件结合使用以进行高效的上下文管理:

  1. 在 CLAUDE.md 中记录重要的项目见解
  2. 频繁使用 /clear 重置对话历史记录
  3. Claude 仍会保留 CLAUDE.md 中的知识
  4. 在工作时使用新见解更新 CLAUDE.md

相关教程

探索这些相关教程以进一步优化您的 Claude Code 工作流程: