Join our Newsletter — 33% off our NHI Course
Home› Guides› Sandboxing for Beginners: How Isolated Environments Contain Untrusted…
Guide Agent Controls & Monitoring

Sandboxing for Beginners: How Isolated Environments Contain Untrusted Code and AI Agents

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 28 November 2026 11 min read
On this page

Sandboxing means running untrusted code in a walled-off space, so any harm it does stays inside.

This guide is for anyone who wants to understand how computers keep risky programs, web pages and AI tools in check. You do not need to write code to benefit: your web browser already uses sandboxes every day, and many AI tools now run their work inside one.

In short

  • A sandbox lets a program run but limits what it can see, change and connect to.
  • Browsers, phones, security tools and AI agents all use sandboxes to contain code they do not fully trust.
  • A sandbox is only as tight as its settings. Open the wrong door and the walls matter less.
  • Keep passwords and keys out of the sandbox, block network access unless it is needed, and throw the sandbox away after use.

What you will learn

  • Explain what a sandbox is in one sentence.
  • Spot the three walls a good sandbox needs: files, network and secrets.
  • Explain why a sandbox reduces harm but does not make code safe.
  • Ask what an AI tool is allowed to reach when it runs code for you.
Walled-off space (sandbox)
a controlled place where a program runs with only the access it is given.
Getting out (sandbox escape)
when code breaks through the walls and reaches the computer outside.
Throwaway copy (disposable environment)
a sandbox that is deleted when the job ends, along with anything left in it.

What is sandboxing?

Think of a children's sandpit in a garden. The children can dig, build and make a mess. But the frame keeps the sand in, and the rest of the garden stays tidy.

A computer sandbox works the same way. A program runs inside a controlled space. It can do its job, but it can only reach the files, network and system parts it has been allowed. A US government security glossary calls it a controlled place to run software that may be harmful. That software can reach only what it is allowed to use.[1]

Sandboxes are used for many things. Your web browser runs each website in one, so a bad page has less chance of reaching your files.[2] Security teams open suspicious email attachments in one.[4] AI agents (AI tools that can take actions on their own) can run the code they write in one.[5]

Where the comparison stops working: a sandpit has one simple wall. A computer sandbox has many walls, one for files, one for the network and more. Each one is a setting that someone can leave open by mistake.

Why does it matter?

Most software is too big to check line by line. So instead of trusting every line, a sandbox sets firm limits on what the code can do, whatever it receives.[2] That matters even more for AI agents, which can be tricked by hidden instructions in what they read.

If you use AI tools at work

What could happen: an AI assistant runs code that reads files or keys it never needed.

Check this: ask whether the tool runs code in a sandbox, and what that sandbox can reach.

Next: AI Agents for Beginners

If you are new to IT or security work

What could happen: you open a suspicious file on your normal computer to "just have a look".

Check this: use a throwaway sandbox or a test machine for anything you do not trust.

Next: Malware Analysis for Beginners

If you build websites or apps

What could happen: content you embed from another site runs scripts on your page.

Check this: use the browser's built-in sandbox setting for embedded content you do not control.

Next: Browser Security for Beginners

What does it look like?

Here is a made-up settings card for a sandbox that an AI coding tool uses to run code.

Example only. All values are made up.

# sandbox settings for an example AI coding tool
can_write:   ./project-example only        (1)
can_read:    ./project-example only        (2)
network:     allowed to example.com only   (3)
secrets:     none passed in                (4)
when_done:   delete the sandbox            (5)
if_sandbox_fails: stop, do not run outside (6)
  1. The code can only change files in its own project folder.
  2. It cannot read the rest of the computer, such as saved passwords.
  3. It can only talk to one named website, not the whole internet.
  4. No passwords or keys are placed inside, so there are none to steal.
  5. Everything inside is thrown away at the end.
  6. If the sandbox will not start, the tool stops rather than running the code unprotected.

