Skip to main content
An agent asked to “count all resource requests and limits across all pods” returned a full capacity report in 56ms — from a single API call. All the computation happened inside the sandbox.

The Numbers

MetricValue
Total time56ms
API calls made inside sandbox1
Pods analyzed11
Tool calls seen by the LLM1

The Code

async () => {
  const clusterId = "cls_abc123"; // resolved by the agent from conversation

  const allPods = await cnap.request({
    method: "GET",
    path: `/v1/clusters/${clusterId}/kube/api/v1/pods`,
  }).then(r => r.body);

  // Unit parsers — written by the agent, executed in the sandbox
  const parseCpu = (v) => {
    if (!v) return 0;
    if (v.endsWith("m")) return parseInt(v) / 1000;
    return parseFloat(v);
  };
  const parseMem = (v) => {
    if (!v) return 0;
    if (v.endsWith("Gi")) return parseFloat(v) * 1024;
    if (v.endsWith("Mi")) return parseFloat(v);
    if (v.endsWith("Ki")) return parseFloat(v) / 1024;
    return parseFloat(v) / (1024 * 1024);
  };

  const totals = { requests: { cpu: 0, memMi: 0 }, limits: { cpu: 0, memMi: 0 } };
  const perPod = [];

  for (const pod of allPods.items.filter(p => p.status.phase === "Running")) {
    const podRes = { requests: { cpu: 0, memMi: 0 }, limits: { cpu: 0, memMi: 0 } };
    for (const c of pod.spec.containers) {
      const r = c.resources || {};
      podRes.requests.cpu += parseCpu(r.requests?.cpu);
      podRes.requests.memMi += parseMem(r.requests?.memory);
      podRes.limits.cpu += parseCpu(r.limits?.cpu);
      podRes.limits.memMi += parseMem(r.limits?.memory);
    }
    totals.requests.cpu += podRes.requests.cpu;
    totals.requests.memMi += podRes.requests.memMi;
    totals.limits.cpu += podRes.limits.cpu;
    totals.limits.memMi += podRes.limits.memMi;
    perPod.push({
      pod: pod.metadata.name,
      ns: pod.metadata.namespace,
      requests: { cpu: `${Math.round(podRes.requests.cpu * 1000)}m`, mem: `${Math.round(podRes.requests.memMi)}Mi` },
      limits: { cpu: `${Math.round(podRes.limits.cpu * 1000)}m`, mem: `${Math.round(podRes.limits.memMi)}Mi` },
    });
  }

  return {
    pod_count: perPod.length,
    totals: {
      requests: { cpu: `${Math.round(totals.requests.cpu * 1000)}m`, mem: `${Math.round(totals.requests.memMi)}Mi` },
      limits: { cpu: `${Math.round(totals.limits.cpu * 1000)}m`, mem: `${Math.round(totals.limits.memMi)}Mi` },
    },
    per_pod: perPod.sort((a, b) => parseInt(b.requests.mem) - parseInt(a.requests.mem)),
  };
}

Why This Matters

The Kubernetes API returns raw pod specs — CPU in millicores ("100m"), memory in mixed units ("256Mi", "1Gi", "131072Ki"). A traditional MCP tool would dump all that raw JSON into the LLM’s context and hope it can do math. Code Mode puts the computation in the sandbox. The agent wrote unit parsers for CPU and memory, iterated every container in every running pod, aggregated totals, and returned a clean capacity report. The LLM context received a sorted summary — not the raw specs of 11 pods.

What the Agent Does

  1. Fetches all pods across all namespaces (single API call)
  2. Writes CPU millicore and memory unit parsers
  3. Iterates every container in every running pod
  4. Aggregates requests and limits per-pod and cluster-wide
  5. Converts back to human-readable units, sorts by memory
  6. Returns a formatted capacity report