kosa8 / Integrations / OpenAI Agents SDK

Integration · OpenAI Agents SDK · Last checked: November 2026

Run the OpenAI Agents SDK in a microVM sandbox

openai-agents-kosa8 is a sandbox client for the OpenAI Agents SDK's sandbox feature. Pass Kosa8SandboxClient to SandboxRunConfig and the runner creates, uses and deletes a kosa8 Cloud microVM sandbox for each run, so the commands and file edits the model asks for run there, not on your machine. This is the recommended way to use kosa8 with the Agents SDK. The function tools in kosa8[openai-agents] remain as the alternative, for the kosa8 daemon on your own machine and for SDK releases the client does not support.

01How it works

Kosa8SandboxClient follows the SDK's guide to building a sandbox provider. The agent loop and the model calls stay in your Python process; the SDK hands the sandbox's work (exec, read, write, ls, mkdir, rm, mv, extract, apply_patch, workspace snapshots and exposed ports) to the client, and the client runs it in a kosa8 Cloud sandbox over the kosa8 API.

Each command is started by a small POSIX sh wrapper in a process group of its own, with stdin at EOF. A watchdog inside the sandbox enforces timeout and kills the process group; cancelling the call kills it too. Output comes back base64-encoded, so any bytes survive, and a command may run longer than one 90 s buffered exec. Files go through the kosa8 files API. Errors from kosa8 Cloud become the SDK's sandbox errors (WorkspaceStartError, ExecTransportError, ExecTimeoutError and so on) with retryable set, and with the kosa8 status, code, request_id and, for a payment refusal, upgrade_url in context.

02Steps

  1. Install the package. It brings openai-agents 0.26 and the kosa8 Python SDK with it.

  2. Set your keys. KOSA8_API_KEY is a kosa8 Cloud API key from app.kosa8.com; without it, the client uses the key kosa8 login saved. The Agents SDK reads your OpenAI key in this shell, outside the sandbox.

  3. Write the agent. A SandboxAgent with the Shell capability, and a run config that names the kosa8 client. ttl_s=3600 has kosa8 delete the sandbox an hour after it starts.

    coder.py
    import asyncio
     
    from agents import RunConfig, Runner
    from agents.sandbox import SandboxAgent, SandboxRunConfig
    from agents.sandbox.capabilities import Shell
    from openai_agents_kosa8 import Kosa8SandboxClient, Kosa8SandboxClientOptions
     
     
    async def main() -> None:
        agent = SandboxAgent(name="coder", instructions="Work in the sandbox.", capabilities=[Shell()])
        async with Kosa8SandboxClient() as client:
            sandbox = SandboxRunConfig(client=client, options=Kosa8SandboxClientOptions(ttl_s=3600))
            result = await Runner.run(agent, "Write fizzbuzz.py, run it, show the output.", run_config=RunConfig(sandbox=sandbox))
            print(result.final_output)
     
     
    asyncio.run(main())
  4. Run it. The runner creates the sandbox when the run starts and deletes it when the run ends.

  5. Keep one sandbox across several runs. Create the session yourself; the runner uses it but does not close it. On the way out of async with, the session persists the workspace (if a snapshot is configured) and deletes the sandbox.

    coder.py
    session = await client.create(options=Kosa8SandboxClientOptions(ttl_s=3600))
    async with session:
        config = RunConfig(sandbox=SandboxRunConfig(session=session))
        await Runner.run(agent, "Clone the repo.", run_config=config)
        await Runner.run(agent, "Now run the tests.", run_config=config)
  6. Pause and resume. A run that stops for a tool approval saves the sandbox id and how it was created in its RunState, never the API key or environment values. Resuming reconnects to the same sandbox if it still exists, waking it if it sleeps; otherwise it starts a replacement and the runner restores the workspace from the run's snapshot. With on_exit="sleep" the sandbox sleeps between the pause and the resume, and kosa8 bills only its sleep storage meanwhile.

03Options

Kosa8SandboxClientOptions refuses unknown fields. Once the package is imported, BaseSandboxClientOptions.parse({"type": "kosa8", ...}) returns it.

OptionDefaultWhat it does
templateaccount defaultThe kosa8 template for the sandbox
imagenoneAn OCI image (a sized sandbox)
sizetemplate sizevCPU x GiB: 0.25x0.5, 0.5x1, 1x2, 2x4, 4x8, 8x16 or 16x32
regionchosen by kosa8Where the sandbox runs
ttl_snonekosa8 deletes the sandbox this many seconds after it starts (60 s to 90 days)
sleep_after_saccount settingIdle seconds before the sandbox sleeps; it wakes on its next command
persistentoffRun every command in one long-lived container, so the whole filesystem and background processes last between commands
envnoneEnvironment for every command, kept on the sandbox, not in session state
internetonFalse turns off the open internet
egress_allownoneHosts the sandbox may reach
egress_presetsnoneNamed host sets, such as pypi or npm
secretsnoneNames of org secrets the sandbox holds, set in the kosa8 dashboard or CLI
labels, namenoneStored on the sandbox
exposed_portsnonePorts session.resolve_exposed_port(port) may open as kosa8 previews, an https URL each
public_portsFalseOpen those previews as public URLs instead of signed links
on_exit"delete"What shutdown does with the sandbox: "delete", "sleep" or "keep"
workspace_persistence"tar""tar" archives the workspace root; "snapshot" takes a kosa8 snapshot of the whole sandbox and restores into a new sandbox started from it
run_dir/tmp/.kosa8-openai-agentsWhere the command wrapper keeps its run files

Kosa8SandboxClient(api_key=None, base_url=None, cloud=None) takes the key (default KOSA8_API_KEY, then the key kosa8 login saved), the API address (default https://api.kosa8.com), or a ready kosa8.AsyncCloud. The client opens its own HTTP pool; close it with await client.aclose() or async with.

04What is supported, and the limits

SDK featureOn kosa8
exec with timeoutYes; output is byte-exact
read, write, read_boundedYes, through the kosa8 files API after the SDK's remote path check
ls, mkdir, rm, mv, extract, apply_patchYes (the SDK's shell implementations)
Manifest files, directories, local files, git repos, environmentYes
Workspace snapshotsYes: a tar, or a kosa8 snapshot
Exposed portsYes, as kosa8 previews
Resume from saved stateYes
PTY sessions (write_stdin)No: the Shell capability offers only exec_command
Another user (user=, manifest users and groups)No: refused with ConfigurationError
Manifest mounts (S3, GCS and the like)No: refused with ConfigurationError before any sandbox is created
  • Only /workspace persists between commands unless you set persistent=True. Keep the manifest root under /workspace, the default.
  • Output: each output stream keeps its first 16 MiB.
  • Snapshots are kept: "snapshot" persistence makes a kosa8 snapshot each time the workspace is persisted, and kosa8 keeps it until you delete it.
  • A replacement sandbox on resume gets the saved creation settings but not the options' env values, which are never saved.
  • The image needs a POSIX sh, base64, dd, tar and the usual coreutils, which kosa8's templates include.
  • SDK release: the package needs openai-agents 0.26 (>=0.26,<0.27); with another release, use the function tools below.

05The alternative: function tools on your machine

The kosa8 Python SDK also ships function tools for the Agents SDK. They suit an agent whose sandbox should be a microVM from the kosa8 daemon on your own Mac, Linux or Windows machine, and an SDK release openai-agents-kosa8 does not support. sandbox_tools() binds seven tools (run code, a shell, read, write and list files, git, and preview links) to one kosa8 sandbox.

Kosa8Client talks to the kosa8 daemon on your computer, over ~/.kosa8/run/kosa8.sock, or to a remote worker named by KOSA8_HOST and KOSA8_TOKEN. A tool never raises a kosa8 error into the run. It returns text starting with error:, with the daemon's reason and, when it has one, a what to do: line, so the model can change course. shell calls share one session, so cd and export carry from one call to the next. Long output is cut to 20,000 characters, keeping the start and the end.

  1. Install kosa8 and start its daemon. Sandboxes run on an Apple silicon Mac, an x86-64 Linux machine with KVM, or Windows 11 through WSL2 (preview). kosa8 doctor reports anything missing.

  2. Install the SDK with the Agents SDK extra. The extra installs openai-agents.

  3. Write the agent. The sandbox is created first, with a network limited to the Python package index, and handed to the agent as tools.

    coder_local.py
    from agents import Agent, Runner
    from kosa8 import Kosa8Client
    from kosa8.integrations.openai_agents import sandbox_tools
     
    client = Kosa8Client()   # the kosa8 daemon on this machine
    sbx = client.create_sandbox(
        name="coder-1",
        ephemeral=True,          # deleted when it stops
        auto_stop_min=15,        # stops after 15 idle minutes
        egress_allow=["pypi.org", "files.pythonhosted.org"],
    )
    sid = sbx["sandbox"]["id"]
     
    agent = Agent(
        name="coder",
        instructions="You work in a Linux sandbox. Keep your files in /workspace.",
        tools=sandbox_tools(client, sid),
    )
    try:
        result = Runner.run_sync(agent, "Write fizzbuzz.py, run it for 1..15 and show the output.")
        print(result.final_output)
    finally:
        client.stop_sandbox(sid)  # ephemeral, so stopping deletes it
  4. Give it fewer tools, and check its network. Pick a subset of run_code, shell, read_file, write_file, list_files, git and preview. kosa8 sandbox egress shows a running sandbox's network rule and how many requests it refused.

    coder_local.py
    tools=sandbox_tools(client, sid, tools=["shell", "read_file", "write_file"])
SettingWhereDefaultWhat it does
egress_allow=[...]create_sandboxthe org policy's defaultOnly these hosts are reachable
egress="deny"create_sandboxNo network at all, DNS included
ephemeral=Truecreate_sandboxFalseDelete the sandbox whenever it stops
auto_stop_min, ttl_mincreate_sandboxnoneStop after that many idle minutes, or that long after creation
cpus, mem_mb, disk_gbcreate_sandboxthe daemon'sThe microVM's size
tools=[...]sandbox_toolsall sevenWhich tools the agent gets
session=sandbox_tools"agent"The shell session the tools share
approval=sandbox_toolsnoneA signed approval sent with every call
name_prefix=sandbox_tools"sandbox_"The prefix of each tool's name

06What this protects against, and what it does not

What it protects against

  • Model-written commands and code reaching your files, SSH keys or other projects: with Kosa8SandboxClient they run in a kosa8 Cloud microVM, not on your machine; with the function tools, the local microVM sees none of your disk unless you mount a folder.
  • Calls to hosts you did not allow, when you set internet=False, egress_allow or egress_presets. On the local route, egress_allow and egress="deny" are enforced on the host, outside the VM, so root inside cannot lift them.
  • Your OpenAI key leaking from the sandbox: the model is called from your process, so the key is not there to read unless you pass it in.
  • Leftover state between runs: by default the runner deletes the sandbox when the run ends, and ttl_s bounds a sandbox that is never resumed.

What it does not

  • The agent loop, the model calls and any Python function tools of your own: those run in your process, not in the VM.
  • What the agent sends to a host you allowed. An allowed host that accepts uploads is still a way out.
  • Values you give the sandbox in env or secrets: commands in it can read them.
  • A folder you mount on the local route: the agent can change anything in it.

kosa8 limits what a hijacked agent can reach; it does not detect or prevent prompt injection. SOC 2 Type II is in progress; there is no SOC 2 report and no external penetration test yet.

openai-agents-kosa8 passed its 12 live tests on kosa8 Cloud: exec, environment from the options and the manifest, a file round trip, a 9 MiB file, 3 MiB of output, a timeout, a command longer than one buffered exec, a workspace restored into a second sandbox, egress on, off and allow-listed, a Runner run, kosa8 snapshot persistence and an exposed port. The Runner test uses a scripted model, not a real one. The function tools are tested in the kosa8 SDK's own suite against a mock daemon.

07Questions

Which kosa8 package should I use with the OpenAI Agents SDK?

openai-agents-kosa8, when you use the SDK's sandbox feature with openai-agents 0.26 and a kosa8 Cloud API key: pass Kosa8SandboxClient to SandboxRunConfig and each run gets a kosa8 Cloud microVM. Use the function tools in kosa8[openai-agents] for a sandbox from the kosa8 daemon on your own machine, or with another SDK release.

Does the OpenAI Agents SDK itself run inside the sandbox?

No. The agent loop runs in your Python process and calls the model from there. With Kosa8SandboxClient, the sandbox's commands and file edits run in a kosa8 Cloud microVM; with sandbox_tools(), the tool calls run in a microVM from the kosa8 daemon.

Can one sandbox last across several runs?

Yes. Create the session yourself with client.create() and pass it as SandboxRunConfig(session=session); the runner uses it but does not close it. A run paused for a tool approval can resume in the same sandbox if it still exists, or in a replacement restored from the run's snapshot.

What does Kosa8SandboxClient not support?

PTY sessions (write_stdin), running as another user, and manifest mounts such as S3 or GCS; the last two are refused with ConfigurationError. Without persistent=True, only /workspace is promised to persist between commands.

Which tools does sandbox_tools() give the agent?

Seven function tools: sandbox_run_code, sandbox_shell, sandbox_read_file, sandbox_write_file, sandbox_list_files, sandbox_git and sandbox_preview. Pass tools= with a list such as ["shell", "read_file"] to give fewer.

Can I use the SDK's DockerSandboxClient instead?

Yes. Set DOCKER_HOST to unix://$HOME/.kosa8/run/kosa8.sock and the SDK's Docker sandbox becomes a container in kosa8's shared engine VM on your machine. Kosa8SandboxClient and the function tools give each agent a microVM of its own; the Docker route shares one VM between containers.

Does this stop prompt injection?

No. kosa8 limits what an injected instruction can reach: the sandbox's files, its allowed hosts and its lifetime. It does not detect or block the injection itself.

08Get started

The reference for this page, with every option, is in the kosa8 docs: OpenAI Agents SDK. The package source is on GitHub at kosa8ai/openai-agents-kosa8, and the package is on PyPI as openai-agents-kosa8.

To try a kosa8 sandbox without installing anything or making an account, kosa8.com/try runs code in a fresh sandbox with no outbound network: 15 minutes of run time free per 24 hours, each run up to 60 s.

Try it free See pricing

Try free with no account. Starter is free with no card; plans from $29 a month.

Last checked: November 2026 · Updated 2026-11-29