Python Client Library
Python implementation of the agent-backend package. See the main README for an overview, quick start, and core usage.
Package Info
| Field | Value |
|---|---|
| Package | agent-backend |
| Registry | PyPI |
| Manager | uv / pip |
| Test runner | pytest |
| Build | uv build |
| Linter | ruff |
| Type checker | ty |
| Source | packages/agent-backend/python/agent_backend/ |
| Tests | packages/agent-backend/python/tests/ |
Advanced Features
Environment Variables
Scoped backends support custom environment variables that apply to all commands:
from agent_backend import ScopeConfig
scoped_backend = backend.scope("projects/my-app", ScopeConfig(
env={
"PYTHONPATH": "/workspace/lib",
"API_KEY": "secret",
"DATABASE_URL": "postgres://...",
}
))
await scoped_backend.exec("python -m build") # uses custom env
Operations Logging
from agent_backend import ConsoleOperationsLogger, ScopeConfig
scoped_backend = backend.scope("project", ScopeConfig(
operations_logger=ConsoleOperationsLogger()
))
await scoped_backend.exec("pip install -r requirements.txt")
# Logs: [AgentBackend] exec: pip install -r requirements.txt
Binary Data
from agent_backend import ReadOptions
image_data = await backend.read("logo.png", ReadOptions(encoding="buffer"))
tarball = await backend.exec("tar -czf - .", ExecOptions(encoding="buffer"))
Timeouts
from agent_backend import RemoteFilesystemBackend, RemoteFilesystemBackendConfig
backend = RemoteFilesystemBackend(RemoteFilesystemBackendConfig(
root_dir="/tmp/agentbe-workspace",
host="server.com",
auth_token="...",
operation_timeout_ms=300_000, # 5 minutes
max_output_length=10 * 1024 * 1024, # 10MB
))
Backend Connection Pooling
See docs/connection-pooling.md for BackendPoolManager usage, key-based pooling, idle cleanup, and graceful shutdown.
Examples
Code Execution Sandbox
from agent_backend import LocalFilesystemBackend, LocalFilesystemBackendConfig, IsolationMode
sandbox = LocalFilesystemBackend(LocalFilesystemBackendConfig(
root_dir="/tmp/agentbe-workspace",
isolation=IsolationMode.AUTO,
))
user_code_backend = sandbox.scope(f"users/{user_id}")
await user_code_backend.write("script.py", untrusted_code)
result = await user_code_backend.exec("python script.py")
Multi-tenant SaaS
from agent_backend import RemoteFilesystemBackend, RemoteFilesystemBackendConfig
# Separate backend per organization
org1_backend = RemoteFilesystemBackend(RemoteFilesystemBackendConfig(
root_dir="/var/saas/org1",
host="org1-server.example.com",
auth_token="...",
))
org2_backend = RemoteFilesystemBackend(RemoteFilesystemBackendConfig(
root_dir="/var/saas/org2",
host="org2-server.example.com",
auth_token="...",
))
# Scoped backends per user within each org
org1_user1 = org1_backend.scope("users/user1")
org1_user2 = org1_backend.scope("users/user2")
Agent State Management
from agent_backend import MemoryBackend
state = MemoryBackend()
await state.write("agents/agent1/current-task", "building")
await state.write("agents/agent1/progress", "50%")
all_agents = await state.list_keys("agents/")
Error Handling
from agent_backend import BackendError, DangerousOperationError, PathEscapeError
try:
await backend.exec("rm -rf /")
except DangerousOperationError as e:
# Command blocked by safety validation
print("Blocked:", e.operation)
except PathEscapeError:
# Path attempted to escape scope
pass
except BackendError as e:
# General backend error (check e.code)
print("Error:", e.code, str(e))
Development
Commands
All commands can be run from the monorepo root via Make or from the packages/agent-backend/python/ directory via uv.
| Task | Make (root) | uv (packages/agent-backend/python/) |
|---|---|---|
| Build | make build-python |
uv build |
| Test | make test-python |
uv run pytest |
| Test (cov) | -- | uv run pytest --cov=agent_backend --cov-fail-under=80 |
| Lint | make lint-python |
uv run ruff check . |
| Lint (fix) | make lint-fix |
uv run ruff check --fix . |
| Typecheck | make typecheck-python |
uv run ty check |
Code Style
- ruff enforces formatting (
line-length = 100,target-version = "py311") - Type hints on all function signatures -- avoid
Any snake_casefor functions and variables,PascalCasefor classes- Dataclasses for all config objects (
LocalFilesystemBackendConfig,ScopeConfig, etc.) - Custom error classes:
BackendError,DangerousOperationError,PathEscapeError - Imports sorted with ruff (
isortrules enabled)
Testing
Tests live in packages/agent-backend/python/tests/ and use pytest with pytest-asyncio.
asyncio_mode = "auto" is configured in pyproject.toml, so async test functions are detected automatically -- no @pytest.mark.asyncio decorator needed.
Shared fixtures in conftest.py provide pre-configured backends:
@pytest.fixture
def local_backend(tmp_workspace):
config = LocalFilesystemBackendConfig(
root_dir=tmp_workspace,
prevent_dangerous=True,
)
return LocalFilesystemBackend(config)
Use unittest.mock.AsyncMock for mocking async methods. Use the shared fixtures (local_backend, memory_backend, tmp_workspace) rather than building backends from scratch.
Running tests:
uv run pytest # All tests, single run
uv run pytest -k "safety" # Filter by pattern
uv run pytest --cov=agent_backend # With coverage report
uv run pytest -m "not integration" # Skip integration tests
Gotchas
- All backend methods are
async-- alwaysawaitthem, includingread,write,readdir, andexists. MemoryBackend.exec()raisesNotImplementedBackendError-- memory backends do not support command execution.- Use
list_keys(prefix)onMemoryBackend, notlist(). IsolationMode.AUTOandIsolationMode.NONEare enum members, not string literals.BackendTypeenum values are"local-filesystem","remote-filesystem","memory".- Config objects are dataclasses, not dicts -- use keyword arguments (e.g.,
LocalFilesystemBackendConfig(root_dir=...)). - Scoped backends delegate
track_closeable()to their parent, so resources are closed when the parent is destroyed. destroy()closes all tracked closeables (MCP clients, transports) before tearing down the backend.- Coverage threshold is 80% (
--cov-fail-under=80). Remote backend and transport modules are excluded from coverage. ExecOptionsandReadOptionsuseencoding: Literal["utf8", "buffer"], not Python's standard encoding names.
Release files for agent-backend 0.13.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| agent_backend-0.13.2.tar.gz | 36.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| agent_backend-0.13.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 71.8 kB
Release files / agent_backend-0.13.2.tar.gz
| Download URL | agent_backend-0.13.2.tar.gz |
|---|---|
| Size | 36.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
96e6466562dc42ebf1ebbfa5f72730b0b60a96bd88da277206bf7e7b537f407a
|
|
BLAKE2b-256 checksum How to use checksums |
41c78d200f8c9cb513e57b6d6d9f748301dec10221792372ac6b97a2c50ed234
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.
Transparency logRelease files / agent_backend-0.13.2-py3-none-any.whl
| Download URL | agent_backend-0.13.2-py3-none-any.whl |
|---|---|
| Size | 35.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8e2118f88c75cb8d16631a4541508928bf91fb70b5de3c09b055929ede134f9b
|
|
BLAKE2b-256 checksum How to use checksums |
72f16a40d78edd432874b3d1cc088753aafd8c8bac1807fef8d4fc0872d45360
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.
Transparency log