How to Configure Pydantic AI Kubernetes Guardrails

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai kubernetes guardrails by combining schema checks with cluster role rules. This setup validates agent tool inputs and restricts which API calls the agent can run. Many engineering teams wire an agent directly to a cluster service account for debugging. When that account holds broad rights, one bad prompt can delete workloads or read secrets. Teams need both code checks in the agent and strict rules in the cluster. How pydantic ai kubernetes guardrails work in practice Py

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 pydantic ai kubernetes guardrails by combining schema checks with cluster role rules. This setup validates agent tool inputs and restricts which API calls the agent can run. Many engineering teams wire an agent directly to a cluster service account for debugging. When that account holds broad rights, one bad prompt can delete workloads or read secrets. Teams need both code checks in the agent and strict rules in the cluster.

How pydantic ai kubernetes guardrails work in practice

Pydantic AI lets developers define structured schemas for every tool an agent can call. Before the agent sends a request, the framework checks the inputs against these schemas. If the model picks a bad value, the validation check fails right away. You can write custom validators that reject forbidden namespace names or risky action types. The framework then sends the error back to the model so it can try again.

Inside the cluster, Kubernetes RBAC controls what the agent service account can do. A Role sets permissions within one namespace, while a ClusterRole covers the whole cluster. A RoleBinding grants those permissions in a specific namespace. A ClusterRoleBinding grants access across the entire cluster. In Kubernetes RBAC, permissions are additive only, which means there are no deny rules. You must start with zero permissions and add only the exact verbs the agent needs.

Why native roles and schema checks fall short

Schema checks and RBAC rules decide who logs in and which API endpoints respond. They do not inspect what individual commands or requests do while in flight. For example, Kubernetes RBAC treats pod logs and pod exec as subresources. You grant them by adding rules for the pods log or pods exec subresource. Once an agent has pod exec rights, RBAC cannot filter the shell commands it runs.

Schema checks in Python also have clear limits in production. They run inside the agent process and rely on developers to predict every bad input. If an agent reads pod logs, native RBAC returns the full text to the model. Those logs often contain sensitive user data like emails, phone numbers, or card numbers. Native cluster roles cannot mask those fields in the output before the agent reads them.

AI Guardrails + Kubernetes RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Building safer tool boundaries for cluster agents

To lower risk, split your agent tools into small, read-only functions first. Give the agent a dedicated service account bound to a single namespace with a RoleBinding. Avoid ClusterRoleBindings unless the agent must check nodes or resources across all namespaces. Keep pod exec permissions off the agent role whenever possible. If the agent only needs to read status, grant get and list on pods only. When an agent must run a change, require a human check before the tool runs.

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

Can Kubernetes RBAC block specific shell commands inside a pod exec session?

No, Kubernetes RBAC only checks if the account can open the exec subresource. Once the session starts, RBAC does not inspect or block the shell commands inside it.

Do Kubernetes Roles support deny rules for risky agent actions?

No, permissions in Kubernetes RBAC are additive only and include no deny rules. You must restrict the agent by leaving risky resources and verbs out of its Role.

How do Pydantic AI validators handle bad tool arguments from a model?

When a tool argument fails a schema check, Pydantic AI catches the validation error. It can pass that error back to the model so the agent fixes its request.

The query runs. The emails stay hidden.