# Class ContextFilterPlugin

java.lang.Object
com.google.adk.plugins.BasePlugin
com.google.adk.plugins.ContextFilterPlugin

All Implemented Interfaces:
  `Plugin`

public class ContextFilterPlugin extends BasePlugin
A plugin that filters the LLM request `Content` list to reduce its size, for example to adhere to context window limits.

This plugin can be configured to trim the conversation history based on one or both of the following criteria:

- `numInvocationsToKeep(N)`: Retains only the last `N` model turns and any preceding user turns. If multiple user messages appear consecutively before a model message, all of them are kept as part of that model invocation window.
- `customFilter()`: Applies a custom `UnaryOperator` to filter the list of `Content` objects. If `numInvocationsToKeep` is also specified, the custom filter is applied after the invocation-based trimming occurs.

Function Call Handling: The plugin ensures that if a `FunctionResponse` is included in the filtered list, its corresponding `FunctionCall` is also included. If filtering would otherwise exclude the `FunctionCall`, the window is automatically expanded to include it, preventing orphaned function responses.

If no filtering options are provided, this plugin has no effect. If the `customFilter` throws an exception during execution, filtering is aborted, and the `LlmRequest` is not modified.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `ContextFilterPlugin.Builder`
  Builder for `ContextFilterPlugin`.

- ## Field Summary

  ### Fields inherited from class BasePlugin

  `name`
  Modifier and Type
  Field
  Description
  `protected final String`
  `name`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `ContextFilterPlugin(ContextFilterPlugin.Builder builder)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `io.reactivex.rxjava3.core.Maybe<LlmResponse>`
  `beforeModelCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest)`
  Filters the LLM request context by trimming recent turns and applying any custom filter.
  `static ContextFilterPlugin.Builder`
  `builder()`

  ### Methods inherited from class BasePlugin

  `getName`
  Modifier and Type
  Method
  Description
  `String`
  `getName()`

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface Plugin

  `afterAgentCallback, afterModelCallback, afterRunCallback, afterToolCallback, beforeAgentCallback, beforeRunCallback, beforeToolCallback, close, onEventCallback, onModelErrorCallback, onToolErrorCallback, onUserMessageCallback`
  Modifier and Type
  Method
  Description
  `default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content>`
  `afterAgentCallback(BaseAgent agent, CallbackContext callbackContext)`
  Callback executed after an agent's primary logic has completed.
  `default io.reactivex.rxjava3.core.Maybe<LlmResponse>`
  `afterModelCallback(CallbackContext callbackContext, LlmResponse llmResponse)`
  Callback executed after a response is received from the model.
  `default io.reactivex.rxjava3.core.Completable`
  `afterRunCallback(InvocationContext invocationContext)`
  Callback executed after an ADK runner run has completed.
  `default io.reactivex.rxjava3.core.Maybe<Map<String,Object>>`
  `afterToolCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext, Map<String,Object> result)`
  Callback executed after a tool has been called.
  `default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content>`
  `beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext)`
  Callback executed before an agent's primary logic is invoked.
  `default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content>`
  `beforeRunCallback(InvocationContext invocationContext)`
  Callback executed before the ADK runner runs.
  `default io.reactivex.rxjava3.core.Maybe<Map<String,Object>>`
  `beforeToolCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext)`
  Callback executed before a tool is called.
  `default io.reactivex.rxjava3.core.Completable`
  `close()`
  Method executed when the runner is closed.
  `default io.reactivex.rxjava3.core.Maybe<Event>`
  `onEventCallback(InvocationContext invocationContext, Event event)`
  Callback executed after an event is yielded from runner.
  `default io.reactivex.rxjava3.core.Maybe<LlmResponse>`
  `onModelErrorCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error)`
  Callback executed when a model call encounters an error.
  `default io.reactivex.rxjava3.core.Maybe<Map<String,Object>>`
  `onToolErrorCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext, Throwable error)`
  Callback executed when a tool call encounters an error.
  `default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content>`
  `onUserMessageCallback(InvocationContext invocationContext, com.google.genai.types.Content userMessage)`
  Callback executed when a user message is received before an invocation starts.

- ## Constructor Details

  - ### ContextFilterPlugin

    protected ContextFilterPlugin(ContextFilterPlugin.Builder builder)

- ## Method Details

  - ### builder

    public static ContextFilterPlugin.Builder builder()

  - ### beforeModelCallback

    public io.reactivex.rxjava3.core.Maybe<LlmResponse> beforeModelCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest)
    Filters the LLM request context by trimming recent turns and applying any custom filter.

    If `numInvocationsToKeep` is set, this method retains only the most recent model turns and their preceding user turns. It ensures that function calls and responses remain paired. If a `customFilter` is provided, it is applied to the list after trimming.

    Parameters:
      `callbackContext` - The context of the callback.
      `llmRequest` - The request builder whose contents will be updated in place.
    Returns:
      `Maybe.empty()` as this plugin only modifies the request builder.