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 exampleDirect 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 parametersDirect link to Constructor parameters
options?:
exclude?:
filterAfterToolSteps?:
preserveModelOutput?:
ReturnsDirect link to Returns
id:
name:
processLLMRequest:
Step filteringDirect 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 outputDirect 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 exampleDirect link to Extended usage example
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 callsDirect 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(),
],
})