Control Context with Compaction

As you work through complex development tasks, your conversation history with Agentforce grows, and so does your token usage. Eventually, you'll approach the context window limit, which can interrupt your workflow or cause Agentforce to lose track of important details from earlier in the conversation.

Compaction helps you manage this challenge. By summarizing your conversation history while preserving essential context, compaction frees up space in the context window so you can continue working without starting over. Think of it as distilling the relevant parts of a conversation while discarding the verbose details.

You can trigger compaction manually by using the Compact option in the task header, located next to the max token indicator. Unlike starting a new task, compaction keeps you in the same workflow. Compaction is ideal when you're deep in a debugging session, brainstorming, or in iterative development and don't want to break your momentum.

When you trigger compaction from the task header, the assistant analyzes your entire conversation and generates a comprehensive summary. This summary captures the key decisions, code changes, technical details, and insights from your session.

Compaction is designed to reduce token usage while maintaining the continuity of your work. The goal is to consume fewer tokens without losing the insights, decisions, and approaches you've established.

There are two approaches to managing context: Auto Compaction and Manual Compaction. Each serves different needs, and understanding when to use each approach helps you work more effectively.

AspectAuto CompactionManual Compaction
ControlLowHigh
EffortNoneUser-driven
PredictabilityMedium–LowHigh
Risk of losing key infoHigherLower
Best forFast, routine workDeep, critical work

Auto Compaction is a safety net built into Agentforce that activates automatically when your conversation approaches the context window limit. It works in the background, summarizing, trimming, or dropping older context to keep Agentforce responsive and prevent hard failures.

This hands-off approach is well suited for everyday development flows and long coding sessions where the context grows gradually. If you prefer not to manage context explicitly, auto compaction handles everything for you with zero effort.

Auto compaction is a great fit for iterative refactors within a small set of files, and situations where you're moving fast and don't need to preserve every detail from conversation history.

Manual compaction puts you in the driver's seat. Instead of letting the system decide what to keep or remove, you explicitly trigger compaction at the moment that makes sense for your workflow. This approach is ideal for complex or long-running tasks, architectural work, and any situation where correctness matters more than speed. By taking control of the compaction process, you make sure that critical decisions, constraints, and APIs remain preserved in the context.

Manual compaction shines in scenarios where preserving context accuracy is critical, for example:

  • Complex or long-running tasks where you've built up significant context over time.
  • Large refactors where Agentforce must remember patterns established earlier.
  • Debugging issues spanning many files where you've explored multiple approaches.
  • Heavy MCP tool usage where tool output can flood the context window.

While auto compaction provides a convenient safety net, manual compaction gives you deliberate control over your context management. Compaction uses your existing prompt cache, making it cost-effective. By compacting manually and earlier, you avoid processing bloated history with every subsequent API call, potentially saving tokens and costs over the course of a long session. Use manual compaction to:

  • Compress proactively before hitting critical capacity rather than waiting for auto compaction to trigger at the edge of your context limit, giving you more room to continue working.

  • Compact at logical moments in your workflow, such as after finishing a debugging exploration. Auto compaction fires at arbitrary token thresholds, which might not align with natural breakpoints in your work.

  • Compact at a meaningful juncture, such as after exploring multiple debugging approaches, which often produces a more coherent and purposeful summary than one generated at a random token limit.

As your tasks grow in complexity, thoughtful context management becomes an essential skill for maintaining accuracy, reducing costs, and staying productive.