Skip to content
mentoor

Topic 3 of 4 · 5 min read

Running analysis in the provider's code sandbox

Goal: Use a hosted code execution tool to analyse an uploaded file and interpret the outputs and errors it returns.

Hosted code execution runs the model's Python or Bash in a provider-managed container, so you upload a file, ask a question, and read back the outputs and errors, within fixed limits.

Check yourself · 1 of 3

In a code execution result block, which field tells you the script failed?

Choose one answer

Why it matters

Models are poor at exact arithmetic and large tables when they answer from text alone. Given code execution, the model writes a script, runs it on your data, and reports what the script printed. You get computed numbers, and you do not run the sandbox yourself.

How to do it

  1. Add the tool to the request. With Claude it is code_execution. Hosted code execution is also offered by OpenAI (code interpreter) and Gemini.
  2. Upload the file through the Files API and reference it with a container_upload block in the user message. The file appears inside the container.
  3. Ask a specific question: "Total revenue per region from sales.csv, and name any rows that failed to parse."
  4. Read the response blocks. Each command is a server_tool_use block followed by a result with stdout, stderr and return_code. Zero means success. Anything else means the script failed, even if the model's summary sounds fine.
  5. Reuse the container by passing the container id from an earlier response when later steps need earlier files. Without it each request starts a fresh container, and expired containers cannot be reused.

Limits to plan for

  • No internet. The container cannot download packages. Only the pre-installed libraries are available, so pip install fails.
  • Memory. The container has 5 GiB of RAM; large files can fail to load.
  • Time. A call that runs too long returns an execution_time_exceeded error.
  • Cost. Each organization gets 1,550 free hours a month, then $0.05 per hour per container. Code execution is free when used together with the newer web search or fetch tools.
  • Errors in the block. Tool-level errors use codes such as unavailable, execution_time_exceeded and too_many_requests.

Before and after

Weak: "Here is sales.csv pasted as text. What is the total?" The model adds 4,000 rows in its head and gets it nearly right.

Better: upload the file, enable code execution, and ask for the total. The stdout shows total=1284930.55, and you can open the script to check it.

resp = client.messages.create(
    model=MODEL, max_tokens=2000,
    tools=[{"type": "code_execution_20250825", "name": "code_execution"}],
    messages=[{"role": "user", "content": [
        {"type": "text", "text": "Total revenue per region in sales.csv."},
        {"type": "container_upload", "file_id": file_id},
    ]}],
)

What goes wrong

  • Trusting the summary over the return code. The model may describe a failed run as if it worked.
  • Expecting network access. Scripts that call an API or install a library fail.
  • Losing state. A new container per request means yesterday's files are gone.

Examples

Reading a failed run

A result block shows return_code: 1 and stderr: ModuleNotFoundError: No module named 'geopandas'. The summary text says "I could not map the regions". Cause: the library is not pre-installed and the sandbox cannot install it. Fix: ask for the analysis with the libraries available, or do the mapping step in your own code.

Two-step analysis

Request one uploads orders.csv and asks Claude to clean it and save clean.csv. The response includes container.id. Request two passes that id as container and asks for a chart from clean.csv. Without the id, request two would not find the file.

Key points

  • Upload files with the Files API and reference them with a container_upload block so the script reads real data.
  • Check stdout, stderr and return_code in the result block; a non-zero return code means the run failed.
  • The container has no internet access, so only pre-installed libraries work and package installs fail.
  • Pass the container id back to keep files between requests; otherwise each request starts fresh.
  • Plan for memory, time and hourly container cost limits before sending large files.

Common mistakes

  • Pasting a large table into the prompt instead of uploading it and running code on it.
  • Accepting the model's summary without looking at the return code and stderr.
  • Expecting pip install or an outbound API call to work in the sandbox.
  • Assuming files persist across requests without reusing the container.

Check yourself · 2 of 3

A script run in the hosted sandbox tries pip install and fails. Why?

Choose one answer

Check yourself · 3 of 3

In request one, the model's code turned your raw upload into a cleaned CSV inside the sandbox. You need that cleaned CSV in request two. What do you do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic