---
title: Add a code execution tool to your agent
description: Give your agent a tool that runs generated code in Vercel Sandbox, with network limits, timeouts, and bounded results.
url: "https://vercel.com/kb/prompts/add-sandbox-code-execution-tool"
last_updated: 2026-12-01
related_resources:
  "[Add Python code execution to a TypeScript AI agent with Vercel Sandbox](/kb/guide/python-ai-sdk-vercel-sandbox)": "Add an `executeCode` tool to your TypeScript AI SDK agent with the `ai-sdk-tool-code-execution` package to run Python 3.13 inside an isolated Vercel Sandbox."
  "[Build your startup's product on Vercel Sandbox](/kb/guide/startups-on-vercel-sandbox)": "Build code interpreters, app previews, and upload-processing features with Vercel Sandbox, including authentication, persistence, security, and cost controls."
  "[Vercel Sandbox](https://vercel.com/docs/sandbox)": "Run repository code and agent-generated commands in isolated environments for investigation, reproduction, and testing."
  "[A sandbox without a network boundary is only half a sandbox](https://vercel.com/blog/a-sandbox-without-a-network-boundary-is-only-half-a-sandbox)": "Learn why agents running untrusted repository code need deny-by-default egress, with credentials kept out of the sandbox."
---

# Add a code execution tool to your agent

Give your agent a tool that runs generated code in Vercel Sandbox, with network limits, timeouts, and bounded results.

Let your agent answer questions by writing and running code, such as analyzing an uploaded file or checking a calculation. This prompt adds one execution tool that runs each job in an isolated Vercel Sandbox microVM instead of your app's own runtime.

Open or import the agent repository and describe what the code should do, which languages it needs, and which files or services it may reach. The handoff includes prompt text only; Vercel project access and any credentials must be configured separately.

Expect a connected tool, a locked-down sandbox configuration, and tests for failures such as runaway code and blocked network calls. Isolation limits what generated code can reach, but it can't decide whether a result is correct, so keep human review where answers matter.

## Agent prompt

```text
Add one tool to the agent in this project that runs model-generated code in Vercel Sandbox and returns a bounded, structured result.

If the intended repository is unavailable, ask me to open or import it first. Read repository instructions and inspect the agent framework, installed SDK versions, existing tools, authentication, and tests. Reuse the package manager and established patterns. Install the Vercel Sandbox SDK for the project's language if it is missing, and verify the installed version's APIs before implementation; do not migrate the agent framework for this task. Ask which language and runtime the code needs, what inputs it works on, and whether it needs any network access when those requirements are missing.

Define a narrow tool name and description that say when the agent should run code, the input it accepts, and what it returns. Validate the input with a schema and cap code size. Never run generated code in the application's own process or file system. Create the sandbox from server code, authenticate with the project's Vercel OIDC token or a server-side access token, and never pass Vercel or service credentials into the sandbox or into model-visible text.

Lock the sandbox down before code runs. The default network policy allows all outbound traffic, so set `deny-all`, or an allow list of the specific domains the task needs. Set an explicit session timeout and a per-command deadline that fit the work, within the plan's runtime limit. Sandboxes are persistent by default; for one-off jobs, opt out of persistence so unused snapshots don't accumulate. If the job needs packages, prefer a prepared snapshot or image over installing them on every call. Write input files into the sandbox rather than mounting broad storage.

Treat everything the code produces as untrusted. Capture the exit code, stdout, and stderr separately, truncate them to a fixed size, and return only declared output files. Distinguish invalid input, code that exits with an error, a timeout, a blocked network request, and a sandbox platform failure, and redact internal details from model-visible errors. Stop or delete the sandbox when the call finishes or fails, including on cancellation, so no session keeps running.

Enforce the authenticated user's permissions in the tool's server code. Resolve identity from trusted server context, not model arguments. Only pass the user's own files into the sandbox, and keep one user's sandbox, files, and results separate from another's.

Test a successful run, invalid input, code that exits with an error, an infinite loop that hits the timeout, oversized output, a blocked outbound request, and cleanup after failure. Mock the Sandbox SDK in unit tests while keeping validation and authorization real. Add one opt-in integration test that creates a real sandbox. Separate these tests from any evaluation of whether the model chooses to run code at the right time.

Implement locally and run the relevant checks. Ask before creating real sandboxes, making paid model calls, or deploying. Report actual commands and results, blocked checks, the configuration needed, and remaining risks, such as allowed domains that could be used to send data out.
```

## Related resources

- [Add Python code execution to a TypeScript AI agent with Vercel Sandbox](/kb/guide/python-ai-sdk-vercel-sandbox): Add an \`executeCode\` tool to your TypeScript AI SDK agent with the \`ai-sdk-tool-code-execution\` package to run Python 3.13 inside an isolated Vercel Sandbox.

- [Build your startup's product on Vercel Sandbox](/kb/guide/startups-on-vercel-sandbox): Build code interpreters, app previews, and upload-processing features with Vercel Sandbox, including authentication, persistence, security, and cost controls.

- [Vercel Sandbox](https://vercel.com/docs/sandbox): Run repository code and agent-generated commands in isolated environments for investigation, reproduction, and testing.

- [A sandbox without a network boundary is only half a sandbox](https://vercel.com/blog/a-sandbox-without-a-network-boundary-is-only-half-a-sandbox): Learn why agents running untrusted repository code need deny-by-default egress, with credentials kept out of the sandbox.

## Try other prompts

- [Add a tool to an existing agent](/kb/prompts/add-agent-tool): Extend your agent with one focused tool, including validated inputs, server-side permissions, and tests for service failures.

- [Evaluate an agent before changing its prompt](/kb/prompts/evaluate-agent-prompt-changes): Build a repeatable evaluation for an AI SDK agent, compare prompt versions, and surface failures hidden by an average score.

- [Run agent evaluations in isolated sandboxes](/kb/prompts/run-agent-evals-in-sandboxes): Run each trial of an agent evaluation in its own Vercel Sandbox, forked from one baseline, and compare repeatable results.

- [Build your startup's product on Vercel Sandbox](/kb/prompts/build-startup-product-on-vercel-sandbox): Add a code interpreter, live app previews, or upload processing to your product with Vercel Sandbox, with auth and cost controls.

## Reference Architecture

Choose a stack and work through the decisions behind it, with curated documentation, guides, templates, and courses to help you build.

- [Agent stack](/kb/architecture/agents): Build and operate agents with models, tools, and durable workflows.
- [Web applications](/kb/architecture/web): Choose rendering, data, authentication, and caching approaches.
- [Backends](/kb/architecture/backends): Choose a runtime, connect services, and run background work.

[Browse all stacks](/kb/architecture)

## Build with prompts

Build an app, extend an agent, or improve your project with ready-to-use prompts for your coding agent.

[Browse the prompt library](/kb/prompts) · [Read the Markdown index](/kb/prompts.md)
