---
description: Execute Python and JavaScript code with rich outputs.
title: Use code interpreter
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/sandbox/llms.txt  
> Use this file to discover all available pages before exploring further.

# Use code interpreter

Last updated Aug 24, 2026|Copy as Markdown|[View as Markdown](https://mchen-ai-models-sync.previews.developers.cloudflare.com/sandbox/guides/code-execution/index.md)|[Agent setup](https://mchen-ai-models-sync.previews.developers.cloudflare.com/agent-setup/)

This guide shows you how to execute Python and JavaScript code with rich outputs using the Code Interpreter API.

Coming soon: Sandbox SDK 1.0

This page documents the interpreter on today's stable `@cloudflare/sandbox` package.

In the **1.0 preview** (`@next`), the interpreter is an opt-in extension. Refer to [Code interpreter](https://mchen-ai-models-sync.previews.developers.cloudflare.com/sandbox/1-0-preview/interpreter/) and the [Interpreter API](https://mchen-ai-models-sync.previews.developers.cloudflare.com/sandbox/1-0-preview/api/interpreter/).

## When to use code interpreter

Use the Code Interpreter API for **simple, direct code execution** with minimal setup:

* **Quick code execution** \- Run Python/JS code without environment setup
* **Rich outputs** \- Get charts, tables, images, HTML automatically
* **AI-generated code** \- Execute LLM-generated code with structured results
* **Persistent state** \- Variables preserved between executions in the same context

Use `exec()` for **advanced or custom workflows**:

* **System operations** \- Install packages, manage files, run builds
* **Custom environments** \- Configure specific versions, dependencies
* **Shell commands** \- Git operations, system utilities, complex pipelines
* **Long-running processes** \- Background services, servers

## Create an execution context

Code contexts maintain state between executions:

```js
import { getSandbox } from "@cloudflare/sandbox";

const sandbox = getSandbox(env.Sandbox, "my-sandbox");

// Create a Python context
const pythonContext = await sandbox.createCodeContext({
	language: "python",
});

console.log("Context ID:", pythonContext.id);
console.log("Language:", pythonContext.language);

// Create a JavaScript context
const jsContext = await sandbox.createCodeContext({
	language: "javascript",
});
```

```plaintext
import { getSandbox } from '@cloudflare/sandbox';

const sandbox = getSandbox(env.Sandbox, 'my-sandbox');

// Create a Python context
const pythonContext = await sandbox.createCodeContext({
  language: 'python'
});

console.log('Context ID:', pythonContext.id);
console.log('Language:', pythonContext.language);

// Create a JavaScript context
const jsContext = await sandbox.createCodeContext({
  language: 'javascript'
});
```

## Execute code

### Simple execution

```js
// Create context
const context = await sandbox.createCodeContext({
	language: "python",
});

// Execute code
const result = await sandbox.runCode(
	`
print("Hello from Code Interpreter!")
result = 2 + 2
print(f"2 + 2 = {result}")
`,
	{ context: context.id },
);

console.log("Output:", result.output);
console.log("Success:", result.success);
```

```plaintext
// Create context
const context = await sandbox.createCodeContext({
  language: 'python'
});

// Execute code
const result = await sandbox.runCode(`
print("Hello from Code Interpreter!")
result = 2 + 2
print(f"2 + 2 = {result}")
`, { context: context.id });

console.log('Output:', result.output);
console.log('Success:', result.success);
```

### State within a context

Variables and imports remain available between executions in the same context, as long as the container stays active:

```js
const context = await sandbox.createCodeContext({
	language: "python",
});

// First execution - import and define variables
await sandbox.runCode(
	`
import pandas as pd
import numpy as np

data = [1, 2, 3, 4, 5]
print("Data initialized")
`,
	{ context: context.id },
);

// Second execution - use previously defined variables
const result = await sandbox.runCode(
	`
mean = np.mean(data)
print(f"Mean: {mean}")
`,
	{ context: context.id },
);

console.log(result.output); // "Mean: 3.0"
```

```plaintext
const context = await sandbox.createCodeContext({
  language: 'python'
});

// First execution - import and define variables
await sandbox.runCode(`
import pandas as pd
import numpy as np

data = [1, 2, 3, 4, 5]
print("Data initialized")
`, { context: context.id });

// Second execution - use previously defined variables
const result = await sandbox.runCode(`
mean = np.mean(data)
print(f"Mean: {mean}")
`, { context: context.id });

console.log(result.output); // "Mean: 3.0"
```

Note

Context state is lost if the container restarts due to inactivity. For critical data, store results outside the sandbox or design your code to reinitialize as needed.

## Handle rich outputs

The code interpreter returns multiple output formats:

```js
const result = await sandbox.runCode(
	`
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Simple Chart')
plt.show()
`,
	{ context: context.id },
);

// Check available formats
console.log("Formats:", result.formats); // ['text', 'png']

// Access outputs
if (result.outputs.png) {
	// Return as image
	return new Response(atob(result.outputs.png), {
		headers: { "Content-Type": "image/png" },
	});
}

if (result.outputs.html) {
	// Return as HTML (pandas DataFrames)
	return new Response(result.outputs.html, {
		headers: { "Content-Type": "text/html" },
	});
}

if (result.outputs.json) {
	// Return as JSON
	return Response.json(result.outputs.json);
}
```

```plaintext
const result = await sandbox.runCode(`
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Simple Chart')
plt.show()
`, { context: context.id });

// Check available formats
console.log('Formats:', result.formats);  // ['text', 'png']

// Access outputs
if (result.outputs.png) {
  // Return as image
  return new Response(atob(result.outputs.png), {
    headers: { 'Content-Type': 'image/png' }
  });
}

if (result.outputs.html) {
  // Return as HTML (pandas DataFrames)
  return new Response(result.outputs.html, {
    headers: { 'Content-Type': 'text/html' }
  });
}

if (result.outputs.json) {
  // Return as JSON
  return Response.json(result.outputs.json);
}
```

## Stream execution output

For long-running code, stream output in real-time:

```js
const context = await sandbox.createCodeContext({
	language: "python",
});

const result = await sandbox.runCode(
	`
import time

for i in range(10):
    print(f"Processing item {i+1}/10...")
    time.sleep(0.5)

print("Done!")
`,
	{
		context: context.id,
		stream: true,
		onOutput: (data) => {
			console.log("Output:", data);
		},
		onResult: (result) => {
			console.log("Result:", result);
		},
		onError: (error) => {
			console.error("Error:", error);
		},
	},
);
```

```plaintext
const context = await sandbox.createCodeContext({
  language: 'python'
});

const result = await sandbox.runCode(
  `
import time

for i in range(10):
    print(f"Processing item {i+1}/10...")
    time.sleep(0.5)

print("Done!")
`,
  {
    context: context.id,
    stream: true,
    onOutput: (data) => {
      console.log('Output:', data);
    },
    onResult: (result) => {
      console.log('Result:', result);
    },
    onError: (error) => {
      console.error('Error:', error);
    }
  }
);
```

## Execute AI-generated code

Run LLM-generated code safely in a sandbox:

```js
// 1. Generate code with Claude
const response = await fetch("https://api.anthropic.com/v1/messages", {
	method: "POST",
	headers: {
		"Content-Type": "application/json",
		"x-api-key": env.ANTHROPIC_API_KEY,
		"anthropic-version": "2023-06-01",
	},
	body: JSON.stringify({
		model: "claude-3-5-sonnet-20241022",
		max_tokens: 1024,
		messages: [
			{
				role: "user",
				content: "Write Python code to calculate fibonacci sequence up to 100",
			},
		],
	}),
});

const { content } = await response.json();
const code = content[0].text;

// 2. Execute in sandbox
const context = await sandbox.createCodeContext({ language: "python" });
const result = await sandbox.runCode(code, { context: context.id });

console.log("Generated code:", code);
console.log("Output:", result.output);
console.log("Success:", result.success);
```

```plaintext
// 1. Generate code with Claude
const response = await fetch('https://api.anthropic.com/v1/messages', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'x-api-key': env.ANTHROPIC_API_KEY,
    'anthropic-version': '2023-06-01'
  },
  body: JSON.stringify({
    model: 'claude-3-5-sonnet-20241022',
    max_tokens: 1024,
    messages: [{
      role: 'user',
      content: 'Write Python code to calculate fibonacci sequence up to 100'
    }]
  })
});

const { content } = await response.json();
const code = content[0].text;

// 2. Execute in sandbox
const context = await sandbox.createCodeContext({ language: 'python' });
const result = await sandbox.runCode(code, { context: context.id });

console.log('Generated code:', code);
console.log('Output:', result.output);
console.log('Success:', result.success);
```

## Manage contexts

### List all contexts

```js
const contexts = await sandbox.listCodeContexts();

console.log(`${contexts.length} active contexts:`);

for (const ctx of contexts) {
	console.log(`  ${ctx.id} (${ctx.language})`);
}
```

```plaintext
const contexts = await sandbox.listCodeContexts();

console.log(`${contexts.length} active contexts:`);

for (const ctx of contexts) {
  console.log(`  ${ctx.id} (${ctx.language})`);
}
```

### Delete contexts

```js
// Delete specific context
await sandbox.deleteCodeContext(context.id);
console.log("Context deleted");

// Clean up all contexts
const contexts = await sandbox.listCodeContexts();
for (const ctx of contexts) {
	await sandbox.deleteCodeContext(ctx.id);
}
console.log("All contexts deleted");
```

```plaintext
// Delete specific context
await sandbox.deleteCodeContext(context.id);
console.log('Context deleted');

// Clean up all contexts
const contexts = await sandbox.listCodeContexts();
for (const ctx of contexts) {
  await sandbox.deleteCodeContext(ctx.id);
}
console.log('All contexts deleted');
```

## Best practices

* **Clean up contexts** \- Delete contexts when done to free resources
* **Handle errors** \- Always check `result.success` and `result.error`
* **Stream long operations** \- Use streaming for code that takes >2 seconds
* **Validate AI code** \- Review generated code before execution

## Related resources

* [Code Interpreter API reference](https://mchen-ai-models-sync.previews.developers.cloudflare.com/sandbox/api/interpreter/) \- Complete API documentation
* [AI code executor tutorial](https://mchen-ai-models-sync.previews.developers.cloudflare.com/sandbox/tutorials/ai-code-executor/) \- Build complete AI executor
* [Execute commands guide](https://mchen-ai-models-sync.previews.developers.cloudflare.com/sandbox/guides/execute-commands/) \- Lower-level command execution

Was this helpful?

YesNo

## On this page

[![](https://mchen-ai-models-sync.previews.developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://mchen-ai-models-sync.previews.developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/sandbox/guides/code-execution/#page","headline":"Use code interpreter · Cloudflare Sandbox SDK docs","description":"Execute Python and JavaScript code with rich outputs.","url":"https://developers.cloudflare.com/sandbox/guides/code-execution/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-08-24","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
```
