Remote API¶
Client and service for sandboxes that live in another process. See Remote Sandboxes for the concepts and the security model.
RemoteSandbox¶
Needs the remote extra (httpx).
pydantic_ai_backends.remote.client.RemoteSandbox
¶
Bases: BaseSandbox
Sandbox backed by a sandboxd service over HTTP.
Implements the same synchronous surface as
:class:~pydantic_ai_backends.DockerSandbox, so it drops into
:class:~pydantic_ai_backends.SessionManager or a console toolset
unchanged. Every file operation is served by the remote side rather than
derived from shell commands, so read of a large file transfers only the
requested slice.
The session — and therefore the container behind it — is opened on the first
operation, not on construction. An agent granted a sandbox it never uses
costs nothing: no session, no container, not even a round trip. Call
:meth:start explicitly only to pre-warm one.
Failures are returned, never raised: a transport error surfaces the same way
a missing file does (b"", [], an Error: ... string), matching
LocalBackend and DockerSandbox. A tool call must not take down an
agent run because a socket blipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_url
|
str
|
Base URL of the service, e.g. |
'http://localhost:8080'
|
token
|
str
|
Service token, sent as the |
''
|
session_id
|
str | None
|
Identifier to open or re-attach to. Generated when omitted. |
None
|
reuse
|
bool
|
Attach to the session under |
False
|
runtime
|
str | None
|
Alias of a server-allowed runtime. |
None
|
tenant
|
str | None
|
Whoever this sandbox is for, against which the service applies its per-tenant session ceiling. A capacity label only — it grants nothing and authorizes nothing. |
None
|
timeout
|
float
|
Request timeout in seconds for operations without their own. |
DEFAULT_TIMEOUT_SECONDS
|
client
|
Client | None
|
Pre-built |
None
|
Example
Source code in src/pydantic_ai_backends/remote/client.py
| Python | |
|---|---|
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
session_id
property
¶
Alias for the sandbox id, for parity with DockerSandbox.
__init__(service_url='http://localhost:8080', *, token='', session_id=None, runtime=None, tenant=None, reuse=False, timeout=DEFAULT_TIMEOUT_SECONDS, client=None)
¶
Source code in src/pydantic_ai_backends/remote/client.py
start()
¶
Open the remote session, or attach to it when reuse is set.
Idempotent within one instance: repeated calls do nothing once a session
is open. With reuse, a new instance naming an existing session id
attaches to it rather than failing — which is what makes a sandbox
outlive the run that created it.
Usable again after :meth:stop, which opens a fresh session — the same
way DockerSandbox starts a new container after being stopped.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the service refuses to open a session. Unlike the file operations this does raise, because a caller that cannot get a sandbox at all needs to know why. |
Source code in src/pydantic_ai_backends/remote/client.py
stop(purge=False)
¶
Delete the remote session and close an owned HTTP client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
purge
|
bool
|
Also discard what the session accumulated — its container's filesystem and its host workspace. Leave it off to end a turn while keeping the files for the next one; turn it on when the thing the session belonged to is gone for good. |
False
|
Source code in src/pydantic_ai_backends/remote/client.py
is_alive()
¶
Whether the remote session exists and its sandbox is running.
Source code in src/pydantic_ai_backends/remote/client.py
resource_usage()
¶
Sample the remote sandbox's resource usage.
Source code in src/pydantic_ai_backends/remote/client.py
execute(command, timeout=None)
¶
Run a command in the remote sandbox.
Without an explicit timeout the command runs under the service's
ceiling, and the transport waits that long plus slack — the two are one
contract, and the client giving up first turned a slow-but-successful
command into a reported outage.
Source code in src/pydantic_ai_backends/remote/client.py
read(path, offset=0, limit=2000)
¶
Read a slice of a text file.
Source code in src/pydantic_ai_backends/remote/client.py
read_bytes(path)
¶
Read a whole file as bytes, or b"" when unreadable.
Source code in src/pydantic_ai_backends/remote/client.py
write(path, content)
¶
Write a file.
Source code in src/pydantic_ai_backends/remote/client.py
edit(path, old_string, new_string, replace_all=False)
¶
Replace a string inside a file.
Source code in src/pydantic_ai_backends/remote/client.py
exists(path)
¶
Whether the path is a regular file.
Source code in src/pydantic_ai_backends/remote/client.py
| Python | |
|---|---|
ls_info(path)
¶
List one directory, or [] when it cannot be listed.
Source code in src/pydantic_ai_backends/remote/client.py
glob_info(pattern, path='/')
¶
Match files by glob, or [] when the search cannot be run.
Source code in src/pydantic_ai_backends/remote/client.py
| Python | |
|---|---|
grep_raw(pattern, path=None, glob=None, ignore_hidden=True)
¶
Search file contents, returning matches or an error string.
Source code in src/pydantic_ai_backends/remote/client.py
WorkspaceArchive¶
Read-only view of the files sessions left behind, with no sandbox running. Needs
the remote extra.
pydantic_ai_backends.remote.client.WorkspaceArchive
¶
Read-only view of the files sessions left behind.
Reads the service's host volume directly, so a session reaped long ago is still browsable and listing a conversation's files costs no container start.
Unlike :class:RemoteSandbox, this raises rather than degrading. Nothing
here is in an agent's tool path — the caller is an application answering a
user who asked to see some files, and it needs to tell "there are none" apart
from "the service is misconfigured".
Requires the service to be running with SandboxdConfig.workspace_root set,
and the service token: a reaped session has no token of its own left, and the
intended caller is a backend applying its own authorization first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_url
|
str
|
Base URL of the service. Ignored when |
'http://localhost:8080'
|
token
|
str
|
Service token. |
''
|
timeout
|
float
|
Request timeout in seconds. |
DEFAULT_TIMEOUT_SECONDS
|
client
|
Client | None
|
Pre-built |
None
|
Example
Source code in src/pydantic_ai_backends/remote/client.py
| Python | |
|---|---|
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
__init__(service_url='http://localhost:8080', *, token='', timeout=DEFAULT_TIMEOUT_SECONDS, client=None)
¶
Source code in src/pydantic_ai_backends/remote/client.py
ls(session_id, path='.')
¶
List one directory of a stored workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Session whose files are wanted. |
required |
path
|
str
|
Directory to list. An absolute in-container path works, so a path taken from a live session's listing can be handed straight back. |
'.'
|
Raises:
| Type | Description |
|---|---|
WorkspaceArchiveError
|
If the workspace or directory is absent, the path escapes the workspace, or the service cannot be reached. |
Source code in src/pydantic_ai_backends/remote/client.py
read(session_id, path, offset=0, limit=2000)
¶
Read a slice of a stored workspace file.
Decoded exactly as a live session would decode it, so the archive and the sandbox never disagree about what a file says.
Raises:
| Type | Description |
|---|---|
WorkspaceArchiveError
|
If the file is absent, too large, not readable as text, outside the workspace, or the service is unreachable. |
Source code in src/pydantic_ai_backends/remote/client.py
close()
¶
pydantic_ai_backends.remote.client.WorkspaceArchiveError
¶
Bases: Exception
Raised when a stored workspace cannot be listed or read.
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
What the service answered, or |
Source code in src/pydantic_ai_backends/remote/client.py
SandboxdConfig¶
Everything the service decides on a client's behalf. Needs the server extra.
pydantic_ai_backends.remote.server.SandboxdConfig
dataclass
¶
Everything the service decides on a client's behalf.
Attributes:
| Name | Type | Description |
|---|---|---|
token |
str
|
Service token. Required to open, list or inspect any session. |
runtimes |
Mapping[str, str | SandboxRuntime]
|
Allowlist mapping a client-visible alias to what may run
under it — a bare image string, or a :class: |
default_runtime |
str
|
Alias used when a request names none. Empty — the
default — takes the first entry in |
max_sessions |
int | None
|
Ceiling on resident sandboxes across the service, or
|
max_open_sessions |
int | None
|
Ceiling on sessions that exist, resident or
hibernated, or |
evict_idle_after |
int | None
|
Seconds of inactivity after which a session at the
ceiling may be hibernated to make room for a new one, instead of the
new one being refused with |
max_sessions_per_tenant |
int | None
|
Ceiling on simultaneous sessions carrying one
|
mem_limit |
str | None
|
Default memory ceiling per sandbox, in Docker syntax. A runtime naming its own overrides this, upwards or downwards. |
memswap_limit |
str | None
|
Default ceiling on memory and swap combined. Raise it only where swap is |
cpus |
float | None
|
Default hard CPU ceiling per sandbox, in cores. |
cpu_shares |
int | None
|
Default relative CPU weight, applied only under contention.
Preferable to a hard |
tmpfs_size |
str | None
|
Size of an in-memory Sized against |
pids_limit |
int | None
|
Default process ceiling per sandbox. |
network_mode |
str | None
|
Default Docker network mode. |
oci_runtime |
str | None
|
Default low-level runtime for every sandbox, overridable per
runtime. |
work_dir |
str
|
Working directory inside every sandbox. Applied to built
runtimes as well, overriding whatever their |
workspace_root |
str | None
|
Host directory backing each sandbox's work directory, as
|
prewarm |
bool
|
Pull and build the whole allowlist in the background at startup. Without it the first session on a built runtime pays for the image build — a measured ten seconds and upwards — and pays it in the middle of somebody's request. Only applies to the default Docker builder, since nothing else knows how to warm a custom one. |
persist_containers |
bool
|
Give each sandbox a stable container name, so a
reaped session keeps its filesystem and the next attach restarts the
same container. |
idle_timeout |
int
|
Seconds of inactivity before a session is reaped. |
cleanup_interval |
int
|
Seconds between reaping passes. |
workspace_ttl |
int | None
|
Seconds an unused workspace directory is kept before it is
swept, timed from when its session was last opened. |
container_ttl |
int | None
|
Seconds a stopped persisted container is kept before it
is removed, timed from when it stopped. This reclaims what a session
installed inside its container — the build, the wheels, the
|
sandbox_uid |
int | None
|
Run every built runtime's sandbox as this unprivileged
user instead of root, and give each session's workspace to it.
Worth turning on. A container escape starts from whoever the
container runs as, and every file an agent writes into its
bind-mounted workspace is owned by that user on the host — as root,
a Two things it asks of the deployment. The service must be able to
|
max_read_bytes |
int
|
Largest single file a client may read out of a sandbox. |
execute_timeout |
int
|
Hard ceiling applied to every command, so one client cannot occupy a worker indefinitely. |
max_workers |
int
|
Threads dedicated to blocking sandbox calls. Sandbox commands are long, so this pool is kept separate from asyncio's shared default one. |
ui_enabled |
bool
|
Serve the bundled dashboard at |
Source code in src/pydantic_ai_backends/remote/server.py
| Python | |
|---|---|
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |
resolve_runtime(alias)
¶
Resolve a client-supplied alias to an allowed runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str | None
|
Runtime alias from the request, or |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, SandboxRuntime]
|
The |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the alias is not on the allowlist. |
Source code in src/pydantic_ai_backends/remote/server.py
limits_for(runtime)
¶
The ceilings one runtime actually runs under.
Its own values where it names them, the service defaults otherwise.
Source code in src/pydantic_ai_backends/remote/server.py
SandboxRuntime¶
One entry in the service's allowlist: what an alias runs, and under which ceilings.
pydantic_ai_backends.remote.server.SandboxRuntime
dataclass
¶
One entry in the service's runtime allowlist.
A client names the alias this is registered under and nothing else, so
everything here is the operator's decision. Give it either a ready-made
image, or a runtime whose packages are built into an image on first use.
The ceilings are per runtime, because they are not one number for a whole
service: a notebook-style data runtime needs several gigabytes where a plain
shell needs a few hundred megabytes, and forcing one value on both either
starves the first or over-commits the host for the second. A ceiling left
None takes the service-wide default from :class:SandboxdConfig, which is
a default rather than a maximum — a runtime may name more.
Attributes:
| Name | Type | Description |
|---|---|---|
image |
str | None
|
Ready-made image, started as-is. |
runtime |
RuntimeConfig | str | None
|
|
description |
str
|
What this environment is for, shown in the dashboard. |
mem_limit |
str | None
|
Memory ceiling in Docker syntax, e.g. |
memswap_limit |
str | None
|
Ceiling on memory and swap combined. |
cpus |
float | None
|
Hard CPU ceiling in cores. A sandbox never exceeds it — and so cannot use cores that are idle. |
cpu_shares |
int | None
|
Relative CPU weight, which applies only under contention. On a small host this is usually the better knob: one active sandbox may take the whole machine, and several are still divided fairly. |
pids_limit |
int | None
|
Process ceiling. |
network_mode |
str | None
|
Docker network mode. Naming anything but |
oci_runtime |
str | None
|
Low-level runtime the daemon starts this runtime's
containers with — |
Source code in src/pydantic_ai_backends/remote/server.py
| Python | |
|---|---|
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
builds
property
¶
Whether first use builds an image rather than pulling a ready one.
resolved_runtime()
¶
The RuntimeConfig this entry builds, looked up when named.
Source code in src/pydantic_ai_backends/remote/server.py
describes()
¶
Human-readable summary of what will run, for the policy view.
Source code in src/pydantic_ai_backends/remote/server.py
| Python | |
|---|---|
image_label()
¶
The image, or what it is built from when there is not one yet.
Source code in src/pydantic_ai_backends/remote/server.py
| Python | |
|---|---|
create_app¶
pydantic_ai_backends.remote.server.create_app(config, *, sandbox_builder=None)
¶
Build the sandbox service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SandboxdConfig
|
Service policy. See :class: |
required |
sandbox_builder
|
SandboxBuilder | None
|
Override for how a sandbox is constructed, receiving
|
None
|
Returns:
| Type | Description |
|---|---|
FastAPI
|
A configured |
Source code in src/pydantic_ai_backends/remote/server.py
Wire protocol¶
The HTTP contract as Pydantic models — one source of truth for both sides.
Wire protocol shared by the remote sandbox client and server.
These models are the single source of truth for the HTTP contract. The
operation endpoints (/exec, /read, /write, /ls, /glob) keep the field
names that :class:~pydantic_ai_backends.backends.kubernetes.KubernetesPodSandbox
already sends in mode="http", so one server can serve both clients; /edit,
/grep, /exists and /read_bytes are additions.
Binary-capable payloads travel base64-encoded (content_b64) because JSON
cannot carry arbitrary bytes, and a sandbox holds real files.
CreateSessionRequest
¶
Bases: BaseModel
Open a sandbox session.
Deliberately carries no container settings. Image, mounts, network mode and every resource ceiling come from the server's configuration, because a process holding the Docker socket can start a privileged container that mounts the host — a client that could name its own image or volumes would own the machine.
Source code in src/pydantic_ai_backends/remote/wire.py
runtime = None
class-attribute
instance-attribute
¶
Alias of a server-allowed runtime. None selects the server default.
tenant = Field(default=None, pattern=TENANT_PATTERN)
class-attribute
instance-attribute
¶
Who this session is opened on behalf of, for the per-tenant ceiling.
Declared by the client rather than derived from session_id, so the service
imposes no naming convention on ids. Only a caller holding the service token
can open a session at all, so this is a capacity label from a trusted
application — not an authorization claim, and it grants nothing.
reuse = False
class-attribute
instance-attribute
¶
Attach to the session under session_id when one is already open.
Off by default, so an id collision is reported rather than silently sharing
somebody else's sandbox. Turn it on for a session that spans several runs —
one conversation, say — where the second run has to reach the files the
first one wrote. The runtime of an attached session is whatever it was
opened with; a runtime that disagrees is rejected rather than ignored.
SessionCreated
¶
Bases: BaseModel
A freshly opened session and the token scoped to it.
Source code in src/pydantic_ai_backends/remote/wire.py
token
instance-attribute
¶
Grants access to this session only. The service token also works.
SessionInfo
¶
Bases: BaseModel
Observable state of one session.
Source code in src/pydantic_ai_backends/remote/wire.py
tenant = None
class-attribute
instance-attribute
¶
Whoever the session was opened for, when the client said.
state = 'running'
class-attribute
instance-attribute
¶
Whether the session holds a sandbox, or was stopped to free a slot and is
waiting for its next request to wake it. A hibernated session is never
alive; it still has its token, its event log and its files.
SessionList
¶
Bases: BaseModel
Every open session, for monitoring.
Source code in src/pydantic_ai_backends/remote/wire.py
limit = None
class-attribute
instance-attribute
¶
Configured max_sessions — the ceiling on resident sandboxes.
open_limit = None
class-attribute
instance-attribute
¶
Configured max_open_sessions, or None when uncapped.
tenant_limit = None
class-attribute
instance-attribute
¶
Configured max_sessions_per_tenant, or None when uncapped.
SessionUsage
¶
Bases: BaseModel
Point-in-time resource usage for one sandbox.
Source code in src/pydantic_ai_backends/remote/wire.py
SessionEvent
¶
Bases: BaseModel
One operation performed against a session.
Records what was asked for and how it went — never file contents or command output, which would turn an audit trail into a data leak and grow without bound.
Source code in src/pydantic_ai_backends/remote/wire.py
seq
instance-attribute
¶
Monotonic per-session sequence number, for incremental polling.
op
instance-attribute
¶
Operation name: exec, read, write, edit, ls, glob, grep, exists.
target
instance-attribute
¶
Command or path the operation addressed, truncated.
detail = ''
class-attribute
instance-attribute
¶
Short outcome summary, e.g. exit 0 or 14 entries.
SessionEvents
¶
Bases: BaseModel
A slice of a session's activity log.
Source code in src/pydantic_ai_backends/remote/wire.py
latest_seq = 0
class-attribute
instance-attribute
¶
Highest sequence number the service holds; pass it back as after.
ServiceHealth
¶
Bases: BaseModel
Liveness and capacity summary. Unauthenticated.
Source code in src/pydantic_ai_backends/remote/wire.py
ServicePolicy
¶
Bases: BaseModel
The ceilings and allowlist the service imposes on every sandbox.
Authenticated, because it describes the host's configuration — though it holds nothing a caller with the service token is not already subject to. Exists so an operator can see the limits actually in force rather than inferring them from a config file.
Source code in src/pydantic_ai_backends/remote/wire.py
runtimes = Field(default_factory=list)
class-attribute
instance-attribute
¶
Every allowed runtime, with the ceilings it actually runs under.
max_sessions = None
class-attribute
instance-attribute
¶
Ceiling on resident sandboxes.
max_open_sessions = None
class-attribute
instance-attribute
¶
Ceiling on sessions that exist, resident or hibernated.
evict_idle_after = None
class-attribute
instance-attribute
¶
Seconds after which an idle session may be hibernated to free a slot.
memswap_limit = None
class-attribute
instance-attribute
¶
Default memory-plus-swap ceiling, or None when swap is pinned to memory.
oci_runtime = None
class-attribute
instance-attribute
¶
Default low-level runtime, or None for whatever the daemon defaults to.
persist_containers = False
class-attribute
instance-attribute
¶
Whether a stopped sandbox keeps its filesystem for the next attach.
sandbox_uid = None
class-attribute
instance-attribute
¶
Uid built runtimes run as, or None when sandboxes run as root.
workspace_ttl = None
class-attribute
instance-attribute
¶
Seconds an unused workspace is kept before it is swept, or None.
container_ttl = None
class-attribute
instance-attribute
¶
Seconds a stopped container is kept before its build is reclaimed.
tmpfs_size = None
class-attribute
instance-attribute
¶
Size of the in-memory /tmp each sandbox gets, or None when it has none.
prewarm = False
class-attribute
instance-attribute
¶
Whether the allowlist is pulled and built at startup rather than on demand.
buildkit = False
class-attribute
instance-attribute
¶
Whether image builds use BuildKit, and so keep package caches between them.
ServiceIndex
¶
Bases: BaseModel
What this service is and where to find its endpoints.
Served at / so that opening the base URL says something useful instead of
a bare 404. Unauthenticated, and deliberately free of any session detail.