AIFCC
記事一覧へ
claude-setupclaude-workflowagent-ops

Claude Codeのセッション管理と1Mコンテキスト

Thariq@trq212
27728
Claude Codeユーザーとの最近の会話で、一つのテーマが繰り返し出てくる: 1Mトークンのコンテキストウィンドウは諸刃の剣だ。 Claude Codeがより長く自律的に動作し、タスクをより確実に処理できるようになる一方、セッション管理に気を配らないとコンテキスト汚染への扉を開くことになる。 セッション管理はこれまで以上に重要になっており、多くの質問がある。一つのターミナルで一つのセッションを維持するか、二つか?全てのプロンプトで新鮮に始めるか?compact、rewind、サブエージェントはいつ使うべきか?悪いcompactとは何か? 驚くほど多くの詳細があり、ほとんどのガイダンスは開発者ドキュメントに散らばっている。そこで、私が最も多く聞いた質問に答える包括的なガイドを書くことにした。 ## セッション管理の核心 **セッションとは何か?** Claude Codeのセッションは一つのチャットスレッドに対応する。「セッション」を開始すると、Claudeはその会話の全メッセージにアクセスできる。 セッションには二つの制限がある: - **コンテキストウィンドウ**: 1Mトークン(スペースを使い果たすと「コンテキストが長すぎます」というエラーが出る) - **トークン制限**: 計画された費用制限(Maxプランでは「制限に達しました」というメッセージが出る) ## /usageのアップデート内容 今日の/usageのアップデートで何が変わったか: 1. **セッションごとのトークン使用量の可視化**: どのセッションが最もトークンを消費しているかを確認できる 2. **コンテキストウィンドウのステータス**: 現在のセッションがどれだけのコンテキストウィンドウを使用しているかを確認できる 3. **コスト追跡**: セッションごとのコストの内訳を確認できる ## コンテキスト管理の戦略 **コンテキスト汚染とは何か?** 「コンテキスト汚染」は、無関係または時代遅れの情報がコンテキストウィンドウに蓄積されるときだ。 - 廃棄されたコードまたは計画 - 解決された(しかし記録された)エラーと問題 - 古くなったコンテキスト - 削除されたファイルや機能への参照 **一つのセッション vs 複数のセッション** 一つのセッションを使う場合: - 一つのプロジェクトまたはタスクに取り組んでいる - Claudeが以前のコンテキストを参照する必要がある - 開発フロー状態にある 複数のセッションを使う場合: - 無関係なタスクの間を切り替える - 新しいプロジェクトまたは大きな機能を開始する - 以前の決定に関連するコンテキスト汚染が疑われる ## compactについて **compactはいつ使うか?** セッションのコンテキストが約70%を超えたとき。セッションの終わりではなく、継続中のセッションの途中で使うのが最善だ。 **良いcompactとは何か?** 良いcompactは次のものを保持する: - 現在作業中のタスク - 重要な決定とその理由 - アクティブな問題と障害 - 最近変更したファイル **悪いcompactとは何か?** 悪いcompactはコンテキストをランダムに削除し、Claudeが現在何に取り組んでいるかを見失う。高い特異性(「サインアップフォームの検証バグを修正している」)ではなく、低い特異性(「プロジェクトをコーディングしている」)を持つ要約になる。 ## サブエージェントについて サブエージェントは独立したコンテキストを持つ別のClaude Codeインスタンスだ。メインセッションのコンテキストウィンドウを消費しない。 サブエージェントを使う時: - メインのコーディングセッションに影響を与えずにリサーチをしたい - 隔離されたコンテキストで複数のタスクを並行して実行したい - 特定のタスクに対してクリーンな状態から始めたい ## rewindについて rewindは会話の以前のポイントに戻す。バグを導入したメッセージを特定した場合、または間違った方向に進んでいた場合に使う。 rewindはコンテキストウィンドウのスペースを回復しない。単に以前の会話の状態に戻るだけだ。 ## まとめ Claude Codeでセッションを管理するための主要な原則: 1. コンテキスト汚染を避けるために新しいタスクには新しいセッションを使う 2. セッションの70%で/compactを使う 3. 独立した並行タスクにはサブエージェントを使う 4. 間違った方向に進んでいた場合はrewindを使う 5. /usageで使用量を監視する
原文を表示 / Show original
In my recent calls with Claude Code users, one theme keeps coming up: the 1M token context window is a double-edged sword. It lets Claude Code operate autonomously for longer and handle tasks more reliably, but it also opens the door to context pollution if you're not deliberate about managing your sessions. Session management matters more than ever and there seem to be a lot of questions about it. Do you keep one session open in a terminal, or two? Start fresh with every prompt? When should you use compact, rewind, or subagents? What causes a bad compact? There’s a surprising amount of detail here that can really shape your experience with Claude Code and almost all of it comes from managing your context window. A Quick Primer on Context, Compaction & Context Rot The context window is everything the model can "see" at once when generating its next response. It includes your system prompt, the conversation so far, every tool call and its output, and every file that's been read. Claude Code has a context window of one million tokens. Unfortunately using context has a slight cost, which is often called context rot. Context rot is the observation that model performance degrades as context grows because attention gets spread across more tokens, and older, irrelevant content starts to distract from the current task. For our 1MM context model, we see some level of context rot happen around ~300-400k tokens, but it is highly dependent on the task- not a fast rule. Context windows are a hard cutoff, so when you’re nearing the end of the context window, you will need to summarize the task you’ve been working on into a smaller description and continue the work in a new context window, we call this compaction. You can also trigger compaction yourself. Every Turn Is a Branching Point Say you've just asked Claude to do something and it's finished, you’ve now got some information in your context (tool calls, tool outputs, your instructions) and you have a surprising number of options for what to do next: Continue — send another message in the same session /rewind (esc esc) — jump back to a previous message and try again from there /clear — start a new session, usually with a brief you've distilled from what you just learned Compact — summarize the session so far and keep going on top of the summary Subagents — delegate the next chunk of work to an agent with its own clean context, and only pull its result back in While the most natural is just to continue, the other four options exist to help manage your context. When to Start a New Session The new 1M context windows means that you can now do longer tasks more reliably, for example to have it build a full-stack app from scratch. But just because your model hasn't run out of context, it doesn't mean you shouldn't start a new session. Our general rule of thumb is when you start a new task, you should also start a new session. A grey area is when you may want to do related tasks where some of the context is still necessary, but not all. For example, writing the documentation for a feature you just implemented. While you could start a new session, Claude would have to reread the files that you just implemented, which would be slower and more expensive. Since documentation may not be a highly intelligence sensitive task, the extra context is probably worth the efficiency gain of not having to re-read the relevant files again. Rewinding Instead of Correcting If I had to pick one habit that signals good context management, it’s rewind. In Claude Code, double-tapping Esc(or running /rewind) lets you jump back to any previous message and re-prompt from there. The messages after that point are dropped from the context. Rewind is often the better approach to correction. For example, Claude reads five files, tries an approach, and it doesn't work. Your instinct may be to type "that didn't work, try X instead." but the better move is to rewind to just after the file reads, and re-prompt with what you learned. "Don't use approach A, the foo module doesn't expose that — go straight to B." You can also use “summarize from here” to have Claude summarize its learnings and create a handoff message, kind of like a message to the previous iteration of Claude from its future self that tried something and it didn’t work. Compacting vs. Fresh Sessions Once a session gets long, you have two ways to shed weight: /compact or /clear (and start fresh). They feel similar but behave very differently. Compact asks the model to summarize the conversation so far, then replaces the history with that summary. It's lossy, you're trusting Claude to decide what mattered, but you didn't have to write anything yourself and Claude might be more thorough in including important learnings or files. You can also steer it by passing instructions (/compact focus on the auth refactor, drop the test debugging). With /clear you write down what matters ("we're refactoring the auth middleware, the constraint is X, the files that matter are A and B, we've ruled out approach Y") and start clean. It's more work, but the resulting context is what you decided was relevant. What Causes a Bad Compact? If you run a lot of long running sessions, you might have noticed times in which compacting might be particularly bad. In this case we’ve often found that bad compacts can happen when the model can’t predict the direction your work is going. For example autocompact fires after a long debugging session and summarizes the investigation and your next message is "now fix that other warning we saw in bar.ts." But because the session was focused on debugging, the other warning might have been dropped from the summary. This is particularly difficult, because due to context rot, the model is at its least intelligent point when compacting. With one million context, you have more time to /compact proactively with a description of what you want to do. Subagents & Fresh Context Windows Subagents are a form of context management, useful for when you know in advance that a chunk of work will produce a lot of intermediate output you won't need again. When Claude spawns a subagent via the Agent tool, that subagent gets its own fresh context window. It can do as much work as it needs to, and then synthesize its results so only the final report comes back to the parent. The mental test we use: will I need this tool output again, or just the conclusion? While Claude Code will automatically call subagents, you may want to tell it to explicitly do this. For example, you may want to tell it to: “Spin up a subagent to verify the result of this work based on the following spec file” “Spin off a subagent to read through this other codebase and summarize how it implemented the auth flow, then implement it yourself in the same way” “Spin off a subagent to write the docs on this feature based on my git changes” Summary In summary, when Claude has ended a turn and you’re about to send a new message, you have a decision point. Overtime we expect that Claude will help you handle this itself, but for now this is one of the ways you can guide Claude's output.

AIFCC — AI Fluent CxO Club

読み書きそろばん、AI。経営者が AI を自分で動かせるようになるコミュニティ。

Claude Codeのセッション管理と1Mコンテキスト | AIFCC