Use this file to discover all available pages before exploring further.
Mastra is an open source TypeScript framework for building agentic applications. It includes built-in support for Blaxel sandboxes, making it possible to create agents that are backed by remote execution environments (sandboxes) in the cloud.This tutorial explores how you can use Mastra with Blaxel.
The Mastra framework is provider-agnostic and can be used with multiple LLM providers. This tutorial uses Anthropic models, but you can also read about using other models.
In your Mastra project directory, initialize a new project and add the Mastra and Blaxel packages, tsx to run the TypeScript examples, and Node.js types:
In your project directory, create a file named index.ts with the following code.
TypeScript
import { Agent } from '@mastra/core/agent'import { Workspace } from '@mastra/core/workspace'import { BlaxelSandbox } from '@mastra/blaxel'async function main() { const sandboxId = `mastra-blaxel-simple-${Date.now()}` // create sandbox const workspace = new Workspace({ sandbox: new BlaxelSandbox({ id: sandboxId, image: 'blaxel/base-image', memory: 4096, }), }) try { // create agent const agent = new Agent({ id: 'agent', name: 'Agent', instructions: 'You have access to a sandbox environment. You can execute commands, manage files, and inspect processes inside the sandbox.', model: 'anthropic/claude-sonnet-4-6', workspace, }) // give the agent a task const result = await agent.generate( 'Install a complete Go development environment. Once done, return the installed Go version number and Go environment variables.', { maxSteps: 50 } ) console.log(result.text) } finally { // delete the sandbox once complete await workspace.sandbox?.destroy() }}main().catch((error) => { console.error(error) process.exit(1)})
This creates a simple sandbox-backed agent on Blaxel with the Mastra framework. Instructions that you send the agent will be executed in a uniquely named sandbox that is dynamically created and managed in your workspace on Blaxel’s infrastructure.Run the agent:
npx tsx index.ts
It will create and deploy a Blaxel sandbox using the blaxel/base-image image, then investigate the sandbox and install all the tools required for Go development. Once done, it will return a report of its work and automatically delete the sandbox. Here’s a partial example of the output:
Go is installed and ready.- Installed version: `go1.25.9`- Command check: `go version` → `go version go1.25.9 linux/amd64`**Go Env**- `AR='ar'`- `CC='cc'`- `CGO_CFLAGS='-O2 -g'`- `CGO_CPPFLAGS=''`- `CGO_CXXFLAGS='-O2 -g'`- `CGO_ENABLED='1'`- `CGO_FFLAGS='-O2 -g'`...
One of the most popular uses for agents is to generate code. Sandboxes provide isolated execution environments, allowing agents to securely run generated code with no risk of escaping.Update index.ts to build a simple coding agent that accepts tasks from the command line and streams its responses as it works:
TypeScript
import { Agent } from '@mastra/core/agent'import { Workspace } from '@mastra/core/workspace'import { BlaxelSandbox } from '@mastra/blaxel'// read task from command lineconst task = process.argv[2]if (!task) { console.error('Usage: npx tsx index.ts "<task>"') process.exit(1)}async function main() { const sandboxId = `mastra-blaxel-coding-${Date.now()}` // create sandbox const workspace = new Workspace({ sandbox: new BlaxelSandbox({ id: sandboxId, image: 'blaxel/py-app', memory: 4096, }), }) try { // create agent const agent = new Agent({ id: 'coding-agent', name: 'Coding agent', instructions: `You are an expert software engineer with access to a sandbox environment. You can write, run, and debug code in any language. Use the sandbox to execute code, install dependencies, and verify your solutions work correctly before responding. Install whichever packages you need for your task.`, model: 'anthropic/claude-sonnet-4-6', workspace, }) // set task for agent // stream agent output const stream = await agent.stream(task, { maxSteps: 50 }) process.stdout.write('agent > ') for await (const chunk of stream.textStream) { process.stdout.write(chunk) } process.stdout.write('\n' + '--'.repeat(20) + '\n') } finally { // delete sandbox when complete await workspace.sandbox?.destroy() }}main().catch((error) => { console.error(error) process.exit(1)})
This creates a coding agent that accepts requests from the command-line. The coding agent’s uniquely named sandbox is generated from Blaxel’s blaxel/py-app image, which contains a complete Python development environment.Test the agent:
npx tsx index.ts "Write a Python program to calculate the number of vowels in a word. Show the code. Test it and show the results."
The agent will start working on the task in a sandbox, streaming its responses as it works. Here’s a partial example of the output:
agent > Here's the Python program to calculate the number of vowels in a word:def count_vowels(word): vowels = "aeiouAEIOU" count = 0 for letter in word: if letter in vowels: count += 1 return count# Test wordstest_words = ["hello", "Python", "AEIOU", "rhythm", "beautiful", "sky"]...
That’s it! You’re ready to start building and deploying Blaxel sandbox-backed agents with Mastra.