Metadata-Version: 2.5
Name: smolagents-kosa8
Version: 0.1.0
Summary: kosa8 Cloud microVM sandboxes as a remote Python executor for Hugging Face smolagents
Project-URL: Homepage, https://kosa8.com
Project-URL: Documentation, https://github.com/kosa8ai/smolagents-kosa8#readme
Project-URL: Repository, https://github.com/kosa8ai/smolagents-kosa8
Project-URL: Issues, https://github.com/kosa8ai/smolagents-kosa8/issues
Author: kosa8
License: MIT
License-File: LICENSE
Keywords: agents,code-execution,kosa8,microvm,sandbox,smolagents
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: <4.0,>=3.10
Requires-Dist: kosa8<1,>=0.12.0
Requires-Dist: smolagents<2,>=1.26
Description-Content-Type: text/markdown

# smolagents-kosa8

**[kosa8](https://kosa8.com): Zero Trust cybersecurity infrastructure for AI agents.** Every agent sealed in its own microVM. Every action verified and recorded.

Run a [smolagents](https://github.com/huggingface/smolagents) `CodeAgent`'s Python actions in a [kosa8 Cloud](https://kosa8.com) microVM sandbox instead of on your machine.

`Kosa8Executor` is a smolagents remote Python executor, built on `RemotePythonExecutor` like the E2B, Modal and Blaxel executors. The agent and the model stay on your machine; each code step runs in the sandbox's long-lived Python process. Tested against `smolagents` 1.26.

## Install

```bash
pip install smolagents-kosa8
```

Set `KOSA8_API_KEY` to a kosa8 Cloud API key (create one at [app.kosa8.com](https://app.kosa8.com), or run `kosa8 login`). The Starter plan is free, no card needed; plans are at [kosa8.com/pricing](https://kosa8.com/pricing/).

## Example

```python
from smolagents import CodeAgent, InferenceClientModel
from smolagents_kosa8 import Kosa8Executor

with Kosa8Executor(additional_imports=["numpy"]) as executor:
    agent = CodeAgent(
        tools=[],
        model=InferenceClientModel(),
        executor=executor,
        additional_authorized_imports=["numpy"],
    )
    print(agent.run("Use numpy to compute the mean of the first 20 primes."))
```

Leaving the `with` block (or calling `executor.cleanup()`) deletes the sandbox. A new sandbox also gets a one hour lifetime by default (`ttl_s`), so a crashed process cannot leave it running.

Pass the same packages to `additional_imports` (installed in the sandbox) and to the agent's `additional_authorized_imports` (allowed in the agent's code).

## What works

| smolagents feature | kosa8 |
|---|---|
| State between steps (variables, imports, functions) | yes: one Python process per sandbox |
| `final_answer` | yes, with smolagents' safe JSON encoding; pickle only with `allow_pickle=True` |
| Tools (`send_tools`), including their pip requirements | yes |
| Agent state (`send_variables`) | yes |
| Images (a `PIL.Image` as a step's last expression) | yes, returned as a `PIL.Image` |
| Several runs of one agent, with `reset=False` or not | yes |
| Managed agents | no: smolagents does not support them with remote executors |

Packages install with `pip` through a shell command in the sandbox, so a long install is not held to the code step time limit. Import names map to their distribution (`PIL` to `pillow`, `sklearn` to `scikit-learn`), and standard library modules are skipped.

## Options

`Kosa8Executor(additional_imports=None, logger=None, allow_pickle=False, **options)`:

| Option | Default | |
|---|---|---|
| `api_key` | `KOSA8_API_KEY`, then the key `kosa8 login` saved | kosa8 Cloud API key |
| `base_url` | `https://api.kosa8.com` | kosa8 Cloud API address |
| `cloud` | none | a ready `kosa8.Cloud` client |
| `sandbox` | none | an existing persistent sandbox (or its id) to use; the executor never deletes a sandbox it did not create |
| `template` | `"python"` | kosa8 template for a new sandbox |
| `size` | template size | vCPU x GiB: `0.25x0.5`, `0.5x1`, `1x2`, `2x4`, `4x8`, `8x16` or `16x32` |
| `ttl_s` | `3600` | lifetime of a new sandbox, 60 s to 90 days; `None` for none |
| `labels` | none | labels for a new sandbox (it also gets `integration=smolagents`) |
| `create_kwargs` | none | anything else `kosa8.Cloud().sandboxes.create` takes: `env`, `region`, `egress`, `secrets`, ... |
| `timeout_s` | kosa8's limit | time limit for one code step |
| `install_timeout_s` | `600` | time limit for one `pip install` |

For example, a sandbox that can reach only PyPI:

```python
Kosa8Executor(create_kwargs={"egress": {"internet": False, "presets": ["pypi"]}})
```

## `executor_type="kosa8"`

smolagents plans an executor registry through the `smolagents.executors` entry point group ([huggingface/smolagents#2722](https://github.com/huggingface/smolagents/issues/2722)). This package registers `kosa8` there, so once smolagents resolves it, `CodeAgent(..., executor_type="kosa8", executor_kwargs={...})` will work too. Until then, pass the executor with `executor=`.

## Tests

```bash
uv run --group test pytest                                      # unit tests, against a fake kosa8 Cloud
KOSA8_API_KEY=... uv run --group test pytest tests/test_live.py   # live tier, on kosa8 Cloud
```

The unit tests cover sandbox creation and cleanup, state between steps, errors, `final_answer` (and pickle refused by default), tools, variables, images, installs and their failures, and `CodeAgent` runs with a scripted model. The live tier (skipped without `KOSA8_API_KEY`) runs a multi-step `CodeAgent` with a tool and a scripted model on a real sandbox, a second run on the same agent, a package install, an image result and an error, and checks the sandbox is deleted afterwards. No LLM is called.

## License

MIT
