opsen
opsen

The Python client

Sessions, files, exec and cost — the whole surface is small on purpose.

pip install opsen

A session

from opsen_client import Client

opsen = Client("osk_...")                    # or set OPSEN_API_KEY

s = opsen.session(
    task_id="invoice-extract",   # groups runs; one line on /costs
    budget_usd=2.00,             # refused mid-run, not reported after
    runtime="auto",              # or "e2b", "modal", "local"
    labels={"customer": "acme"}, # anything you want to group by
)

Use it as a context manager and the machine stops when the block ends:

with opsen.session(task_id="x") as s:
    s.exec("echo hello")

Files and commands

s.fs.write("agent.py", b"print(1)")
data = s.fs.read("out.json")
out = s.exec("python3 agent.py", timeout=600)

out["stdout"], out["stderr"], out["exit_code"]

Cost

c = s.cost()
c.total_usd, c.compute_usd, c.tokens_usd, c.tools_usd
c.calls, c.input_tokens, c.output_tokens
c.per_call_usd()

Lifecycle

s.suspend()          # stop billing compute, keep the filesystem
s.resume()
s.kill()             # release the machine
s.refresh()          # re-read state from the control plane
s.host(3000)         # a URL reaching a port, where the runtime supports it
s.retag("new-task")  # move this run's spend to another task id

Account-level

opsen.sessions()              # list
opsen.get_session(sid)
opsen.stuck_sessions()        # running long with nothing to show for it
opsen.costs(group_by="labels.customer")
opsen.task_cost("invoice-extract")
opsen.events(since=...)
opsen.provider_keys()
opsen.add_provider_key("anthropic", "sk-ant-...")

See the client reference for signatures and Errors for what is raised and which failures are worth retrying.