Together Code Interpreter

Seamlessly execute LLM-generated code with a simple API call

Using the Together Code Interpreter

Execute Endpoint

The Execute endpoint allows you to run code snippets and receive the output. It supports session-based execution, which means you can maintain state between multiple code executions. A session has a maximum duration of 30 minutes, after which it will no longer be available and you need to start a new session.

Basic Usage

POST <https://api.together.ai/tci/execute>
Content-Type: application/json
Authorization: Bearer YOUR_TOGETHER_API_KEY

{
  "language": "python",
  "code": "print('Hello, world!')"
}

{
  "data": {
    "outputs": [
      {"data":"Hello, world!\\n","type":"stdout"}
      ],
    "session_id":"ses_CM41U1rWw2RoKcJ1GgFnA"
    },
  "errors":null
}

Using Sessions

Sessions allow you to maintain state between executions:

  1. First request - A session ID is returned:

    POST <https://api.together.ai/tci/execute>
    Content-Type: application/json
    Authorization: Bearer YOUR_TOGETHER_API_KEY
    
    {
      "language": "python",
      "code": "!pip install matplotlib"
    }
    
    # Response
    {
    	"data": {
    		"outputs": [],
    		"session_id":"ses_CM42NfvvzCab123"
    	},
    	"errors":null
    }
    
  2. Follow-up requests - Include the session ID. The previously installed library will now be available.

POST <https://api.together.ai/tci/execute>
Content-Type: application/json
Authorization: Bearer YOUR_TOGETHER_API_KEY

{
 "code":  "import matplotlib.pyplot as plt\\n\\nx = [1, 2, 3, 4, 5]\\ny = [2, 3, 5, 7, 11]\\n\\nplt.figure()\\nplt.plot(x, y, marker='o')\\nplt.title('Simple Graph')\\nplt.xlabel('X-axis')\\nplt.ylabel('Y-axis')\\nplt.grid(True)\\n\\nplt.show()",
  "session_id": "ses_CM42NfvvzCab123"
}

# Response
{
"data":{
 "outputs":[
   {"data":{
     "image/png":"iVBORw0KGgoAAAANSUhEUgAAA...",
     "text/plain":"<Figure size 640x480 with 1 Axes>"
     },
     "type":"display_data"
   }],
 "session_id":"ses_CM42NfvvzCab123"
 },
"errors": null
}

Response Format

The API returns:

  • session_id: Identifier for the current session
  • outputs: Array of execution outputs, which can include:
    • Execution output (the return value of your snippet)
    • Standard output (stdout)
    • Standard error (stderr)
    • Error messages
    • Rich display data (images, HTML, etc.)

List Active Sessions

To retrieve all your active sessions:

GET <https://api.together.ai/tci/sessions>
Content-Type: application/json
Authorization: Bearer YOUR_TOGETHER_API_KEY

# Response
{
  "data": {
  "sessions": [
    {
      "id":"ses_CM3zokEcfkdh5G8UiKApw",
      "started_at":"2025-03-12T10:34:22.125248Z",
      "execute_count":2,
      "last_execute_at":"2025-03-12T10:37:24.145685Z",
      "expires_at":"2025-03-12T11:04:22.125248Z"
    }
    ]
  },
  "errors": null
}