messages list into three parts:
Implement client-side compaction
Set your API key in theMOONSHOT_API_KEY environment variable before running 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, callcompact_history() to update messages:
messages is updated, continue with the next turn:
Frequently asked questions
When should I trigger compaction?
When should I trigger compaction?
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.How should I handle multiple compactions in a long conversation?
How should I handle multiple compactions in a long conversation?
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.