Tool Use
Intermediate 12 min read Tool Use

Code Execution Tokens

Let the model compute — safely

A code-execution tool lets the model run writes, math, manipulation or experiments. Powerful, and dangerous: it needs a sandbox, timeouts, resource caps, and a read-only bias. Done right it converts 'estimate' into 'compute'.

code executionsandboxpythontimeoutresource capread-only
Prerequisites: Tool Use Overview
Before we start

What you will learn

  • Architect a sandboxed executor
  • Cap runtime resources
  • Bias toward read-only execution
Introduction

What is this?

A code-execution tool runs model-written code (usually in a sandbox): Python in a container, timeout enforced, memory/CPU bounded, network and filesystem restricted, results returned as text/images. It makes computations exact and enables manipulation, analysis and iteration the model couldn't do by reasoning alone.

Motivation

Why do you need it?

  • Math by reasoning is unreliable; math by code is exact.
  • Iterative experiments — clean, transform, plot — turn the model into an analyst.
  • Deterministic computation fixes a whole class of arith hallucinations.
  • Sandboxing turns the danger into a governed, bounded compute slot.
Think about it

Mental model

Imagine

A chemistry lab with fume hoods

Think of

Code executor

Students (models) may run experiments, but only in hooded, fume-containing benches: gloves (sandbox), timed sessions (timeout), capped reagents (memory), no contact with street (network off). The hood is why you're fine letting students cook.

Mechanics

How it works

  • Pass code + optional input files to a sandboxed runtime.
  • Enforce timeout, memory, CPU, disk limits.
  • Restrict network where not needed; block sensitive mounts.
  • Capture stdout/stderr + artifacts.
  • Return the result (text/render) back to the model loop.

Execution envelope.

Code
Sandbox
Limits
Output
Feedback
Real world

In production

  • Data analysts query DBs then plot with Python.
  • Finance tools compute exact forecasts and tables.
  • Infrastructure copilots dry-run then apply approved changes.
Guidelines

Best practices

  • Default sandbox: no network, tight time, modest memory.
  • Persist nothing across runs unless intentional.
  • Return structured stdout + artifacts, not raw dumps.
  • Log every execution (code, limits, outcome) for audit.
Watch out

Common mistakes

  • Executing in the server's own container.
  • No timeout — infinite loops bill forever.
  • Giving the executor prod credentials.
Root cause analysis

Common issues

Error
Model writes buggy code repeatedly
Root cause

No feedback loop

Fix

Return tracebacks; let the model fix and re-run (capped).

Error
Sandbox lacks libs
Root cause

Thin image

Fix

Curate the image for your use cases.

When things break

Debugging tips

  • Execution reports missing deps → preload required libs in the image.
  • Timeout hits commonly → lower expectations or split the task.
  • Non-deterministic results → seed/bump environment drift in logs.
Interviews

Interview checkpoint

Practice

Hands-on lab

Sandbox a Python Executor with Cap
35 min · intermediate
In progress
Objectives
  • Execute model-provided code in a subprocess
  • Enforce timeout and resource limits
  • Return stdout/stderr to the calling loop

Sandbox a Python Executor with Cap

35 min · intermediate

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Sandbox prerequisites for a code tool:

2

Deterministic computation removes arithmetic hallucination.

Go deeper

Official documentation