贊助商 Sora2Prompt AI, 免費 Sora 2 提示詞生成器

如何有效使用 /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 工作流程: