26 Downloads
26 Downloads
This Plugin - GitHub - Context Cleanup | LMStudio
Optional Compatible Plugins (I recommend the Explicit over the Model Dependent version, regardless either work and the choice is up to your tastes.) GitHub - Persisting Memories Explicit | LMStudio - Persisting Memories Explicit
GitHub - Persisting Memories (Model Dependent) | LMStudio - Persisting Memories (Model Dependent)
Tested on Windows 11 Pro 25H2 โ LM Studio 0.4.24
Standarized save memory regex between the this plugin and the other two persisting memories plugins.
Imported the more advanced acquirelock from my PM plugin into this plugin.
Redid the whole ICID and ConversationFileName scan logic flow. This was done to try and minimize the uptime or creation of a lock file for the relationship file.
Moved ICID and CFN to a shared module-level state to conform to a similar standard as my PME plugin.
Moved save memory regex to config.ts to conform with the style of my other plugins and simplify possible future implementations.
The Context Cleanup plugin gives users real-time control over auto trimming the amount of conversation history that stays tied up during a chat session, without them having to manually prune old turns or wait for the threshold where LM Studio determines the context window is full enough to begin truncation. This plugin accomplishes that by using the prompt preprocessor hook and maintaining the respective conversation file so only relevant information stays loaded โ freeing up portions of the context window that were consumed by irrelevant data in the current turn.
Every conversation you have in LM Studio is stored as a .conversation.json file. As your chats grow, these files accumulate metadata and older turns that keep getting resent to the model on every message โ quietly burning through context window space. This plugin steps between messages (via the prompt preprocessor hook) to clean those files up behind the scenes.
On each trigger it performs several passes of housekeeping:
From the LM Studio Website: Install from the LM Studio Hub and enable the plugin.
From GitHub Source Code: Open PowerShell or terminal, navigate to the root folder of the plugin (where you see the README, package, and manifest). Enter lms dev -i -y. The plugin should now be available in LM Studio.
Make sure Cleanup Context? is enabled alongside enabling the plugin โ cleanup won't run at all if this is off.
Choose your options. The plugin will start working immediately after you send a message. Three turns must exist before the plugin begins cleaning. The OVERRIDES take precedence over the default plugin's workflow.

