{"slug": "how-to-use-boxlite-with-python", "title": "How to use BoxLite with Python?", "summary": "BoxLite released a Python SDK, installable via `pip install boxlite` on Python 3.10 or later, that boots a dedicated hardware-virtualized microVM per Box from standard OCI images such as `python:slim` so model-generated code runs isolated from the host. The Apache-2.0 runtime runs embedded microVMs inside the Python process, while the SDK's REST client (`Boxlite.rest`) targets a self-hosted `boxlite serve` instance or the managed BoxLite Cloud service for AI agents. The SDK adds `NetworkSpec` hostname allowlisting with DNS sinkholing and TLS SNI/HTTP Host inspection, plus `Secret` objects that pass placeholders like `<BOXLITE_SECRET:anthropic>` into the guest and swap in real credentials only for allowlisted hosts.", "body_md": "Install the BoxLite Python SDK with `pip install boxlite` (Python 3.10 or later), then run commands inside a local microVM using `async with boxlite.SimpleBox(image=\"python:slim\") as box:` and `await box.exec(...)`. The Apache-2.0 open-source runtime runs embedded microVMs directly in your Python process, while the SDK's REST client (`Boxlite.rest`) also lets you target self-hosted `boxlite serve` or BoxLite Cloud, the managed isolated compute service for AI agents.\n\nCalling `subprocess.run` executes model-generated code straight on your laptop or server. With BoxLite, your Python process embeds hardware virtualization and boots a dedicated microVM for each Box from standard OCI images like `python:slim`.\n\nYou need Python 3.10 or later:\n\n```\npip install boxlite\n```\n\nRunning Boxes locally requires CPU virtualization: Apple Silicon macOS 12+ (`Hypervisor.framework`), `/dev/kvm` on Linux, or KVM inside WSL2 on Windows ([our embedded sandbox guide](https://blog.boxlite.ai/embed-a-microvm-sandbox-in-your-agent-at-zero-extra-cost) covers the local architecture).\n\nIf your CI runner doesn't expose KVM, install `boxlite` anyway. You can point your client at a remote `boxlite serve` instance or [BoxLite Cloud](https://blog.boxlite.ai/one-sdk-one-microvm-runtime-from-laptop-to-boxlite-cloud).\n\nStart an `async with` block with an OCI image, then pass your command to `box.exec`:\n\n``` python\nimport asyncio\nimport boxlite\n\nasync def main():\n    async with boxlite.SimpleBox(image=\"python:slim\") as box:\n        result = await box.exec(\"python\", \"-c\", \"print('Hello from BoxLite')\")\n        print(result.stdout)\n\nasyncio.run(main())\n```\n\nOn the first run, BoxLite pulls `python:slim` and caches the OCI image layers under `~/.boxlite` so later runs skip the download. When the `async with` block exits, `SimpleBox` stops and removes that temporary Box for you.\n\nUntrusted code with plain environment variables and open outbound networking has an easy path to leak your keys.\n\nMount only the repository directory the task needs instead of exposing your home directory. Use `NetworkSpec(mode=\"disabled\")` for offline code runs, or `NetworkSpec(mode=\"enabled\", allow_net=[...])` so BoxLite resolves only allowlisted hostnames, sinkholes other DNS lookups to `0.0.0.0`, and inspects TLS SNI and HTTP `Host` headers.\n\nFor API keys, `Secret` keeps the real token out of the guest environment by passing a placeholder like `<BOXLITE_SECRET:anthropic>`. When the Box sends an outbound HTTPS request to an allowlisted host in `Secret(..., hosts=[...])`, the host proxy swaps in the real value.\n\n``` python\nimport asyncio\nimport os\nimport boxlite\n\nasync def main():\n    secret = boxlite.Secret(\n        name=\"anthropic\",\n        value=os.environ[\"ANTHROPIC_API_KEY\"],\n        hosts=[\"api.anthropic.com\"],\n    )\n    net = boxlite.NetworkSpec(\n        mode=\"enabled\",\n        allow_net=[\"api.anthropic.com\"],\n    )\n\n    async with boxlite.SimpleBox(\n        image=\"python:slim\",\n        network=net,\n        secrets=[secret],\n        volumes=[(\"/path/to/repo\", \"/workspace\", False)],\n        env=[(\"ANTHROPIC_API_KEY\", \"<BOXLITE_SECRET:anthropic>\")],\n    ) as box:\n        res = await box.exec(\"sh\", \"-c\", \"echo $ANTHROPIC_API_KEY\")\n        print(res.stdout)  # Prints only the placeholder <BOXLITE_SECRET:anthropic>\n\nasyncio.run(main())\n```\n\nEven if generated code runs `printenv`, it sees only the placeholder string (walked through in our [coding-agent isolation guide](https://blog.boxlite.ai/run-a-coding-agent-in-a-microvm-before-enabling-yolo-mode)). That doesn't make the sandbox unconditionally safe, though. Code inside the Box can still call endpoints on `hosts` using your injected credential, and passing `False` as the third item in the `volumes` tuple mounts `/workspace` read-write so the agent can edit repository files.\n\nWhen you move workloads off your laptop, the `Boxlite` factory selects between your local embedded runtime and a remote endpoint using `BOXLITE_REST_URL`:\n\n``` php\nimport os\nfrom boxlite import Boxlite, BoxliteRestOptions\n\ndef get_runtime() -> Boxlite:\n    if os.getenv(\"BOXLITE_REST_URL\"):\n        return Boxlite.rest(BoxliteRestOptions.from_env())\n    return Boxlite.default()\n```\n\nWith `BOXLITE_REST_URL` unset, `Boxlite.default()` runs the Apache-2.0 runtime embedded inside your process. Setting `BOXLITE_REST_URL` and credentials points `Boxlite.rest(...)` at a self-hosted `boxlite serve` instance you manage yourself or at BoxLite Cloud, where BoxLite manages the isolated compute infrastructure.\n\n`SimpleBox` removes its Box and virtual disk state when the context block exits. For longer-lived workflows or managed BoxLite Cloud instances, setting `auto_delete=0` keeps disk state intact after compute stops.\n\nKeep an eye on idle timeouts in BoxLite Cloud. Idle tracking for `auto_stop` measures activity at the box boundary, so long-running background processes inside a Box may not count as activity and can still be stopped.\n\nIf you don't want to wire raw shell commands into your agent loop, the SDK also includes `SkillBox` for calling agent skill packages (`async with boxlite.SkillBox(skills=[\"anthropics/skills\"]) as box:` and `await box.call(...)` with `CLAUDE_CODE_OAUTH_TOKEN`). Synchronous scripts can use `SyncBoxlite`, shown in `run_openclaw.py` under `examples/python/06_ai_agents/` in the BoxLite GitHub repository.", "url": "https://wpnews.pro/news/how-to-use-boxlite-with-python", "canonical_source": "https://blog.boxlite.ai/how-to-use-boxlite-with-python", "published_at": "2026-12-04 07:45:26.075421+00:00", "updated_at": "2026-12-04 07:45:28.502177+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-safety", "ai-infrastructure"], "entities": ["BoxLite", "BoxLite Cloud", "Python", "Anthropic", "Hypervisor.framework", "KVM", "WSL2"], "also_reported_by": [], "takeaway": "BoxLite's `Secret` passes a placeholder like `<BOXLITE_SECRET:anthropic>` into the guest environment and swaps in the real key only for hosts listed in `Secret(..., hosts=[...])`, so generated code running `printenv` sees just the placeholder.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/how-to-use-boxlite-with-python", "markdown": "https://wpnews.pro/news/how-to-use-boxlite-with-python.md", "text": "https://wpnews.pro/news/how-to-use-boxlite-with-python.txt", "jsonld": "https://wpnews.pro/news/how-to-use-boxlite-with-python.jsonld"}}