Skip to main content

ToolCallFilter

The ToolCallFilter is an input processor that filters out tool calls and their results from the prompt sent to the model. This is useful when you want to exclude specific tool interactions from context or remove all tool calls entirely.

Filtering happens in the processLLMRequest hook, which runs after the message list is converted to a model prompt. Changes are transient: they affect only what's sent to the model on that call. Stored messages, memory, and UI history keep their original tool calls and results.

Usage example
Direct link to Usage example

import { ToolCallFilter } from '@mastra/core/processors'

// Exclude all tool calls
const filterAll = new ToolCallFilter()

// Exclude specific tools by name
const filterSpecific = new ToolCallFilter({
exclude: ['searchDatabase', 'sendEmail'],
})

// Enable filtering during agent loops and keep the two most recent tool-producing steps
const filterAfterRecentTools = new ToolCallFilter({
filterAfterToolSteps: 2,
})

// Preserve compact model-facing output for filtered completed tool results
const filterWithCompactToolHistory = new ToolCallFilter({
preserveModelOutput: true,
})

Constructor parameters
Direct link to Constructor parameters

options?:

Options
Configuration options for the tool call filter
Options

exclude?:

string[]
List of specific tool names to exclude. If not provided or undefined, all tool calls are excluded

filterAfterToolSteps?:

number
Enables filtering during agent loops and preserves tool calls and results from this many recent tool-producing steps. If undefined, step filtering is disabled

preserveModelOutput?:

boolean
Preserves compact model-facing output from completed filtered tool results with providerMetadata.mastra.modelOutput. Raw tool args and raw results are removed

Returns
Direct link to Returns

id:

string
Processor identifier set to 'tool-call-filter'

name:

string
Processor display name set to 'ToolCallFilter'

processLLMRequest:

(args: ProcessLLMRequestArgs) => Promise<ProcessLLMRequestResult | undefined>
Filters tool calls and results out of the model prompt before it is sent to the provider. Returns undefined when nothing is filtered. Changes are transient and are not persisted to the message list or memory

Step filtering
Direct link to Step filtering

By default, ToolCallFilter filters tool calls from history but leaves tool calls made during the current agent loop in place. Set filterAfterToolSteps to also filter tool calls produced by the current loop.

filterAfterToolSteps counts tool-producing steps. For example, filterAfterToolSteps: 2 keeps tool calls and results from the two most recent tool-producing steps and filters older tool calls and results. Non-tool text remains in context.

Set filterAfterToolSteps: 0 to filter all previous tool calls and results on each step.

const filter = new ToolCallFilter({
filterAfterToolSteps: 2,
})

Preserve compact model output
Direct link to Preserve compact model output

Set preserveModelOutput: true to retain compact toModelOutput history for tool results that the filter removes. The removed tool call and result are replaced with a single text part in the prompt, so the model still sees the output while the raw tool arguments are dropped.

Tool results without model output that can be represented as text are removed entirely.

const filter = new ToolCallFilter({
preserveModelOutput: true,
})

Combine preserveModelOutput with exclude to preserve compact output only for filtered tools:

const filter = new ToolCallFilter({
exclude: ['searchDatabase'],
preserveModelOutput: true,
})

Extended usage example
Direct link to Extended usage example

src/mastra/agents/filtered-agent.ts
import { Agent } from '@mastra/core/agent'
import { ToolCallFilter } from '@mastra/core/processors'

export const agent = new Agent({
id: 'filtered-agent',
name: 'filtered-agent',
instructions: 'You are a helpful assistant',
model: 'openai/gpt-5.6-sol',
tools: {
searchDatabase,
sendEmail,
getWeather,
},
inputProcessors: [
// Filter out database search tool calls from context
// to reduce token usage while keeping other tool interactions
new ToolCallFilter({
exclude: ['searchDatabase'],
}),
],
})

Filtering all tool calls
Direct link to Filtering all tool calls

import { Agent } from '@mastra/core/agent'
import { ToolCallFilter } from '@mastra/core/processors'

export const agent = new Agent({
id: 'no-tools-context-agent',
name: 'no-tools-context-agent',
instructions: 'You are a helpful assistant',
model: 'openai/gpt-5.6-sol',
tools: {
searchDatabase,
sendEmail,
},
inputProcessors: [
// Remove all tool calls from the message history
// The agent can still use tools, but previous tool interactions
// won't be included in the context
new ToolCallFilter(),
],
})