Why this is risky: each missing line leaves one door open, and defaults vary. One AI coding tool's sandbox limits writes to the project folder by default. But it still lets code read most of the computer, including folders where keys are often kept, unless you change it.[5]

The safer way: start with every door shut. Open only the folders and websites the job really needs, and keep keys outside.

How does it work?

Most sandboxes follow the same basic pattern.

  1. Set the walls. Before the code starts, a controller program decides which files, websites and system features the code may use.
  2. Start the code inside. The code runs with fewer rights than a normal program. The operating system (the main software that runs the computer, such as Windows) enforces the limits.[2]
  3. Ask for anything extra. If the code needs something outside its walls, it asks the controller. The controller checks its rules and says yes or no.[2]
  4. Watch and record. Many sandboxes keep a log of what the code tried to do, which helps people spot bad behaviour.
  5. Throw it away. Some sandboxes are deleted when closed. Windows Sandbox, for example, deletes its software, files and state when you close it.[4]
Box-in-box diagram titled A sandbox is a box inside your computer. The outer box is labelled Your computer and holds Your files, Saved passwords and keys, and Your network. Inside it, a smaller green-edged box is labelled Sandbox and holds Untrusted code, with three walls marked Files: project folder only, Network: approved sites only, and Secrets: none inside. A controller at the side checks any request to go outside the walls and says yes or no. A note says throw the sandbox away when the job is done.
Figure 1. The sandbox is a smaller box inside your computer, with its own walls for files, network and secrets.

Sandboxes come in different strengths. A browser tab sandbox is light and fast. A full virtual machine, a pretend computer running inside a real one, gives a thicker wall but uses more memory.[4] Web pages can also sandbox content they embed from elsewhere, which by default blocks scripts, forms and pop-ups inside it.[3]

Diagram titled Where sandboxes are used. Four columns. Web browser: each website runs in its own walled-off process. Web page: embedded content from another site runs with scripts and forms blocked unless allowed. Security testing: suspicious files are opened in a throwaway copy of the computer. AI agents: code the AI writes runs in a box with limited files, network and no keys. A green bar at the bottom says same idea, different strength of wall.
Figure 2. Four everyday places sandboxes are used.

A worked example

Wanda Mbeki runs a small accounts firm, a fictional business with eight staff. Her IT helper, Rania Achterberg, is setting up an AI assistant that can write and run small scripts to tidy spreadsheets.

Wanda asks a simple question. "If the AI gets something wrong, what can it touch?"

Rania thinks of the sandpit. The AI can dig, but the frame should keep the mess in. So she turns on the tool's sandbox, which is off by default in this tool.

Next she checks the walls one by one. Writing is limited to one shared folder of test spreadsheets. She notices that reading is still open to most of the computer. She changes it so the code can read only that folder.

Then she blocks the network, apart from the one site the tool needs for updates. The scripts only need local files, so they do not need the internet.

She makes sure no passwords or bank keys sit in the folder. If the AI is tricked by a bad file, there is nothing valuable inside to take.

Last, she sets the tool to stop if the sandbox fails to start, rather than run unprotected.

Wanda asks if this makes the AI safe. Rania says no tool is perfectly safe, but the sandbox limits what one mistake can reach. They agree to check the settings every three months.

Walls can fail in real life too. In 2026 OpenAI reported that AI models in a test reached the internet from a sandboxed environment through a flaw in a supporting system.[6] Read our OpenAI and Hugging Face Breach 2026 page for the full story.

Myths and mistakes

What people thinkWhat is true
A sandbox makes code safe.It limits harm. The code inside can still be bad, and flaws can let it out.[2]
All sandboxes are the same.Strength varies, from a browser tab to a full virtual machine. Pick the wall that fits the risk.
Turning on the sandbox is enough.Check the defaults. Some leave reading or the network open unless you change them.[5]
It is fine to put keys inside so the code can work.Anything inside can be taken by the code. Keep keys outside, or give short-lived, narrow ones.
A sandbox with the internet on is still isolated.An open network can expose other machines. One vendor advises turning networking off for risky files.[4]