The table below outlines every available configuration in the control panel and explains what each one does:
| Control Panel Option | Type | Purpose |
|---|---|---|
| Cleanup Context? | Boolean ยท ON / OFF | Master switch that turns all cleanup behavior on or off. The plugin only functions while this is enabled. |
Keep first <N> messages | Numeric, min 1 | Number of oldest user/assistant turns to preserve from the beginning of the conversation. Message 1 will never be an option to remove. |
Keep last <N> messages | Numeric, min 1 | Number of newest user/assistant turns to keep. |
Cleanup after every <N>th message | Numeric, min 1 | Trigger rate of cleanup after the <N>th message from the last cleanup. Cleaning only begins once at least three existing messages are present (the current turn counts toward this). The OVERRIDES below still take precedence. |
Multiple OVERRIDES are allowed to be enabled at the same time:
Cleanup But Keep All Messages + Cleanup Thinking Context Only + Keep All Thinking Context = All messages and thinking kept, no other cleaning occurs.
Cleanup But Keep All Messages + Cleanup Thinking Context Only = All messages kept, only thinking is cleaned up.
Cleanup But Keep All Messages + Keep All Thinking Context = All messages kept, everything other than thinking is cleaned up.
Cleanup Thinking Context Only + Keep All Thinking Context = No cleanups are performed. They cancel each other out. CTCO tries to clean up thinking and save the rest. KATC tries to clean up the rest but keep thinking. Neither allows the other to operate.
The plugin relies on LM Studio's local storage layout and manages a few files/folders automatically as part of its operation:
When used alongside my Persisting Memories Plugin, context cleanup has been barred during an ongoing save memory command. Either finish up the save memory command, type exit save memory, or reinitialize the plugin by timeout (idle 20 seconds) or restart to reset the values stored in memory.
| Path / File | Role in Cleanup |
|---|---|
| USER MESSAGE 1 | The first user's message is used to house metadata from my GitHub - Persisting Memories Explicit plugin. This includes the ICID, memories the user injects, and formatting instructions. You can freely delete the user's message 1 โ there is built-in recovery so nothing will break. This is just for your information; nothing is required of you on this front. |
~/.lmstudio/conversations/ | The default main directory that LM Studio stores current chat sessions in. This is where the plugin reads, cleans up, and maintains conversation files. |
ChatSessionConversationRelationship.json (inside /conversations) | Automatically created and used by this plugin. Tracks the mapping between an Internal Chat ID (ICID) and its matching conversation file so the plugin can reliably and cheaply locate the correct session across restarts or random uncontrollable plugin reinitializations. Hard cap of only 15 of the latest entries to keep the file size small. |
<conversationFile>.lock (temporarily inside /conversations) | A temporary lock file created while cleanup is in progress and removed when finished. This lets this plugin safely run alongside others, such as my Persisting Memories plugin, that may want to modify the conversation at the same time. |
This Plugin - GitHub - Context Cleanup | LMStudio
Optional Compatible Plugins (I recommend the Explicit over the Model Dependent version, regardless either work and the choice is up to your tastes.) GitHub - Persisting Memories Explicit | LMStudio - Persisting Memories Explicit
GitHub - Persisting Memories (Model Dependent) | LMStudio - Persisting Memories (Model Dependent)
Tested on Windows 11 Pro 25H2 โ LM Studio 0.4.24
Standarized save memory regex between the this plugin and the other two persisting memories plugins.
Imported the more advanced acquirelock from my PM plugin into this plugin.
Redid the whole ICID and ConversationFileName scan logic flow. This was done to try and minimize the uptime or creation of a lock file for the relationship file.
Moved ICID and CFN to a shared module-level state to conform to a similar standard as my PME plugin.
Moved save memory regex to config.ts to conform with the style of my other plugins and simplify possible future implementations.
The Context Cleanup plugin gives users real-time control over auto trimming the amount of conversation history that stays tied up during a chat session, without them having to manually prune old turns or wait for the threshold where LM Studio determines the context window is full enough to begin truncation. This plugin accomplishes that by using the prompt preprocessor hook and maintaining the respective conversation file so only relevant information stays loaded โ freeing up portions of the context window that were consumed by irrelevant data in the current turn.
Every conversation you have in LM Studio is stored as a .conversation.json file. As your chats grow, these files accumulate metadata and older turns that keep getting resent to the model on every message โ quietly burning through context window space. This plugin steps between messages (via the prompt preprocessor hook) to clean those files up behind the scenes.
On each trigger it performs several passes of housekeeping:
From the LM Studio Website: Install from the LM Studio Hub and enable the plugin.
From GitHub Source Code: Open PowerShell or terminal, navigate to the root folder of the plugin (where you see the README, package, and manifest). Enter lms dev -i -y. The plugin should now be available in LM Studio.
Make sure Cleanup Context? is enabled alongside enabling the plugin โ cleanup won't run at all if this is off.
Choose your options. The plugin will start working immediately after you send a message. Three turns must exist before the plugin begins cleaning. The OVERRIDES take precedence over the default plugin's workflow.

