Skip to main content

Kubernetes Agent Sandbox integration for LangChain Deep Agents

Project description

langchain-k8s

Kubernetes Agent Sandbox integration for LangChain Deep Agents.

Implements the BaseSandbox / SandboxBackendProtocol contract using kubernetes-sigs/agent-sandbox as the execution backend. Agents get isolated, ephemeral Kubernetes pods for running shell commands and file operations — fully self-hosted, no vendor lock-in.

Installation

pip install langchain-k8s

Or with uv:

uv add langchain-k8s

Prerequisites

  • A Kubernetes cluster with the agent-sandbox controller installed
  • A SandboxTemplate resource defining the pod spec for your sandboxes
  • kubectl configured with cluster access

Quick start

from langchain_anthropic import ChatAnthropic
from deepagents import create_deep_agent
from langchain_k8s import KubernetesSandbox

backend = KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
)

agent = create_deep_agent(
    model=ChatAnthropic(model="claude-sonnet-4-20250514"),
    system_prompt="You are a Python coding assistant with sandbox access.",
    backend=backend,
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "Create a Python script that prints the Fibonacci sequence"}]}
)

backend.stop()

Usage

Context manager (recommended)

with KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
) as backend:
    agent = create_deep_agent(model=model, backend=backend, ...)
    result = agent.invoke({"messages": [...]})
# Sandbox pod is automatically cleaned up

Explicit lifecycle

backend = KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
)
backend.start()
try:
    resp = backend.execute("python3 --version")
    print(resp.output)
finally:
    backend.stop()

Direct execution

with KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
) as backend:
    # Execute commands
    resp = backend.execute("echo 'Hello from K8s!'")
    print(resp.output, resp.exit_code)

    # Upload files
    backend.upload_files([("/workspace/script.py", b"print('hello')\n")])

    # Download files
    results = backend.download_files(["/workspace/script.py"])
    print(results[0].content)

Connection modes

Mode Configuration Use case
Production gateway_name="my-gateway" Cluster with Gateway API
Development (default — no gateway, no api_url) Auto kubectl port-forward
Advanced api_url="http://localhost:8080" Pre-existing port-forward or in-cluster
# Production — cluster Gateway
backend = KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
    gateway_name="sandbox-gateway",
)

# Development — automatic port-forward
backend = KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
)

# Advanced — existing port-forward or in-cluster routing
backend = KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
    api_url="http://localhost:8080",
)

Sandbox lifecycle strategies

Control how sandboxes are managed across multiple agent invocations via the reuse_sandbox parameter:

Persistent (default)

backend = KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
    reuse_sandbox=True,  # default
)

One sandbox pod is created lazily and reused across all calls. Fast for cached, long-lived agents. Filesystem state persists between invocations. Auto-reconnects if the pod dies.

Ephemeral

backend = KubernetesSandbox(
    template_name="python-sandbox-template",
    namespace="agent-sandbox-system",
    reuse_sandbox=False,
)

A fresh sandbox is created for each start()/stop() cycle. Maximum isolation between invocations at the cost of cold-start latency.

Configuration reference

Parameter Type Default Description
template_name str (required) SandboxTemplate CRD name
namespace str "default" Kubernetes namespace
gateway_name str | None None Gateway name (production mode)
gateway_namespace str "default" Gateway namespace
api_url str | None None Direct router URL (advanced mode)
server_port int 8888 Sandbox runtime port
reuse_sandbox bool True Reuse sandbox across calls
max_output_size int 1048576 Max output bytes before truncation
command_timeout int 300 Command timeout in seconds

Development

# Clone and install
git clone https://github.com/uesleilima/langchain-k8s.git
cd langchain-k8s
uv sync

# Run unit tests (no cluster needed)
uv run pytest tests/unit/ -v

# Lint and type check
uv run ruff check src/ tests/
uv run mypy src/

Integration tests with Kind

Integration tests require a Kubernetes cluster. The repository includes scripts and manifests to set up a Kind cluster with everything needed.

Prerequisites: kind, kubectl, docker

# Create the Kind cluster and deploy agent-sandbox components
./scripts/kind-setup.sh

# Run integration tests
uv run pytest tests/integration/ -v -m integration

# Tear down when done
./scripts/kind-teardown.sh

The setup script will:

  1. Create a Kind cluster named langchain-k8s
  2. Install the agent-sandbox controller and extension CRDs (v0.1.1)
  3. Enable the extensions controller
  4. Deploy the sandbox router
  5. Apply the python-sandbox-template SandboxTemplate
k8s/
├── sandbox-router.yaml            # Router Deployment + Service
└── sandbox-template.yaml          # SandboxTemplate for Python runtime

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_k8s-0.1.0.tar.gz (138.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_k8s-0.1.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file langchain_k8s-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_k8s-0.1.0.tar.gz
  • Upload date:
  • Size: 138.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for langchain_k8s-0.1.0.tar.gz
Algorithm Hash digest
SHA256 812bdd8b8b3f3bd9f4df7e4bf703949197362d80a8fdc8c15c2d95b41afa46af
MD5 69793b9e4dbd508e8b4b35c1aaf86030
BLAKE2b-256 5895d9c9dbca1dc12082e9111429cfb72444c51b7d3b2881ba48c4cba5b570db

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_k8s-0.1.0.tar.gz:

Publisher: publish.yml on uesleilima/langchain-k8s

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_k8s-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: langchain_k8s-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for langchain_k8s-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b76caaffe59ca0e543332bf08b7c860387370b0dc1ad7e823779ef2faeb10f7
MD5 bb3f5c0cfa064ec9837a97b70fb0b7f7
BLAKE2b-256 9f5fe1e5263eed6a8b8fe88076d6d8e24893cfd7d4624a7c64fc417f45b0348e

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_k8s-0.1.0-py3-none-any.whl:

Publisher: publish.yml on uesleilima/langchain-k8s

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page