kosa8 docs

Guides / kosa8 Cloud / Code interpreter

Code interpreter

Send Python cells to a persistent sandbox. Each cell runs in a long-lived kernel inside the sandbox, so variables, imports and loaded data are still there for the next call. You get back the value of the cell's last expression, the charts it drew, and its output and errors. This is the tool a ChatGPT-style data-analysis agent needs.

Run a cell

curl -s -X POST https://api.kosa8.com/v1/sandboxes \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"template":"python","persistent":true}'
# {"sandbox":{"id":"sbx_…","persistent":true,…}}

curl -s -X POST https://api.kosa8.com/v1/sandboxes/sbx_…/code \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"code":"x = 40\nprint(\"hello\")\nx + 2"}'
# {"context":"default","execution_count":1,"results":[{"type":"text","data":"42"}],
#  "stdout":"hello\n","stderr":""}

curl -s -X POST https://api.kosa8.com/v1/sandboxes/sbx_…/code \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"code":"x * 10"}'
# {"context":"default","execution_count":2,"results":[{"type":"text","data":"400"}],…}

What comes back

curl -s -X POST https://api.kosa8.com/v1/sandboxes/sbx_…/code \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"code":"import matplotlib.pyplot as plt\nplt.plot([1, 3, 2])\nplt.title(\"demo\")"}'
# {"results":[{"type":"text","data":"Text(0.5, 1.0, 'demo')"},
#             {"type":"image/png","data":"iVBORw0KGgo…","name":"figure-1.png"}],…}

Contexts

POST   /v1/sandboxes/{id}/code/contexts {"name":"analysis"}   # start one (a cell to a new name starts it too)
GET    /v1/sandboxes/{id}/code/contexts                      # [{name, execution_count}]
POST   /v1/sandboxes/{id}/code/contexts/analysis/reset       # forget its variables
DELETE /v1/sandboxes/{id}/code/contexts/analysis             # end its process

How it works, and what it needs