Skip to main content

Krysta NoA — Python SDK

logo

Krysta NoA lets you execute AI-agent-generated code safely. Every run happens inside an isolated Docker container: no network access, capped memory, read-only filesystem, non-root user with live streaming output and automatic safety checks.

Installation

pip install krysta

Quickstart

import asyncio
from krysta.noa import Noa

async def main():
    async with Noa() as client:
        async for event in client.execute(language="python", code="print('hello world')"):
            print(event)

asyncio.run(main())

Core concepts

Noa

The main client. Connects to the Krysta execution gateway and streams results back over SSE.

from krysta.noa import Noa

async with Noa() as client:
    pass

By default, connects to the hosted Krysta gateway. To use your own gateway (self-hosted), pass gateway_url:

async with Noa(gateway_url="https://your-gateway.example.com") as client:
    pass

execute()

Runs a piece of code inside the sandbox. It can be used in two ways:

  1. As an async generator (iterating using async for) to stream runtime events chunk-by-chunk.
  2. Awaited directly to get a complete ExecutionTrace containing resolved stdout, exit code, rules, and durations.
async def execute(
    language: str = "python",
    code: str = None,
    code_path: str = None,
    timeout_ms: int = 10000,
    session_id: str = None,
)

As an Async Generator

async with Noa() as client:
    async for event in client.execute(language="python", code="print('hello')"):
        print(event)

As an Awaitable (Trace Resolution)

async with Noa() as client:
    trace = await client.execute(language="python", code="print('hello')")
    print(trace.stdout_lines)
    print(trace.duration_ms)

Parameters

  • language — "python" or "javascript"
  • code — a string of source code to run
  • code_path — alternatively, a path to a local file whose contents will be sent and run. Max 256KB.
  • timeout_ms — max execution time before the process is killed (default 10000ms, max 60000ms)
  • session_id — if set, gives this run a persistent workspace at /workspace shared across calls with the same session_id. Without it, the sandbox is stateless and the filesystem is locked down.

Either code or code_path must be provided.

spawn() — stateful sessions

For multi-step workflows (an agent writing code, testing it, fixing it, rerunning), use spawn() to get a sandbox tied to a persistent session:

from krysta.noa import spawn

async with spawn(runtime="python", session_id="agent_turn_1") as sandbox:
    async for event in sandbox.execute(code="with open('data.txt','w') as f: f.write('hello')"):
        print(event)

    async for event in sandbox.execute(code="with open('data.txt') as f: print(f.read())"):
        print(event)

Within a session, filesystem access is allowed but confined to /workspace inside the container nothing outside it is reachable, and network access remains disabled regardless of session state.

test_agent()

Given a file containing one or more functions, run a list of test cases against it inside the sandbox:

async with Noa() as client:
    results = await client.test_agent(
        code_path="agent_output.py",
        test_cases=[
            {"name": "normal case", "input": "calculate_stats([1,2,3,4,5])"},
            {"name": "empty list", "input": "calculate_stats([])"},
        ]
    )
    for r in results:
        print(r["name"], "->", r["status"])

Each test case's input is a Python expression calling a function defined in code_path. Results report "pass" or "fail" per case, along with stdout and rule results.


Error Handling

The SDK raises custom exceptions from the krysta.exceptions module:

  • KrystaError — Base exception class.
  • KrystaTimeoutError — Raised when streaming telemetry channel times out.
  • KrystaGatewayError — Raised when gateway rejects requests, outputs non-JSON, or SSE connection fails.
  • KrystaSandboxError — Base sandbox error.
from krysta.noa import Noa
from krysta.exceptions import KrystaGatewayError, KrystaTimeoutError

async def main():
    try:
        async with Noa() as client:
            trace = await client.execute(language="python", code="print('hello')")
            print(trace.stdout_lines)
    except KrystaGatewayError as e:
        print(e)
    except KrystaTimeoutError as e:
        print(e)

Streaming events

Every execute() call yields a sequence of event dicts:

{"type": "system" | "stdout" | "stderr" | "rules" | "done" | "timeout" | "error", "text": "...", "timestamp": 1234567890}
  • system — lifecycle markers, e.g. "EXECUTION_STARTED"
  • stdout / stderr — output lines as they're produced, streamed live
  • rules — emitted once near the end; text is a JSON string of rule results (see below)
  • done — the run finished and exited cleanly (exit code 0)
  • timeout — the run exceeded timeout_ms and was killed
  • error — the run failed (non-zero exit, blocked before running, or an infrastructure error)

Safety rules

Every run is checked against a fixed set of rules. Results are returned in the rules event as a JSON list:

[{"rule": "NoNetworkCallsRule", "result": "PASS", "reason": "...", "category": "security"}, ...]

Security rules (these determine whether a run is safe):

Rule What it checks
NoNetworkCallsRule No outbound network calls were made or attempted
ExitCodeZeroRule The process exited cleanly (code 0)
MemoryLimitRule Memory stayed under the 128MB container limit
NoFilesystemAccessRule No filesystem access outside /workspace (or none at all, if no session)

Optional / informational rules (don't affect whether a run is "safe", just describe the output):

Rule What it checks
ValidJsonRule Whether stdout is a single valid JSON document

PyPI

https://pypi.org/project/krysta/

Metadata

Release files for krysta 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for krysta 1.0.9
File Size Uploaded Provenance Actions
krysta-1.0.9.tar.gz 16.6 kB Without provenance Details for krysta-1.0.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for krysta 1.0.9
File Interpreter ABI Platform Actions
krysta-1.0.9-py3-none-any.whl Python 3 none any Details for krysta-1.0.9-py3-none-any.whl

Total release size: 32.6 kB

Release files / krysta-1.0.9.tar.gz

Download URL krysta-1.0.9.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d1fcbff115b56e2c7a0e46628c2d26625dbb838afde4caf8542553fe50870a64
BLAKE2b-256 checksum
How to use checksums
f68bf73b86218b5d4a3b5d28781aec619129382c8288c7c0eef320666a1cd80a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release files / krysta-1.0.9-py3-none-any.whl

Download URL krysta-1.0.9-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19dbc091f8a90c4d47047783561fce7b8b796eb65f23550d1c54325aa34e5234
BLAKE2b-256 checksum
How to use checksums
c6cf669ba0dcd44debc0431b8da504118caecb76a45856ea1a9934f98deee4cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Security for krysta 1.0.9

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page