> ## Documentation Index
> Fetch the complete documentation index at: https://docs.akua.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Resource Audit

> Cluster-wide CPU/memory capacity report in 56ms from a single API call

<Frame>
  <img className="block dark:hidden" src="https://mintcdn.com/akua-1dce587a/AEEz0U2s7Do2sYaM/images/heros/ai-resource-audit-light.svg?fit=max&auto=format&n=AEEz0U2s7Do2sYaM&q=85&s=c2d13b77b25385b9724c10695ada1138" alt="Raw pod specs in mixed CPU and memory units from one API call passing through an in-sandbox aggregate step that normalizes them into a clean requests-versus-limits capacity report with a per-pod breakdown" width="1536" height="864" data-path="images/heros/ai-resource-audit-light.svg" />

  <img className="hidden dark:block" src="https://mintcdn.com/akua-1dce587a/AEEz0U2s7Do2sYaM/images/heros/ai-resource-audit-dark.svg?fit=max&auto=format&n=AEEz0U2s7Do2sYaM&q=85&s=3b9e660acde1a295af516f1f58cd75f6" alt="Raw pod specs in mixed CPU and memory units from one API call passing through an in-sandbox aggregate step that normalizes them into a clean requests-versus-limits capacity report with a per-pod breakdown" width="1536" height="864" data-path="images/heros/ai-resource-audit-dark.svg" />
</Frame>

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

| Metric                        | Value    |
| ----------------------------- | -------- |
| Total time                    | **56ms** |
| API calls made inside sandbox | 1        |
| Pods analyzed                 | 11       |
| Tool calls seen by the LLM    | **1**    |

## The Code

```js expandable theme={null}
async () => {
  const clusterId = "cls_abc123"; // resolved by the agent from conversation

  const allPods = await platform.request({
    method: "GET",
    path: `/v1/clusters/${clusterId}/kube_proxy/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

## Related topics

<CardGroup cols={2}>
  <Card title="Parallel log analysis" icon="file-lines" href="/ai/examples/parallel-log-analysis">
    35,000 log lines across 11 pods in 506ms. Same fan-out approach.
  </Card>

  <Card title="Security audit" icon="shield-halved" href="/ai/examples/security-audit">
    Includes per-container resource limit checks.
  </Card>

  <Card title="Cross-cluster comparison" icon="code-compare" href="/ai/examples/cross-cluster-comparison">
    Extend resource audits across your entire fleet.
  </Card>

  <Card title="Code Mode in action" icon="bolt" href="/ai/code-mode-in-action">
    How in-sandbox computation avoids flooding the context window.
  </Card>
</CardGroup>
