Your agents can now respond before, during, and after long-running tool calls without blocking the agentic loop.
Background tasks are a new configuration option that allows tools to run asynchronously, letting you stream progress back to the user while the tool works in the background.
Background tasks also support parallel tool calling. When an agent calls multiple background-enabled tools, each one runs concurrently.
Get started
Enable background tasks on your Mastra instance:
Requires @mastra/core@1.29.0 or later, added in PR #12557.
import { Mastra } from "@mastra/core";
export const mastra = new Mastra({
storage: {
//...
},
backgroundTasks: { enabled: true }
});A storage provider is required to persist state, allowing tasks to resume and survive server restarts.
Configure a tool with backgroundTasks: { enabled: true } to run it in the background. The same configuration can also be set at the agent-level.
import { createTool } from "@mastra/core/tools";
import { z } from "zod";
export const researchTool = createTool({
id: "research",
description: "Run a deep research job on a topic",
inputSchema: z.object({ topic: z.string() }),
backgroundTasks: {
enabled: true,
timeoutMs: 600_000
},
execute: async ({ context }) => {
//...
}
});Call your agent with .streamUntilIdle() to keep the stream open and send feedback to the user.
const stream = await agent.streamUntilIdle("Research SpaceX for me", {
memory: { thread: "t1", resource: "u1" }
});
for await (const chunk of stream.fullStream) {
// initial turn, in progress, and continuation turns
}The stream surfaces chunks at each stage of the agentic loop. See the full list of stream chunks for the complete payload shapes.
For more information and full configuration options, see:
