Skip to content

All posts

KubernetesAI

How to set a ResourceQuota for an AI agent namespace

Ritesh Patel

Kubernetes: How to set a ResourceQuota for an AI agent namespace

A ResourceQuota caps the total CPU, memory and object count that pods in one namespace can use. Create it in the agent’s namespace, and the control plane rejects any request that would go past it. Once the quota covers CPU or memory, every new pod must set requests or limits for those values, so pair it with a LimitRange.

Checked against the Kubernetes v1.37 documentation as fetched on 2026-11-21.

What a ResourceQuota limits in an agent namespace

A ResourceQuota is an API object that lives in one namespace. The Kubernetes resource quota docs say it limits aggregate resource consumption per namespace, and it can also cap how many objects of a given kind exist there. For an agent namespace, the caps that usually matter are the total CPU and memory the agents request, and the number of pods and Jobs they can create.

Two details decide whether the quota helps. The quota is expressed in absolute units, so adding nodes to the cluster does not raise a namespace’s limit. The docs also say a quota creates no restrictions around nodes. Pods from several namespaces can run on the same node, so the quota caps usage, not placement.

A quota does not change what already runs. The docs say neither contention nor changes to quota affect resources that already exist, so tightening a quota will not stop an agent that is already running. For the pod-level security settings that sit beside quotas, see Pod Security Admission levels for AI agent workloads and how to restrict egress from AI agent pods with NetworkPolicy.

Pick the quota keys that match agent risk

The quota API groups its keys into families. Compute keys cap the sum of pod requests and limits. Object-count keys cap how many objects exist. The table lists the keys most useful for agent workloads, using the names the docs give.

Quota family Example keys What it caps
Compute requests.cpu, requests.memory, limits.cpu, limits.memory The sum across all pods that are not in a terminal state
Object count pods, count/jobs.batch, count/pods How many objects of that kind the namespace can hold
Storage requests.storage, persistentvolumeclaims Total requested PVC storage, and the number of PVCs
Ephemeral storage requests.ephemeral-storage, limits.ephemeral-storage The sum of local ephemeral storage across pods

Object-count keys guard against runaway controllers. The docs suggest a Jobs quota to protect against a badly configured CronJob, and an agent that schedules its own batch work can create the same kind of load. A count/jobs.batch key caps that. The pods key counts only pods that are not terminal.

Set up the quota in four steps

  1. Create the namespace. The docs use a plain kubectl create namespace for their examples, so this step does not need flags.
kubectl create namespace agents
  1. Write a ResourceQuota manifest. The hard block sets the caps. The keys and their shape come from the docs’ compute and object-count examples. The numbers are an example shape, not a sizing recommendation, so set them from the agent’s measured usage.
apiVersion: v1
kind: ResourceQuota
metadata:
  name: agent-quota
spec:
  hard:
    requests.cpu: "4"
    requests.memory: "8Gi"
    limits.cpu: "8"
    limits.memory: "16Gi"
    pods: "20"
    count/jobs.batch: "10"
  1. Apply the manifest to the namespace. The docs apply their quota with kubectl create -f and a --namespace flag.
kubectl create -f ./agent-quota.yaml --namespace=agents
  1. Read the usage back. The Used column shows what the namespace has consumed against each Hard cap. Run this after each rollout of the agents to confirm the numbers you expected.
kubectl describe quota agent-quota --namespace=agents

Let a LimitRange supply defaults

Once a quota covers CPU or memory, the docs require every new pod to set a request or a limit for that resource. Pods that omit both are the ones that fail. The fix is a LimitRange in the same namespace. The LimitRange docs say its admission step applies default request and limit values to pods that do not set compute requirements, and that this happens first, before the LimitRange checks the pod against its minimum, maximum and ratio rules.

A LimitRange does not check that its defaults fit together. The docs warn that a default limit lower than the request can leave the pod unschedulable. If two or more LimitRange objects exist in one namespace, the docs say it is not deterministic which default applies, so keep one LimitRange per namespace. A LimitRange change also affects only new pods. Pods already running keep their values.

Pod "example-conflict-with-limitrange-cpu" is invalid: spec.containers[0].resources.requests: Invalid value: "700m": must be less than or equal to cpu limit

The message above comes from a pod whose CPU request was higher than the default limit. The docs show that the same pod schedules once it sets a request and a limit that match.

What a rejected agent pod looks like

A rejected pod does not always fail loudly. The quota walkthrough shows a direct kubectl apply of a pod that would push the namespace past its memory request cap. The control plane returns a Forbidden error that names the quota, the requested amount and the amount already used.

Error from server (Forbidden): error when creating "examples/admin/resource/quota-mem-cpu-pod-2.yaml":
pods "quota-mem-cpu-demo-2" is forbidden: exceeded quota: mem-cpu-demo,
requested: requests.memory=700Mi,used: requests.memory=600Mi, limited: requests.memory=1Gi

When the pod comes from a Deployment, the Deployment is created even if its pods are not. The docs say the Deployment creation succeeds, but the Deployment may not be able to get all of the pods it manages to exist. They point to kubectl describe on the Deployment to see what happened. An agent rollout can therefore show a Deployment that exists with fewer pods than its replica count, so check the replica status after every change to the quota.

Fine print that changes the outcome

  • Only non-terminal pods count. The compute totals sum pods that are not in a terminal state, and the pods key counts non-terminal pods only. A finished agent Job frees its quota once its pods reach a terminal phase.
  • Container logs can use the quota. The docs say that with a CRI container runtime, container logs count against the ephemeral storage quota. They warn this can cause the unexpected eviction of pods that have used up their storage quota. A chatty agent can hit this before it hits CPU or memory.
  • A quota is enforced only where it exists. The docs say enforcement happens in a namespace only when a ResourceQuota is there.
  • Scopes narrow what a quota counts. A quota can carry a scope such as BestEffort, NotBestEffort, Terminating or PriorityClass. Resources outside the set the scope allows are a validation error on the quota, so check the scope list for the Kubernetes version you run.

Enforcing this with Kyverno

The Kyverno policy library has an Add Quota policy, add-ns-quota, under the Multi-Tenancy and EKS Best Practices categories. It is a GeneratingPolicy. When a Namespace is created, it generates a ResourceQuota named default-resourcequota and a LimitRange named default-limitrange. The generated quota sets requests.cpu and limits.cpu to 4, and requests.memory and limits.memory to 16Gi. The generated LimitRange sets a container default of 500m CPU and 1Gi memory, and a default request of 200m CPU and 256Mi memory. The policy needs Kyverno 1.19.1 or later.

Those numbers suit a general tenant, not an agent, so review them before you use the policy on an agent namespace. The policy also reads an exceptions list of kind and name pairs, and it filters out any generated object that appears there. It does not generate object-count keys such as count/jobs.batch, so add those in your own manifest. Kyverno is open source, and the Kyverno project page covers the engine. For the per-container side of this, read how to set resource limits on AI agent pods, and for governance across clusters see governing AI agents.

Frequently asked questions

What happens when an agent tries to create a pod over the namespace quota?

The control plane rejects the request with HTTP status 403 Forbidden, and the error message names the quota and the resource that would be exceeded. The pod is never created. If the pod belongs to a Deployment, the Deployment object is still created, so the failure does not show up as an error on the Deployment itself. Run kubectl describe on the Deployment to see whether all of its pods were created.

Does a ResourceQuota stop an agent that is already running?

No. The Kubernetes docs say that neither contention nor changes to a quota affect resources that already exist. The quota is checked when an object is created or updated. A tighter quota only blocks new pods and new usage. To cap a single running agent, set requests and limits on its containers, as the per-pod resource limits post explains.

Why does a pod fail when the namespace has a CPU or memory quota and the pod sets no values?

For cpu and memory, the docs say every new pod in a namespace with that quota must set a request or a limit for the resource, or the control plane may reject it. A LimitRange with a default request fills the gap for pods that omit the values. Set requests and limits in the pod spec when you want exact control, and check that a LimitRange default limit is not lower than the request.

Do finished agent Jobs count against the quota?

Pods in a terminal state do not count. The docs define a terminal pod as one whose phase is Failed or Succeeded, and the compute totals only sum pods that are not terminal. Pods that are still running or pending do count. Run kubectl describe quota to see the used and hard amount for each key, so you can see how much room a finished Job freed.

Can a ResourceQuota keep agents off certain nodes?

No. The docs say a ResourceQuota creates no restrictions around nodes, and pods from several namespaces may run on the same node. A quota caps the totals a namespace can request. It does not decide where the pods run, so placement needs its own controls.

Ritesh Patel, Founder & VP Product, Nirmata

Ritesh Patel is a founder of Nirmata and leads its products. He writes about governing AI coding agents, MCP security and Kubernetes policy as code.

LinkedIn

Govern AI agents with AIControlsControl what AI agents do, and prove it.
Share

Stay up-to-date with the latest in AI, governance, and infrastructure

Subscribe to our free monthly newsletter to get industry news, DevOps tips, and articles right to your inbox on Kyverno, containerization, Kubernetes deployment & management and more.