Together Code Interpreter (beta)

Execute LLM-generated code seamlessly with a simple API call.

Together Code Interpreter (TCI) enables you to execute Python code directly from LLMs.

Example Use Cases:

  • Reinforced learning (RL) training: TCI transforms code execution into an interactive RL environment where generated code is run and evaluated in real time, providing reward signals from successes or failures, integrating automated pass/fail tests, and scaling easily across parallel workers—thus creating a powerful feedback loop that refines coding models over many trials.
  • Developing agentic workflows: TCI allows AI agents to seamlessly write and execute Python code, enabling robust, iterative, and secure computations within a closed-loop system.
Overview of TCI's role in a closed-loop agentic workflow system.

Overview of TCI's role in a closed-loop agentic workflow system.

ℹ️

Currently available in beta

TCI is currently available in beta. During this beta, you won't be billed for usage but lower rate limits will apply. If you need higher rate limits, please contact us.

We're also giving $50 in TCI credits to beta users who share their feedback with us!

Usage overview

Together has created sessions to measure TCI usage.

A session is an active code execution environment that can be called to execute code, they can be used multiple times and have a lifespan of 60 minutes.

Typical TCI usage follows this workflow:

  1. Start a session (create a TCI instance).
  2. Call that session to execute code; TCI outputs stdout and stderr.
  3. Optionally reuse an existing session by calling its session_id.

Language Support

The TCI only currently supports python.

The TCI library is included by default in our Python SDK.

Run your first query using the TCI

from together import Together

client = Together()

# Create a code interpreter instance
code_interpreter = client.code_interpreter

# Simple print statement
print("Example 1: Simple print")
response = code_interpreter.run(
    code='print("Hello from Together!")',
    language="python"
)
print(f"Status: {response.data.status}")
for output in response.data.outputs:
    print(f"{output.type}: {output.data}")
if response.data.errors:
    print(f"Errors: {response.data.errors}")
print("\n")
import Together from 'together-ai';

const client = new Together();

const response = await client.codeInterpreter.execute({
  code: 'print("Hello from Together!")',
  language: 'python',
});

if (response.errors) {
  console.log(`Errors: ${response.errors}`);
} else {
  for (const output of response.data.outputs) {
    console.log(`${output.type}: ${output.data}`);
  }
}

Reusing sessions and maintaining state between runs

The session_id can be used to access a previously initialized session. All packages, variables, and memory will be retained.

from together import Together

client = Together()

# Create a code interpreter instance
code_interpreter = client.code_interpreter

response1 = code_interpreter.run(
    code='x = 42',
    language="python"
)

# save session_id
session_id = response1.data.session_id

response2 = code_interpreter.run(
    code='print(f"The value of x is {x}")',
    language="python",
    session_id=session_id
)

for output in response2.data.outputs:
    print(f"{output.type}: {output.data}")
if response2.data.errors:
    print(f"Errors: {response2.data.errors}")
print("\n")
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}`);
  return;
}

// Save the session_id
const sessionId = response1.data.session_id;

// Resuse 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}`);
  return;
}

for (const output of response2.data.outputs) {
  console.log(`${output.type}: ${output.data}`);
}

Using the TCI for Data analysis

Together Code Interpreter is a very powerful tool and gives you access to a fully functional coding environment. You can install Python libraries and conduct fully fledged data analysis experiments.

code = '''
!pip install numpy
import numpy as np

# Create a random matrix
matrix = np.random.rand(3, 3)
print("Random matrix:")
print(matrix)

# Calculate eigenvalues
eigenvalues = np.linalg.eigvals(matrix)
print("\\nEigenvalues:")
print(eigenvalues)
'''

response = code_interpreter.run(
  	code=code,
    language="python"
)

for output in response.data.outputs:
    print(f"{output.type}: {output.data}")
if response.data.errors:
    print(f"Errors: {response.data.errors}")
import Together from 'together-ai';

const client = new Together();

// Data analysis
const code = `
  !pip install numpy
  import numpy as np

  # Create a random matrix
  matrix = np.random.rand(3, 3)
  print("Random matrix:")
  print(matrix)

  # Calculate eigenvalues
  eigenvalues = np.linalg.eigvals(matrix)
  print("\\nEigenvalues:")
  print(eigenvalues)
`;

const response = await client.codeInterpreter.execute({
  code,
  language: 'python',
});

if (response.errors) {
  console.log(`Errors: ${response.errors}`);
} else {
  for (const output of response.data.outputs) {
    console.log(`${output.type}: ${output.data}`);
  }
}

Pre-installed dependencies

TCI's Python sessions come pre-installed with the following dependencies:

  • aiohttp
  • beautifulsoup4
  • bokeh
  • gensim
  • imageio
  • joblib
  • librosa
  • matplotlib
  • nltk
  • numpy
  • opencv-python
  • openpyxl
  • pandas
  • plotly
  • pytest
  • python-docx
  • pytz
  • requests
  • scikit-image
  • scikit-learn
  • scipy
  • seaborn
  • soundfile
  • spacy
  • textblob
  • tornado
  • urllib3
  • xarray
  • xlrd
  • sympy

Further reading

TCI API Reference docs.

Troubleshooting & questions

If you have questions about integrating TCI into your workflow or encounter any issues, please contact us.


What’s Next

We're actively looking for your feedback about Together Code Interpreter! We're giving $50 in TCI credits to everyone who submits comprehensive feedback with us during the beta.