We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Compaction

If you've never hit a context window limit using a "raw" AI provider's API, know that it's literally a hard stop. You just get an error saying the input is too long.

To avoid this, OpenCode (like other coding agents) will automatically compact your session as you approach the context window limit. In practice, and assuming you're using a model with a reasonably large window, you'll likely never have a "hard stop" because OpenCode's auto-compaction will keep you in safe territory.

How Compaction Works

Unlike lossless compression, compaction doesn't let you reconstruct every detail from the shortened version of the context. It's lossy: some information is concretely left out.

By default, OpenCode asks the model to summarize the session's history and current state, retaining some recent context alongside the summary. Earlier messages remain saved on disk, but the model continues the session using the shorter context. The summary might contain:

  • A list of relevant files and what matters about them
  • A summary of the goal and what's been tried so far
  • Constraints and decisions that should persist
  • Suggested next steps

Manual vs. Automatic

While compaction happens automatically as you approach the context window limit (you'll notice the TUI telling you that it's compacting), you can also trigger it manually at any time with the /compact command. Include instructions to guide the compaction.

Compaction used to suck, but now it's pretty good at retaining relevant context.

Sometimes I manually /compact after a task is "roughly done," like where I might make a Git commit. Then I continue from there in the same session.

I used to make new sessions all the time, but now I tend to have super long-running sessions with more compaction.