Sandbox security constraints
On this page
An isolated environment by design
The Code Execution Python sandbox is designed for security. Code runs in an isolated environment that protects both xAI’s infrastructure and your data. Understanding these constraints is essential to design integrations that work correctly and to know when Code Execution fits your needs.
The five fundamental constraints
1. No network access
The sandbox has no connection to the Internet or an external network. Code cannot:
- Make HTTP requests (no
requests, nourllib) - Connect to remote databases
- Call external APIs
- Download files from the web
- Send emails or notifications
This constraint is the most restrictive. If your use case requires external data, you must provide it in the prompt or use Web Search beforehand to fetch it.
2. Limited file system access
Code runs in a temporary directory with restricted file system access:
- Read/write only in the temporary working directory
- No access to system files
- No access to other users’ directories
- Files created are deleted after execution
3. Temporary and stateless context
Each execution is independent. There is no persistence between two API requests:
- Variables defined during one execution do not exist in the next
- Created files are deleted
- Libraries loaded in memory are reset
- No execution history is kept
This means you cannot build an interactive session where results from one calculation feed into the next—at least not automatically. To simulate this state, you must include previous results in your new prompt.
4. Execution time constraints
The sandbox imposes a timeout on code execution. If a calculation takes too long, it stops. This implies:
- No infinite loops (they will be stopped)
- No extremely long calculations (large datasets, heavy simulations)
- No wait operations (
time.sleepfor long durations)
The model is generally aware of these limits and writes optimized code, but very ambitious requests may fail due to timeout.
5. Memory limitations
The sandbox has a limited amount of memory. Operations on very large data volumes may fail:
- Pandas DataFrames with millions of rows risk exceeding memory
- Very large-dimensional NumPy matrices can cause issues
- Operations that duplicate data in memory increase the risk
Design implications
What Code Execution does well
- Precise mathematical and statistical calculations
- Analysis of moderate-sized datasets (hundreds to thousands of rows)
- Verification of formulas and algorithms
- Generation of charts and visualizations
- Rapid prototyping of calculation logic
What Code Execution does not do well
- Processing large data volumes (big data)
- Interactions with external systems
- Iterative calculations across multiple requests
- Machine learning and model training
- Long operations (> a few seconds)
Workarounds for limitations
If you need to exceed these constraints:
- External data: use Web Search to fetch data, then Code Execution to analyze it
- Large volumes: extract a representative sample and analyze it in the sandbox, then apply results to the whole set in your own environment
- State between requests: store intermediate results client-side and inject them into subsequent requests
Key takeaways
- The sandbox is isolated: no network, no persistence, no system access
- Each execution is independent (stateless)
- Time and memory limits apply
- Code Execution excels at precise calculations and moderate data analysis
- For edge cases, combine Code Execution with other tools or your own infrastructure