Skip to content

Customize Code Interpreter

Build on the managed code-interpreter template when every sandbox needs the same packages. Installation then happens once during the template build rather than at every start.

mjs
const templateName = process.env.TEMPLATE_NAME ?? "custom-code-interpreter";
const template = Template()
  .fromTemplate("code-interpreter")
  .pipInstall("humanize==4.13.0");

await Template.build(template, templateName);

const sandbox = await Sandbox.create(templateName);
try {
  const execution = await sandbox.runCode(
    "import humanize; humanize.intcomma(1234567)",
  );
  console.log(execution.text);
} finally {
  await sandbox.kill();
}

Use a stable template name in your project and rebuild it when its definition changes. Existing sandboxes continue using the build they started from; new sandboxes use the latest successful build.