Skip to main content
The code execution tool lets the assistant run bash commands and create, view, and edit files inside a sandboxed container, directly within a /messages request. Use it to analyze uploaded data, run calculations, transform documents, or produce files such as charts, spreadsheets, and reports. It follows the same model as the Anthropic code execution tool: the server runs every command, so you never execute code or send tool_result blocks yourself.

Prerequisites

  • An API token. See Backoffice Developer Console.
  • A configured code-execution provider, included by default in standard Zylon 1.58 deployments. For Workspace API requests, the Workspace code-execution feature must also be enabled. See Code Execution for configuration and disabled behavior.

Requirements

  • Tool spec: { "name": "code_execution", "type": "code_execution_v1" }
  • Anthropic-compatible spec also accepted: { "name": "code_execution", "type": "code_execution_20250825" }
  • No tool_context required
  • Optional: a container ID to reuse a sandbox across requests

Quick start

To check the request without running it, send the same body to POST /api/gpt/v1/messages/validate.

How code execution works

When you add the code execution tool to a request:
  1. The assistant decides whether running code would help answer the question.
  2. The tool expands into two sub-tools that the assistant can combine freely:
    • bash_code_execution: run shell commands.
    • text_editor_code_execution: view, create, and edit files.
  3. Every operation runs server-side in a sandboxed container, and the results are fed back to the assistant within the same request.
  4. The response contains each tool call (server_tool_use) and its result, followed by the assistant’s final answer.
  5. The response also contains a top-level container object. Pass its id back in later requests to keep working in the same sandbox.
For borderline requests, ask explicitly (for example, “run code to verify this”) so the assistant uses the sandbox instead of answering from memory.

Sub-tools

bash_code_execution Each call runs in a new shell. Files persist between calls; environment variables and cd do not. text_editor_code_execution Relative paths resolve against the workspace (/home/agent/workspace/).

Containers

A container is an isolated sandbox with its own file system. The container is identified by a string ID that you choose or that the server assigns.

Select a container

Set the top-level container field to the ID you want to use. If the container doesn’t exist yet, it’s created; if it exists, the request reuses it and all its files.
If you omit container, the sandbox is keyed on the authenticated user, so every request from that user without a container shares the same sandbox. Use an explicit, unique container ID per conversation or task to keep work isolated.

Read the container from the response

When code runs, the response includes the container that was used:
When streaming, the container object arrives in the final message_delta event as delta.container.

Reuse a container

Pass the same container ID in follow-up requests to keep the files created earlier:
expires_at is a rolling value that is refreshed on every request. When a container is idle, its sandbox is shut down to free resources, but its files are kept. The next request with the same ID starts a fresh sandbox with the same files. Installed packages, running processes, and shell state aren’t kept. To start clean, use a new container ID or delete the files.

File system layout

If your application expects a generated file, ask the assistant to save it to /mnt/user-data/outputs/ (for example, “save the chart as /mnt/user-data/outputs/chart.png”).

Runtime environment

The sandbox includes Python 3 with common data libraries: pandas, numpy, scipy, scikit-learn, statsmodels, matplotlib, seaborn, pyarrow, openpyxl, xlsxwriter, xlrd, pillow, python-pptx, python-docx, pypdf, pdfplumber, pypdfium2, pdf2image, pdfkit, tabula-py, reportlab, img2pdf, sympy, mpmath, tqdm, python-dateutil, pytz, and joblib. Command-line tools include unzip, unrar, 7zip, bc, rg, fd, and sqlite3.

Networking and security

  • No internet access. The sandbox can’t reach the internet, so packages can’t be installed at runtime. Only the pre-installed libraries are available.
  • Zylon API access. The sandbox can reach the Zylon API, with these environment variables already set: ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, and ANTHROPIC_MODEL. Code running in the sandbox, such as a script that uses the Anthropic SDK, can call the model with the permissions of the user who sent the request.
  • Isolation. Each container runs in its own sandbox, isolated from the host and from other containers.
Your operator can allow access to additional internal services. See Code Execution.

Work with files

Use the Files API to put files into a container before a request, and to list, download, and delete the files it produced. Every Files API call takes a scope_id query parameter: this is the container ID.

Upload files

Upload a file into the container’s uploads folder. It’s available to the assistant at /mnt/user-data/uploads/{filename}:
Response:
The id is the file ID used by the other endpoints. It’s the base64url-encoded sandbox path (here, /mnt/user-data/uploads/sales.csv), without padding. Use the path query parameter to choose the key, for example to organize files into folders. Parent folders are created automatically, and uploading to an existing key overwrites it:
A key that starts with outputs/ places the file in /mnt/user-data/outputs/ instead. Keys must be relative and can’t contain .. or end with /. You can also put raw bytes at a key, S3-style:

Analyze uploaded files

After uploading, send a message with the same container ID and refer to the file by its path:

List files

List every file in a container, both uploads and generated outputs, sorted by creation time:
downloadable is true for files in /mnt/user-data/outputs/ and false for uploads. Optional query parameters:

Download files

Download a file with its ID:
Add disposition=inline to open the file in the browser instead of downloading it. The file ID is the base64url-encoded sandbox path, so you can also build it yourself:

Delete files

Delete one file, whether it’s an upload, a generated output, or a workspace file:
Delete every uploaded file under a prefix. Use a trailing / so sibling keys that share the same text aren’t deleted:

End-to-end example

Code execution in the Workspace API

When you use the Workspace API instead of calling the ZylonGPT API directly, Zylon manages the container for you:
  • The container is the chat thread. Each thread gets its own sandbox, and the container field is set to the thread ID. Files persist for the life of the thread, and threads can’t see each other’s files.
  • Code execution is added automatically when the request uses Skills and the Workspace feature is enabled. You can also request it explicitly through pgpt.tools. If features.codeExecution.enabled is false, Workspace removes these tools, including explicitly requested ones. This flag does not globally disable the ZylonGPT API provider; see When disabled.
  • Generated files become artifacts. When the assistant presents a file, Zylon saves it as an artifact in the project and returns it in the stream as a zylon-resource block, so users can preview and download it.
Use the thread endpoints to manage the files in a thread’s sandbox:
Downloads follow your organization’s settings: generated files (outputs/) need generated-resource downloads to be enabled, and uploaded files (uploads/) need uploaded-resource downloads to be enabled.

Response format

Each sub-tool call appears as a server_tool_use block followed by its result block.

Bash command

A non-zero return_code isn’t an error: it means the command ran and failed, and the assistant can read stderr and retry.

File operations

View:
Create:
Edit (str_replace and insert):

Errors

When a tool can’t run, its result contains an error block:
detail.explanation holds the underlying reason. It’s a Zylon extension and is omitted in Anthropic-compatible responses.

Tips

  • Use one container ID per conversation or task, and reuse it for every follow-up and Files API call.
  • Ask for deliverables in /mnt/user-data/outputs/ so they can be listed and downloaded.
  • Combine code execution with other tools, such as Web Fetch or Semantic Search, to bring data into the analysis.
  • Delete uploads and outputs you no longer need: files stay in the container until you remove them.

Next steps