How to Configure Pydantic AI Kubernetes Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a pydantic ai kubernetes sandbox by giving agent tasks a strict namespace role. Then you map agent tool calls to run inside isolated pods in that namespace. Teams often start by giving an agent a wide cluster role so scripts run fast. When roles are too broad, a bad prompt can read secrets or change other workloads. A good setup scopes the agent to one namespace with only the rules it needs. How a pydantic ai kubernetes sandbox works Pydantic AI lets you build agents that call stru

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up a pydantic ai kubernetes sandbox by giving agent tasks a strict namespace role. Then you map agent tool calls to run inside isolated pods in that namespace. Teams often start by giving an agent a wide cluster role so scripts run fast. When roles are too broad, a bad prompt can read secrets or change other workloads. A good setup scopes the agent to one namespace with only the rules it needs.

How a pydantic ai kubernetes sandbox works

Pydantic AI lets you build agents that call structured tools to finish tasks. When an agent needs to run code, the tool call starts a pod job. Kubernetes uses role-based rules to check if the agent service account can do this. A Role sets permissions inside one single namespace. In contrast, a ClusterRole can cover all namespaces or cluster nodes. Permissions in Kubernetes are only additive, which means there are no deny rules. You link the agent service account to the Role with a RoleBinding. This keeps the agent inside the sandbox namespace. To run commands or read output, the role must list pod subresources. Kubernetes writes subresources with a slash, such as the pod log or pod exec subresource. You must grant exact verbs for these subresources so the agent can read job output.

Scoping the sandbox namespace and service account

Start your setup by making a dedicated namespace just for agent tasks. Next, create a service account for the Pydantic AI runner in that namespace. Write a Role that only allows creating pods and reading pod logs there. Bind that Role to the service account with a RoleBinding in the same namespace. Avoid using a ClusterRoleBinding because it grants permissions across the whole cluster. Also keep in mind how Kubernetes handles resource name lists on subresources. You cannot restrict creation on subresources by specific resource names alone. Instead, keep all sensitive apps out of the sandbox namespace completely. Give the sandbox pods strict memory and CPU limits so bad loops stop fast. Drop unnecessary network paths so a sandbox pod cannot reach internal databases on its own.

What native roles solve and what they leave open

Native Kubernetes roles do a great job of limiting where an agent can log in. A RoleBinding stops the agent service account from touching pods in other namespaces. It also blocks the agent from reading cluster nodes or non-resource endpoints. Still, native setup only checks who logs in and which API endpoint they call. Once Kubernetes allows the pod exec subresource, it does not inspect the commands inside. An agent with exec permissions can run any shell command inside that pod. Native roles also do not inspect data returned in pod logs or tool outputs. If a sandbox pod queries a test database, raw emails or phone numbers return untouched. Teams still need a way to check individual commands in flight and mask sensitive fields.

AI Sandbox Environments + Kubernetes RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Check Kubernetes API requests and responses on the wire

Kubernetes RBAC decides who may call the API server. RBAC does not check response payloads or split listing Secret names from reading Secret values when a role grants get access. With hoop.dev, you run an open-source HTTP proxy, a sidecar for your Kubernetes API. The kubectl client changes only the server address it talks to while RBAC stays in place.

On each call, hoop.dev guardrails read the method, normalized path, and headers to refuse bad deletes or full Secret reads with a 403 error. On the way back, hoop.dev data masking walks JSON responses by key and masks matched values in Secrets, ConfigMaps, and pod logs. Every request and verdict is logged without storing bearer tokens. View the project on GitHub.

Frequently asked questions

Why should a Pydantic AI agent use a Role instead of a ClusterRole?

A Role keeps permissions inside one namespace, while a ClusterRole can reach across the whole cluster. Using a Role with a RoleBinding stops the agent from touching other team workloads.

How does the agent read output from a sandbox pod?

The agent service account needs permission for the pod log and pod exec subresources. Kubernetes writes these subresources with a slash in the role rules.

Can I add a deny rule to block one specific command in Kubernetes RBAC?

No, Kubernetes role permissions are purely additive and have no deny rules. You must start with zero permissions and only add the exact resources the agent needs.

The query runs. The emails stay hidden.