createCodeMode()
Added in: @mastra/core@1.38.0
The createCodeMode() function returns a tool and generated instructions that let an agent run multi-tool computations as one TypeScript function. The generated code runs in a workspace sandbox, and each external_* call runs the real tool on the host with validation, request context, and tracing.
For a conceptual overview, see Code mode.
Usage exampleDirect link to Usage example
Create a code mode tool, add its generated instructions to the agent, and register the returned tool with the same id.
import { Agent } from '@mastra/core/agent'
import { createCodeMode, createTool } from '@mastra/core/tools'
import { LocalSandbox } from '@mastra/core/workspace'
import { z } from 'zod'
const getTopProducts = createTool({
id: 'getTopProducts',
description: 'Get top selling products',
inputSchema: z.object({ limit: z.number() }),
outputSchema: z.object({
products: z.array(z.object({ id: z.string(), name: z.string(), totalSales: z.number() })),
}),
execute: async ({ limit }) => fetchTopProducts(limit),
})
const getProductRatings = createTool({
id: 'getProductRatings',
description: 'Get ratings for a product',
inputSchema: z.object({ productId: z.string() }),
outputSchema: z.object({ ratings: z.array(z.object({ score: z.number() })) }),
execute: async ({ productId }) => fetchRatings(productId),
})
const { tool, instructions } = createCodeMode({
tools: { getTopProducts, getProductRatings },
sandbox: new LocalSandbox(),
})
export const shopAgent = new Agent({
name: 'shop-assistant',
instructions: ['You are a helpful shopping assistant.', instructions],
model: 'openai/gpt-5.5',
tools: { execute_typescript: tool },
})
ParametersDirect link to Parameters
config:
tools:
sandbox?:
timeout?:
id?:
transport?:
ReturnsDirect link to Returns
Returns a CodeModeResult object.
tool:
instructions:
CodeModeToolResultDirect link to CodeModeToolResult
The generated tool returns a CodeModeToolResult.
success:
result?:
logs?:
error?:
message:
name?:
line?:
Inspecting instructionsDirect link to Inspecting instructions
Use createCodeModeInstructions() to inspect the exact prompt content generated for a set of tools.
import { createCodeModeInstructions } from '@mastra/core/tools'
console.log(
createCodeModeInstructions({
tools: { getTopProducts, getProductRatings },
}),
)
The instructions include the usage contract and one typed declare function external_<id>(...) line for each configured tool. Tool ids are sanitized into valid TypeScript function names, and conflicting sanitized names throw an error.
createCodeModeTool()Direct link to createCodeModeTool()
Use createCodeModeTool() when you only need the tool and want to manage instructions separately. Most agents should use createCodeMode() so the tool and matching instructions stay together.
import { createCodeModeTool } from '@mastra/core/tools'
import { LocalSandbox } from '@mastra/core/workspace'
export const codeModeTool = createCodeModeTool({
tools: { getTopProducts, getProductRatings },
sandbox: new LocalSandbox(),
})