# ISessionTrimmingService

## Overview

The [ISessionTrimmingService](/ai/components/api/services/isessiontrimmingservice.md) is invoked by the `SmartSession` when the total number of tokens in the messages being submitted to the AI exceeds the limit set by the `SmartEndpoint.ContextWindow` size. This service is responsible for trimming the history by removing or consolidating messages, thus enabling the session to continue interacting with the AI without significantly losing context.

Wisej.AI proactively invokes this service before the AI provider returns an error. It achieves this by comparing the total number of tokens in the last response, through the [Usage](/ai/concepts/usage-metrics.md) property of the last messgae, message with the [`SmartEndpoint.ContextWindow`](/ai/components/api/smartendpoint.md#contextwindow) property of the current endpoint.

## Default Implementation

The default implementation of the [`ISessionTrimmingService`](/ai/components/api/services/isessiontrimmingservice.md) interface is provided by the [`DefaultSessionTrimmingService`](/ai/components/api/services/wisej.ai.services.defaultsessiontrimmingservice.md) class. This class employs two distinct strategies to manage session data: the RollingWindow strategy and the Summarization strategy.

When trimming is initiated, Wisej.AI calls the `TrimAsync()` method of the `ISessionTrimmingService`. This method receives two parameters: the `SmartSession` that requested the trimming and the collection of messages to be trimmed. By default, the service is set up to employ the [`RollingWindow`](/ai/components/api/smartsession/wisej.ai.smartsession.trimmingstrategy.md) trimming strategy, aiming to reduce the message collection by 50%.

{% hint style="success" %}
When using the `RollingWindow` trimming strategy, the `DefaultSessionTrimmingService` prioritizes the removal of tool calls and tool results messages first.
{% endhint %}

Below is an example of how to modify the default parameters for the `DefaultSessionTrimmingService`. Since the default service is registered with a `Shared` lifetime, you can adjust the properties on the service singleton:

{% tabs %}
{% tab title="C#" %}

```csharp
var service = Application.Services.GetService<ISessionTrimmingService>();

service.TrimmingPercentage = 0.3f;
service.TrimmingStrategy = SmartSession.TrimmingStrategy.Summarization;
```

{% endtab %}

{% tab title="VB.NET" %}

```vbnet
Dim service = Application.Services.GetService(Of ISessionTrimmingService)()

service.TrimmingPercentage = 0.3F
service.TrimmingStrategy = SmartSession.TrimmingStrategy.Summarization
```

{% endtab %}
{% endtabs %}

If you wish to implement a different trimming system, you can register your own service implementation at any time. This allows you to utilize other AI libraries and adopt any strategy that suits your needs. It's crucial to trim the message collection rather than the messages associated with the session. This is because Wisej.AI internally clones the messages before using the AI model, enabling parallel usage of the session.

See [Context Overflow](/ai/concepts/usage-metrics.md#context-overflow) for a description of the two trimming strategies.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/ai/components/built-in-services/isessiontrimmingservice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
