Lazy load Tools
Hey everyone,
I’ve been experimenting with Mastra’s Agent setup and ran into something unexpected.
Even when I send a very simple message like "hello", the token usage is extremely high - for example:
"usage": {
"promptTokens": 31184,
"completionTokens": 100,
"totalTokens": 31284
}
The reason seems to be that I have over 40 tools attached to the agent, and each tool’s input/output schema is being sent to the model every time. This adds up to tens of thousands of tokens even before the actual message content.
Here’s roughly how my agent is initialized:
const agent = new Agent({
id: "my-agent",
model: model,
instructions: "..",
tools: {
...toolGroup1,
...toolGroup2,
...toolGroup3,
},
});
My question:
Does Mastra support lazy loading of tools (loading or registering a tool only when it’s needed)?
Or is there a way to avoid sending all tool schemas in every prompt while still keeping them available internally?
Thanks in advance - I’d love to hear how others are handling large toolsets efficiently.1 Reply
Hi @Adnan A. ! You should look into dynamic agents https://mastra.ai/en/docs/agents/dynamic-agents#example-configuration
You can load tools depending on your runtimeContext for example
Dynamic Agents
Dynamically configure your agent's instruction, model, tools, and memory using runtime context.