pydantic-ai-kosa8
Run a Pydantic AI agent's commands and file edits in a kosa8 Cloud microVM sandbox instead of on your machine.
Kosa8Sandbox is a capability that supplies the run's workspace. Compose it with capabilities that use a workspace, such as Coder, Shell and FileSystem from Pydantic AI Harness, or your own tools through ctx.workspace.
Install
pip install pydantic-ai-kosa8
Set KOSA8_API_KEY to a kosa8 Cloud API key (create one at app.kosa8.com). The Starter plan is free with no card and includes $10 of one-time kosa8 Cloud usage; paid plans start at $29 a month (pricing).
Quick start
from pydantic_ai import Agent
from pydantic_ai_harness.coder import Coder
from pydantic_ai_kosa8 import Kosa8Sandbox
agent = Agent('anthropic:claude-sonnet-4-5', capabilities=[Kosa8Sandbox(ttl_s=3600), Coder()])
result = agent.run_sync('Clone https://github.com/pydantic/pydantic-ai and summarize how capabilities work.')
print(result.output)
The sandbox is created on the first workspace operation. Commands start in /workspace (set working_dir= to change it).
Continue in the same sandbox
The sandbox's WorkspaceRef is in the message history and in result.workspace.ref. A later run with that history continues in the same sandbox, and kosa8 wakes it if it was asleep:
again = agent.run_sync('Now run the tests.', message_history=result.all_messages())
Or pass the ref yourself: agent.run_sync(..., workspace=Kosa8Sandbox().backend(ref)).
Clean up
Ending a run does not delete the sandbox. Delete it when you are done:
await Kosa8Sandbox().destroy(result.workspace.ref)
or give new sandboxes a lifetime with ttl_s (60 s to 90 days), after which kosa8 deletes them, and sleep_after_s to put an idle one to sleep.
Options
| Option | Default | |
|---|---|---|
api_key |
KOSA8_API_KEY |
kosa8 Cloud API key |
base_url |
KOSA8_CLOUD_URL or https://api.kosa8.com |
API address |
client |
one per run | an AsyncKosa8Client to share across runs (you close it) |
template |
account default | kosa8 template for a new sandbox |
region |
chosen by kosa8 | region for a new sandbox |
ttl_s |
none | lifetime of a new sandbox |
sleep_after_s |
account default | idle seconds before a new sandbox sleeps |
working_dir |
/workspace |
where commands start; created if missing |
env |
none | environment variables for every command |
Errors from kosa8 Cloud are typed: Kosa8PaymentRequiredError (402, with upgrade_url), Kosa8OrgFrozenError (423), Kosa8RateLimitError (429) and Kosa8Error for the rest. A sandbox that was deleted raises Pydantic AI's WorkspaceUnavailableError.
How it works
Kosa8WorkspaceBackend implements Pydantic AI's WorkspaceBackend and SupportsCommands protocols. Each command is one exec request to the kosa8 Cloud API, run by a small POSIX sh wrapper that:
- starts the command in a process group of its own, with stdin at EOF;
- returns as soon as the command exits, even if a background child keeps its output open;
- enforces
timeout=inside the sandbox and kills the process group when it fires, or when the call is cancelled; - returns stdout and stderr base64-encoded, so any bytes survive (undecodable bytes become U+FFFD), and reads large output back in chunks.
File operations (read, write, stat, list, remove, realpath) come from Pydantic AI's Workspace, which derives them through the sandbox's shell, so path handling and error types match Pydantic AI's other backends. The sandbox image needs sh, base64, dd, find and the other coreutils, which kosa8's templates include.
tests/test_harness.py runs an Agent with Pydantic AI Harness's Shell and FileSystem capabilities on top of Kosa8Sandbox, and checks that their write_file, run_command and read_file calls land in the sandbox. It runs against the bundled fake kosa8 API, and against kosa8 Cloud in the live tier.
The backend passes Pydantic AI's WorkspaceBackendSuite conformance suite (39 of 40 rules run and pass against the bundled fake kosa8 API; the 40th is for the optional SupportsRealpath protocol, which this backend leaves to the shell).
Security
Each kosa8 sandbox is a microVM. kosa8's SOC 2 audit is in progress; kosa8 does not have a SOC 2 report yet. See kosa8.com/security.
Development
uv venv && uv pip install -e . pytest ruff mypy
pytest # unit tests and the conformance suite, against a local fake API
ruff check . && mypy
The live tier runs the same conformance suite against kosa8 Cloud. It creates sandboxes (each with a 15-minute lifetime as a backstop) and deletes them:
PYDANTIC_AI_KOSA8_LIVE=1 KOSA8_API_KEY=... pytest -m kosa8_live
pydantic_ai_kosa8/_kosa8.py is the kosa8 Cloud client shared, unchanged, with langchain-kosa8, inspect-kosa8 and harbor-kosa8.
License
MIT
Metadata
Release files for pydantic-ai-kosa8 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_ai_kosa8-0.1.0.tar.gz | 35.9 kB | Without provenance | Details for pydantic_ai_kosa8-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_ai_kosa8-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pydantic_ai_kosa8-0.1.0-py3-none-any.whl |
Total release size: 67.5 kB
Release files / pydantic_ai_kosa8-0.1.0.tar.gz
| Download URL | pydantic_ai_kosa8-0.1.0.tar.gz |
|---|---|
| Size | 35.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5c24f9c57d326ab029b20982901813f4ffc7b9e625424c6f6e015d9a9f08ad8
|
|
BLAKE2b-256 checksum How to use checksums |
255576ef9bf2f1e0ce075d53ffc10a2779a0ec06522ec8be22089c88e412c1c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|
Release files / pydantic_ai_kosa8-0.1.0-py3-none-any.whl
| Download URL | pydantic_ai_kosa8-0.1.0-py3-none-any.whl |
|---|---|
| Size | 31.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a63a02442bfee47baf9a206f831fcfcd0997845b7651aaf30e44b00b590505a9
|
|
BLAKE2b-256 checksum How to use checksums |
a14f00472a3218550eea9cf84d702294bd167332261cb2dde757266782d3f56a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|
Security for pydantic-ai-kosa8 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.