Recipes · Chapter 45 of 63
Compact a Long Session
Use /compact in Kiro CLI, understand what automatic compaction keeps and summarises, and tune compaction.excludeMessages before you hit a context limit.
All levels 2 min read last reviewed 2026-09-04
◎ Learning objective
Keep a long Kiro session working by compacting deliberately and tuning what compaction protects.
Compaction summarises the older part of a conversation so a long session keeps working. Kiro does it automatically when the context fills, and the CLI lets you trigger it yourself with /compact.
When to use this
Compact when a session has run long and you want to keep going with the same task. Do not compact to tidy up: it is one-way, and the detail you lose is gone. If you are moving to a different task, a new session is better than a compacted old one.
Steps
- Notice the signs early. Slower turns, the agent forgetting a decision from earlier, or an error about the context limit all point the same way.
- Save anything you will need. Compaction cannot be undone, so export or copy the parts of the transcript that matter.
- Run
/compactin the CLI at a natural break, such as after a task finishes rather than in the middle of one. Manual compaction is a CLI feature; the IDE, Web, and Mobile compact automatically. - Know what survives. Compaction keeps task status, file paths, decisions, and next steps. It summarises tool output, old code snippets, and resolved errors. Anything you need verbatim should be in a file, not in the chat.
- Tune the protection if automatic compaction is cutting too close to the present.
compaction.excludeMessages(default 2) keeps that many recent messages out of the summary, andcompaction.excludeContextWindowPercent(default 2) keeps that share of the window. The more conservative of the two wins. - Trim the context instead when the problem is loaded files rather than conversation length.
/context showreports token usage per file, and/context removedrops a rule. - Start a new session for a new task. A fresh session is cheaper and clearer than a session carrying a summary of unrelated work.
Check it worked
After compaction, ask the agent to restate the current task and the next step. Both should survive, because both are in the “keep” list. Then ask about a specific line of code discussed twenty turns ago. That detail may be gone, and knowing which is which is the point of the exercise.
Common problems
- “Context limit exceeded unexpectedly.” Update first. A compaction loop that caused this error was fixed in IDE v1.0.138. If it persists, compact deliberately, or start a fresh session with only the files you need.
- The agent forgot a decision. Write decisions into a file or a steering file rather than relying on chat history to hold them.
- You lost the transcript you needed. Compaction is one-way. In the CLI,
/chat saveexports a session before you compact it. - Compaction fires too often. The loaded context is too large. Remove context rules, or use a model with a larger window.
- You wanted the conversation back. Rewind forks a CLI conversation at an earlier turn, but it cannot recover history that compaction already summarised. See Checkpoints and rewind.
Related
- Undo with checkpoints and rewind covers the other recovery tools.
- Kiro CLI Guide lists the context and session commands.
- Troubleshooting covers the error messages in full.
Frequently asked questions
What does /compact do in Kiro CLI?
It summarises the older part of the conversation so the session fits back inside the context window. It keeps task status, file paths, decisions, and next steps, and summarises tool output, old code snippets, and resolved errors. It is available in the CLI only.
Why do I get "Context limit exceeded unexpectedly" in Kiro?
The conversation plus its loaded context outgrew the model's window before compaction could recover it. Update to a current version, then either compact deliberately with /compact in the CLI, start a fresh session with only the files you need, or move to a model with a larger context window.
Can I undo a compaction?
No. Compaction is one-way. History before the compaction point is not recoverable. Save anything you need from the transcript before you compact a session you care about.
☰ Chapter summary
- Compaction summarises older history when the context fills; it runs automatically in the IDE, CLI, Web, and Mobile.
- Manual /compact exists in the CLI only.
- It keeps task status, file paths, decisions, and next steps; it summarises tool output, old code snippets, and resolved errors.
- Compaction is one-way: history before the compaction point cannot be recovered.
- compaction.excludeMessages and compaction.excludeContextWindowPercent protect recent turns, and the more conservative of the two wins.
All chapter summaries are collected on the revision page.
Related chapters
- RecipesUndo With Checkpoints and RewindRestore a Kiro checkpoint in the IDE, rewind a CLI conversation into a new session, and know exactly which edits checkpoints do not track.
- Hands-onKiro CLI GuideInstall kiro-cli, drive sessions with slash commands, manage agents and context, and take the agent into scripts and CI.
- ReferenceTroubleshooting & FAQFixes for Kiro's common errors: improperly formed request, context limit exceeded, connection interrupted, sign-in, MCP, credits, and cloud sessions.
- RecipesSet the Reasoning EffortSet Kiro's reasoning effort from low to max, in the IDE effort panel or with /effort and --effort in the CLI, and set defaults in chat.modelDefaults.