The table below outlines every available configuration in the control panel and explains what each one does:
| Control Panel Option | Type | Purpose |
|---|---|---|
| Cleanup Context? | Boolean ยท ON / OFF | Master switch that turns all cleanup behavior on or off. The plugin only functions while this is enabled. |
Keep first <N> messages | Numeric, min 1 | Number of oldest user/assistant turns to preserve from the beginning of the conversation. Message 1 will never be an option to remove. |
Keep last <N> messages | Numeric, min 1 | Number of newest user/assistant turns to keep. |
Cleanup after every <N>th message | Numeric, min 1 | Trigger rate of cleanup after the <N>th message from the last cleanup. Cleaning only begins once at least three existing messages are present (the current turn counts toward this). The OVERRIDES below still take precedence. |
Multiple OVERRIDES are allowed to be enabled at the same time:
Cleanup But Keep All Messages + Cleanup Thinking Context Only + Keep All Thinking Context = All messages and thinking kept, no other cleaning occurs.
Cleanup But Keep All Messages + Cleanup Thinking Context Only = All messages kept, only thinking is cleaned up.
Cleanup But Keep All Messages + Keep All Thinking Context = All messages kept, everything other than thinking is cleaned up.
Cleanup Thinking Context Only + Keep All Thinking Context = No cleanups are performed. They cancel each other out. CTCO tries to clean up thinking and save the rest. KATC tries to clean up the rest but keep thinking. Neither allows the other to operate.
The plugin relies on LM Studio's local storage layout and manages a few files/folders automatically as part of its operation:
When used alongside my Persisting Memories Plugin, context cleanup has been barred during an ongoing save memory command. Either finish up the save memory command, type exit save memory, or reinitialize the plugin by timeout (idle 20 seconds) or restart to reset the values stored in memory.
| Path / File | Role in Cleanup |
|---|---|
| USER MESSAGE 1 | The first user's message is used to house metadata from my GitHub - Persisting Memories Explicit plugin. This includes the ICID, memories the user injects, and formatting instructions. You can freely delete the user's message 1 โ there is built-in recovery so nothing will break. This is just for your information; nothing is required of you on this front. |
~/.lmstudio/conversations/ | The default main directory that LM Studio stores current chat sessions in. This is where the plugin reads, cleans up, and maintains conversation files. |
ChatSessionConversationRelationship.json (inside /conversations) | Automatically created and used by this plugin. Tracks the mapping between an Internal Chat ID (ICID) and its matching conversation file so the plugin can reliably and cheaply locate the correct session across restarts or random uncontrollable plugin reinitializations. Hard cap of only 15 of the latest entries to keep the file size small. |
<conversationFile>.lock (temporarily inside /conversations) | A temporary lock file created while cleanup is in progress and removed when finished. This lets this plugin safely run alongside others, such as my Persisting Memories plugin, that may want to modify the conversation at the same time. |
Truncation โ Keeps a configurable number of the oldest and newest user and assistant messages, discarding everything in between that is no longer needed for an ongoing response. If you wish not to lose an assistant response out of context, you have three options: widen the range of kept messages, override cleanup but keep all messages, or use my GitHub - Persisting Memories Explicit to save an assistant's response into a memory seed. You can then inject that back into the conversation for the assistant to reference. You will not visually see the memory seed injected into chat unless you specifically ask the assistant to read the memory back out to you, which would technically waste tokens by duplicating information already present in context.
Thinking cleanup โ Removes previously generated thinking steps from assistant messages so reasoning tokens aren't re-sent forever. The last assistant thinking is excluded and preserved.
Field cleaning โ Blanks or strips leftover Jinja templates, prediction system prompts, past prediction tools, and preprocessed content blocks that are irrelevant after a turn has finished.
Metadata preservation โ Carries over critical information such as the Internal Chat ID (ICID), any memory seeds from the companion plugin GitHub - Persisting Memories Explicit, and Formatting Instructions โ including the message number append from the Model Dependent version of PM. This works correctly across cleanup cycles.
In addition to active cleaning, the plugin can optionally maintain a backup of your conversation before cleanup if enabled.
There are additional setting overrides that some users may find useful.
| OVERRIDE: Cleanup But Keep All Messages | Boolean ยท ON / OFF | When enabled, internal cleaning still runs but no turns are truncated โ everything is retained in context. This overrides the keep-oldest / keep-newest counts system. |
| OVERRIDE: Cleanup Thinking Context Only | Boolean ยท ON / OFF | Restricts cleanup to thinking tokens only; all other default cleanups (system prompt, Jinja templates, tools, preprocessed content) are ignored. |
| OVERRIDE: Keep All Thinking Context | Boolean ยท ON / OFF | Disables thinking cleanup. All thinking will remain. Other cleanups still apply. |
| EXTRA: Maintain an Uncleaned Backup | Boolean ยท ON / OFF | Keeps a backup copy of the conversation in .lmstudio\conversations-backup. The first time it is enabled, an exact 1-to-1 copy is saved; afterward only the latest user + assistant turns are appended to upkeep the current state. |
~/.lmstudio/conversations-backup/ (only if createBackup is enabled) | Holds uncleaned backup copies of your conversation. The first time it's enabled, an exact copy is saved; subsequent runs append only the latest uncleaned user/assistant turns to update the current state without constantly overwriting with a cleaned conversation and losing older chats. Old backup files whose names are reused for a new conversation will be renamed with the current time's UNIX timestamp as a suffix. The most recent conversation has priority over any duplicated base file name. |
<conversationFile>.persisting-memories-final-action.ready (temporarily inside /conversations) | Created by my Persisting Memories plugin to coordinate with the Context Cleanup plugin, allowing Persisting Memories to execute its actions first. PM creates the .ready file; CC does the cleanup. |
Truncation โ Keeps a configurable number of the oldest and newest user and assistant messages, discarding everything in between that is no longer needed for an ongoing response. If you wish not to lose an assistant response out of context, you have three options: widen the range of kept messages, override cleanup but keep all messages, or use my GitHub - Persisting Memories Explicit to save an assistant's response into a memory seed. You can then inject that back into the conversation for the assistant to reference. You will not visually see the memory seed injected into chat unless you specifically ask the assistant to read the memory back out to you, which would technically waste tokens by duplicating information already present in context.
Thinking cleanup โ Removes previously generated thinking steps from assistant messages so reasoning tokens aren't re-sent forever. The last assistant thinking is excluded and preserved.
Field cleaning โ Blanks or strips leftover Jinja templates, prediction system prompts, past prediction tools, and preprocessed content blocks that are irrelevant after a turn has finished.
Metadata preservation โ Carries over critical information such as the Internal Chat ID (ICID), any memory seeds from the companion plugin GitHub - Persisting Memories Explicit, and Formatting Instructions โ including the message number append from the Model Dependent version of PM. This works correctly across cleanup cycles.
In addition to active cleaning, the plugin can optionally maintain a backup of your conversation before cleanup if enabled.
There are additional setting overrides that some users may find useful.
| OVERRIDE: Cleanup But Keep All Messages | Boolean ยท ON / OFF | When enabled, internal cleaning still runs but no turns are truncated โ everything is retained in context. This overrides the keep-oldest / keep-newest counts system. |
| OVERRIDE: Cleanup Thinking Context Only | Boolean ยท ON / OFF | Restricts cleanup to thinking tokens only; all other default cleanups (system prompt, Jinja templates, tools, preprocessed content) are ignored. |
| OVERRIDE: Keep All Thinking Context | Boolean ยท ON / OFF | Disables thinking cleanup. All thinking will remain. Other cleanups still apply. |
| EXTRA: Maintain an Uncleaned Backup | Boolean ยท ON / OFF | Keeps a backup copy of the conversation in .lmstudio\conversations-backup. The first time it is enabled, an exact 1-to-1 copy is saved; afterward only the latest user + assistant turns are appended to upkeep the current state. |
~/.lmstudio/conversations-backup/ (only if createBackup is enabled) | Holds uncleaned backup copies of your conversation. The first time it's enabled, an exact copy is saved; subsequent runs append only the latest uncleaned user/assistant turns to update the current state without constantly overwriting with a cleaned conversation and losing older chats. Old backup files whose names are reused for a new conversation will be renamed with the current time's UNIX timestamp as a suffix. The most recent conversation has priority over any duplicated base file name. |
<conversationFile>.persisting-memories-final-action.ready (temporarily inside /conversations) | Created by my Persisting Memories plugin to coordinate with the Context Cleanup plugin, allowing Persisting Memories to execute its actions first. PM creates the .ready file; CC does the cleanup. |