---
title: "Code Interpreter"
url: "/docs/guides/code_interpreter"
canonical_url: "https://docs.litellm.ai/docs/guides/code_interpreter"
type: "docs"
last_updated: "2026-10-08"
summary: "Use OpenAI's Code Interpreter tool to execute Python code in a secure, sandboxed environment."
related:
  - "/docs/completion/computer_use"
  - "/docs/sandbox"
---
# Code Interpreter

> Index of all LiteLLM docs: https://docs.litellm.ai/llms.txt


Use OpenAI's Code Interpreter tool to execute Python code in a secure, sandboxed environment.

| Feature | Supported |
|---------|-----------|
| LiteLLM Python SDK | ✅ |
| LiteLLM AI Gateway | ✅ |
| Supported Providers | `openai` |

:::tip[Route code interpreter to your own sandbox]

The proxy can intercept `code_interpreter` on `/v1/responses` and run the code in a configured sandbox (e2b today) instead of OpenAI's container, with no change to the client request. The response shape stays identical (`code_interpreter_call` next to `message`). See [Code Interpreter Sandbox Interception](/docs/sandbox#code-interpreter-interceptor).

:::

## LiteLLM AI Gateway

### API (OpenAI SDK)

Use the OpenAI SDK pointed at your LiteLLM Gateway:

```python showLineNumbers title="code_interpreter_gateway.py"
from openai import OpenAI

client = OpenAI(
    api_key="sk-<your-litellm-api-key>",  # Your LiteLLM API key
    base_url="http://localhost:4000"
)

response = client.responses.create(
    model="openai/gpt-5.6-terra",
    tools=[{"type": "code_interpreter"}],
    input="Calculate the first 20 fibonacci numbers and plot them"
)

print(response)
```

#### Streaming

```python showLineNumbers title="code_interpreter_streaming.py"
from openai import OpenAI

client = OpenAI(
    api_key="sk-<your-litellm-api-key>",
    base_url="http://localhost:4000"
)

stream = client.responses.create(
    model="openai/gpt-5.6-terra",
    tools=[{"type": "code_interpreter"}],
    input="Generate sample sales data CSV and create a visualization",
    stream=True
)

for event in stream:
    print(event)
```

#### Get Generated File Content

```python showLineNumbers title="get_file_content_gateway.py"
from openai import OpenAI

client = OpenAI(
    api_key="sk-<your-litellm-api-key>",
    base_url="http://localhost:4000"
)

# 1. Run code interpreter
response = client.responses.create(
    model="openai/gpt-5.6-terra",
    tools=[{"type": "code_interpreter"}],
    input="Create a scatter plot and save as PNG"
)

# 2. Get container_id from response
container_id = response.output[0].container_id

# 3. List files
files = client.containers.files.list(container_id=container_id)

# 4. Download file content
for file in files.data:
    content = client.containers.files.content(
        container_id=container_id,
        file_id=file.id
    )
    
    with open(file.filename, "wb") as f:
        f.write(content.read())
    print(f"Downloaded: {file.filename}")
```

### AI Gateway UI

The LiteLLM Admin UI includes built-in Code Interpreter support.

**Steps:**

1. Go to **Playground** in the LiteLLM UI
2. Select an **OpenAI model** (e.g., `openai/gpt-5.6-terra`)
3. Select `/v1/responses` as the endpoint under **Endpoint Type**
4. Toggle **Code Interpreter** in the left panel
5. Send a prompt requesting code execution or file generation

The UI will display:
- Executed Python code (collapsible)
- Generated images inline
- Download links for files (CSVs, etc.)

## LiteLLM Python SDK

### Run Code Interpreter

```python showLineNumbers title="code_interpreter.py"
import litellm

response = litellm.responses(
    model="openai/gpt-5.6-terra",
    input="Generate a bar chart of quarterly sales and save as PNG",
    tools=[{"type": "code_interpreter"}]
)

print(response)
```

### Get Generated File Content

After Code Interpreter runs, retrieve the generated files:

```python showLineNumbers title="get_file_content.py"
import litellm

# 1. Run code interpreter
response = litellm.responses(
    model="openai/gpt-5.6-terra",
    input="Create a pie chart of market share and save as PNG",
    tools=[{"type": "code_interpreter"}]
)

# 2. Extract container_id from response
container_id = response.output[0].container_id  # e.g. "cntr_abc123..."

# 3. List files in container
files = litellm.list_container_files(
    container_id=container_id,
    custom_llm_provider="openai"
)

# 4. Download each file
for file in files.data:
    content = litellm.containers.retrieve_container_file_content(
        container_id=container_id,
        file_id=file.id,
        custom_llm_provider="openai"
    )
    
    with open(file.filename, "wb") as f:
        f.write(content)
    print(f"Downloaded: {file.filename}")
```

## Related

- [Containers API](/docs/containers) - Manage containers
- [Container Files API](/docs/container_files) - Manage files within containers
- [OpenAI Code Interpreter Docs](https://platform.openai.com/docs/guides/tools-code-interpreter) - Official OpenAI documentation

## Related pages

- [Computer Use](https://docs.litellm.ai/docs/completion/computer_use.md)
- [Code Interpreter Sandbox Interception](https://docs.litellm.ai/docs/sandbox.md)