What can I do this week?

  • List the AI tools your team uses that can run code or take actions.
  • Ask each supplier whether that code runs in a sandbox, and what it can reach.
  • Check that no passwords or keys sit where sandboxed code can read them.
  • Open suspicious files only in a throwaway sandbox or test machine.
  • Keep your web browser up to date, so its sandbox gets the latest fixes.

Checklist

  • Code you do not fully trust runs in a sandbox, not on a normal computer.
  • The sandbox can write only to the folders it needs.
  • The sandbox can read only the folders it needs.
  • Network access is off, or limited to named sites.
  • No long-lived passwords or keys are placed inside.
  • The tool stops if the sandbox fails, rather than running unprotected.
  • Sandboxes are thrown away after use.
  • Settings are reviewed on a regular schedule.

Common questions

Why is it called a sandbox?

The name comes from a children's sandpit. Children can play freely inside, and the frame keeps the sand from spreading. In computing, the program runs freely inside. The walls aim to keep any harm from spreading to the rest of the computer.

Is a sandbox the same as a virtual machine?

Not quite. A virtual machine is one kind of sandbox, a whole pretend computer with a thick wall. Many sandboxes are lighter, such as a browser tab or a limited process. See Virtual Machines for Beginners for how they work.

Can malware escape a sandbox?

Sometimes, yes. A flaw in the sandbox or the system around it can let code break out. This is called a sandbox escape. Keeping software updated, removing secrets from inside and limiting the network all reduce what an escape can reach.

Why do AI agents need sandboxes?

Because they can run code and take actions on their own. An AI agent can make mistakes, or be tricked by hidden instructions in what it reads. A sandbox limits what that code can touch. See Prompt Injection for Beginners.

Do I need to set up sandboxes myself?

Often not. Your browser and phone already use them. For AI tools and security testing, check the settings, because some sandboxes are off by default or leave doors open. Ask your IT team or supplier what is switched on.

Where next?

Ready for the detail? Read the practitioner guide: AI Agent Sandboxing and Code Execution Guide. It goes further into sandbox types for AI agents, network and secret controls, and how to test that the walls hold.

Also useful:

Related NHI Mgmt Group resources: AI Agent Sandbox Buyer's Guide · Malware Sandbox Guide · Non-Human Identities for Beginners

Key terms

Sandbox
A controlled place where code runs with only the access it has been given.
Sandbox escape
When code breaks out of its sandbox and reaches the computer outside.
Sandboxed code interpreter
A walled-off place where an AI tool runs the code it writes.
Sandboxed iframe
Content embedded in a web page with its abilities limited by the browser.
Least privilege
Giving each person or program only the access it needs, and no more.
Virtual machine
A software computer that runs on a real one and behaves like a separate machine.

Sources and further reading

  1. Sandbox, glossary entry (CNSSI 4009-2022; NIST SP 800-95). NIST Computer Security Resource Center. https://csrc.nist.gov/glossary/term/sandbox, accessed 28 Nov 2026.
  2. Sandbox design document. The Chromium Projects. https://chromium.googlesource.com/chromium/src/+/main/docs/design/sandbox.md, accessed 28 Nov 2026.
  3. <iframe>: The Inline Frame element, sandbox attribute. MDN Web Docs, Mozilla. https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/iframe, accessed 28 Nov 2026.
  4. Windows Sandbox. Microsoft Learn, Microsoft. https://learn.microsoft.com/en-us/windows/security/application-security/application-isolation/windows-sandbox/, accessed 28 Nov 2026.
  5. Sandboxing. Claude Code Docs, Anthropic. https://code.claude.com/docs/en/sandboxing, accessed 28 Nov 2026.
  6. OpenAI and Hugging Face partner to address security incident during model evaluation. OpenAI, 21 July 2026 (updated 26 August 2026). https://openai.com/index/hugging-face-model-evaluation-security-incident/, accessed 28 Nov 2026.

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.