Skip to main content
As a conversation progresses, the message history sent with each request grows longer, consuming more tokens and eventually exceeding the model’s context window. Client-side compaction splits the messages list into three parts:

Implement client-side compaction

Set your API key in the MOONSHOT_API_KEY environment variable before running the code:
The table below summarizes each part of the code: The prompt follows Kimi Code’s compaction instruction, adjusted for the way this example passes history as JSON in a separate request.

Call the compaction function and continue the conversation

After a turn finishes, call compact_history() to update messages:
Once messages is updated, continue with the next turn:

Frequently asked questions

After a turn finishes, check the current context length via usage.total_tokens in the response (or by calling the Token Estimate endpoint before sending a request), and call compact_history() once it exceeds your threshold.Avoid compacting on every turn: generating a summary requires an extra API call, and replacing earlier turns changes the messages prefix, which breaks prefix matching for context caching.
Pass a distinct snapshot_path on each compact_history() call (for example, by including a timestamp or turn number in the filename). This prevents errors caused by an existing backup file and lets you inspect the pre-compaction history if the summary omits an earlier detail.Because the summary from the previous compaction is stored as a regular user message, the next compaction automatically includes it in the earlier turns and merges it with newer turns into an updated summary.