返回播客Claude
Claude Code 101
Claude Code 中的上下文管理
is Claw's working memory.
是 Claude 的工作记忆。
Every file it reads, every command it runs, every message you send, it all takes up space in the context window.
它读取的每个文件、运行的每条命令、你发送的每条消息,都会占用 context window 中的空间。
Think of the context window as the amount of space that Claude could hold in his memory.
可以把 context window 理解为 Claude 在记忆中能容纳的空间大小。
Whenever you enter a prompt, Claude reads a file, runs a tool call, gets a tool call result.
每当你输入一个提示词,Claude 读取文件、执行工具调用、获取工具调用结果,
This is added on to the context window.
这些都会叠加到 context window 上。
And since there's only a finite amount you can put in the context window, it becomes extremely important to optimize this as much as possible.
由于 context window 的容量是有限的,尽可能对其进行优化就变得极为重要。
Now, when you approach this limit, the context window is automatically compacted.
当你接近这一上限时,context window 会自动执行 compact。
Compaction will summarize important details and remove the unnecessary tool call results and free up a lot of space in your context window.
Compact 会总结重要细节,移除不必要的工具调用结果,从而在 context window 中释放大量空间。
Do note though that this could potentially lose details in your previous conversation.
不过请注意,这可能会导致之前对话中的一些细节丢失。
You can run the compaction manually as well with the /compact command.
你也可以使用 /compact 命令手动执行 compact。
This will compact everything that you've done up to that point, which could be handy if you want to clear up context space, but also have a memory of what you previously worked on.
这会将你迄今为止所做的一切都进行 compact,如果你想清理 context 空间同时又保留之前工作的记忆,这会非常方便。
If you want to completely start from scratch without memory of what was previously worked on, you can also run /cle and that will remove everything starting from scratch.
如果你想完全从头开始,不保留任何之前工作的记忆,也可以运行 /clear,这将移除所有内容并从头开始。
To check the state of your context, run the /context command.
要检查 context 的状态,请运行 /context 命令。
Here you'll get a big picture of how large your context size is, the different categories that are taking up the most context, and a graphic showing you all of this.
在这里,你将获得 context 大小的整体概览、占用最多 context 的不同类别,以及展示这一切的图表。
A general rule of thumb is when you're working on a specific feature and are going over the context window, but need to continue, then compact.
一般经验法则是:当你在处理某个具体功能时,如果 context window 即将超限但需要继续,就执行 compact。
Keeping the context relevant for this feature is important when continuing development.
在持续开发过程中,保持 context 与该功能相关非常重要。
If you have finished the plan and want to start on a new feature, then clear.
如果你已经完成了计划并想开始新功能,则执行 clear。
You don't want the previous conversation to present bias in anything new that you want to create.
你不希望之前的对话对你想要创建的新内容产生偏差。
For things that you do want Claude to remember in other sessions, put it in the claw.md file.
对于你确实希望 Claude 在其他会话中记住的内容,请将其放入 claude.md 文件中。
That way, it doesn't have to rediscover things from scratch all over again.
这样,它就不必每次都从头重新发现这些内容。
Be specific.
要具体明确。
The irony behind writing a smaller prompt is that it in the long run, it will take up more context.
写更短提示词的讽刺之处在于,从长远来看,它实际上会占用更多 context。
Without being explicit, Claude is forced to look around your codebase more and do its own thinking, which takes up a lot more context.
如果不明确说明,Claude 会被迫在你的代码库中到处查找并自行思考,这会占用更多 context。
window space than if you were just a little bit more clear with a sentence or two.
所占的窗口空间,比你多用一两句话说清楚要多得多。
MCP servers load all of the tools available into context by default.
MCP 服务器默认会将所有可用工具加载到 context 中。
So, if you have a lot of MCP servers for things that are unrelated to the project, it might be worth turning them off.
因此,如果你有很多与项目无关的 MCP 服务器,关掉它们可能是值得的。
You can also try out skills, which works similarly to MCP servers, but doesn't put the entire thing into context, saving you space.
你还可以试试 skills,其工作方式与 MCP 服务器类似,但不会将整个内容加载到 context 中,从而为你节省空间。
Sub agents run in parallel with your main agent but has a complete separate context window.
Sub agents 与主 agent 并行运行,但拥有完全独立的 context window。
So for tasks that require an answer without the journey like where is the authentication endpoints located, you can have the sub agent do the work and return just a summary to your main agent.
因此,对于只需要答案而不需要过程的任务,比如认证端点在哪里,你可以让 sub agent 完成工作,只将摘要返回给主 agent。
Managing context within cloud code is crucial.
在 Claude Code 中管理 context 至关重要。
Use slash compact to summarize long sessions and slashclear to start fresh.
使用 /compact 来总结长会话,使用 /clear 来重新开始。
To use your context window effectively, be specific with what you want.
要有效利用 context window,请明确说明你想要什么。
Check what's using your current context window and use sub agents to delegate tasks you only need the answer for.
检查是什么在占用你当前的 context window,并使用 sub agents 来委托那些你只需要答案的任务。