Together Code Interpreter
Overview
Execute Python code in sandboxed sessions via a simple API call. Sessions persist state for 60 minutes and come pre-installed with popular data science packages.
- Endpoint:
https://api.together.ai/tci/execute - Pricing: $0.03 per session
- Session lifespan: 60 minutes (reusable)
- Also available as an MCP server via Smithery
Installation
# Python (recommended)
uv init # optional, if starting a new project
uv add together
# or with pip
pip install together
# TypeScript / JavaScript
npm install together-ai
Set your API key:
export TOGETHER_API_KEY=<your-api-key>
Quick Start
Execute Code
from together import Together
client = Together()
response = client.code_interpreter.execute(
code='print("Hello from TCI!")',
language="python",
)
print(f"Status: {response.data.status}")
for output in response.data.outputs:
print(f"{output.type}: {output.data}")
import Together from 'together-ai';
const client = new Together();
const response = await client.codeInterpreter.execute({
code: 'print("Hello from TCI!")',
language: 'python',
});
for (const output of response.data.outputs) {
console.log(`${output.type}: ${output.data}`);
}
curl -X POST "https://api.together.ai/tci/execute" \
-H "Authorization: Bearer $TOGETHER_API_KEY" \
-H "Content-Type: application/json" \
-d '{"language": "python", "code": "print(\"Hello from TCI!\")"}'
Reuse Sessions (Maintain State)
# First call — creates a session
response1 = client.code_interpreter.execute(code="x = 42", language="python")
session_id = response1.data.session_id
# Second call — reuses state
response2 = client.code_interpreter.execute(
code='print(f"x = {x}")',
language="python",
session_id=session_id,
)
# Output: stdout: x = 42
import Together from 'together-ai';
const client = new Together();
// Run the first session
const response1 = await client.codeInterpreter.execute({
code: 'x = 42',
language: 'python',
});
if (response1.errors) {
console.log(`Response 1 errors: ${response1.errors}`);
} else {
// Save the session_id
const sessionId = response1.data.session_id;
// Reuse the first session
const response2 = await client.codeInterpreter.execute({
code: 'print(f"The value of x is {x}")',
language: 'python',
session_id: sessionId,
});
if (response2.errors) {
console.log(`Response 2 errors: ${response2.errors}`);
} else {
for (const output of response2.data.outputs) {
console.log(`${output.type}: ${output.data}`);
}
}
}
# First call — creates a session
curl -X POST "https://api.together.ai/tci/execute" \
-H "Authorization: Bearer $TOGETHER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"language": "python",
"code": "x = 42"
}'
# Second call — reuse session_id from the first response
curl -X POST "https://api.together.ai/tci/execute" \
-H "Authorization: Bearer $TOGETHER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"language": "python",
"code": "print(f\"The value of x is {x}\")",
"session_id": "YOUR_SESSION_ID_FROM_FIRST_RESPONSE"
}'
Upload Files
script_file = {"name": "data.py", "encoding": "string", "content": "print('loaded')"}
response = client.code_interpreter.execute(
code="!python data.py",
language="python",
files=[script_file],
)
import Together from 'together-ai';
const client = new Together();
const scriptContent = "import sys\nprint(f'Hello from inside {sys.argv[0]}!')";
const scriptFile = {
name: "myscript.py",
encoding: "string",
content: scriptContent,
};
const response = await client.codeInterpreter.execute({
code: "!python myscript.py",
language: "python",
files: [scriptFile],
});
for (const output of response.data.outputs) {
console.log(`${output.type}: ${output.data}`);
}
curl -X POST "https://api.together.ai/tci/execute" \
-H "Authorization: Bearer $TOGETHER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"language": "python",
"files": [
{
"name": "myscript.py",
"encoding": "string",
"content": "import sys\nprint(f'"'"'Hello from inside {sys.argv[0]}!'"'"')"
}
],
"code": "!python myscript.py"
}'
Install Packages
response = client.code_interpreter.execute(
code="!pip install transformers\nimport transformers\nprint(transformers.__version__)",
language="python",
)
Response Format
{
"data": {
"session_id": "ses_CM42NfvvzCab123",
"status": "completed",
"outputs": [
{"type": "stdout", "data": "Hello!\n"},
{"type": "display_data", "data": {"image/png": "iVBOR..."}}
]
},
"errors": null
}
Output types: stdout, stderr, display_data (images, HTML), error
List Active Sessions
response = client.code_interpreter.sessions.list()
for session in response.data.sessions:
print(session.id)
Pre-installed Packages
numpy, pandas, matplotlib, scikit-learn, scipy, seaborn, plotly, bokeh, requests, beautifulsoup4, nltk, spacy, opencv-python, librosa, sympy, pytest, openpyxl, and more. Install additional packages with !pip install.
Use Cases
- Data analysis: Pandas, NumPy, matplotlib workflows
- RL training: Interactive code execution with reward signals
- Agentic workflows: LLM-generated code execution in a loop
- Visualization: Generate charts and plots returned as base64 images
Resources
- Runnable script: See scripts/execute_with_session.py — execute code with session reuse and chart generation (v2 SDK)
- Runnable script (TypeScript): See scripts/execute_with_session.ts — minimal OpenAPI
x-codeSamplesextraction for execute + sessions list (TypeScript SDK) - Official docs: Together Code Interpreter
- API reference: TCI API