Skip to main content
An agent asked to “count log lines across all pods in my cluster” produced this in a single execute call — no sequential tool calls, no raw logs in the context window.
Agent counting log lines across all pods in a single Code Mode execution

The Numbers

MetricValue
Total time506ms
List all pods139ms
Parallel log fetches (11 containers)367ms
Total log lines counted35,498
API calls made inside sandbox12
Tool calls seen by the LLM1
The agent even self-instrumented the timing with Date.now() inside the sandbox.

The Code

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

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

  // Get all pods across all namespaces
  const allPods = await kube("api/v1/pods");

  // Fetch logs for all running containers in parallel
  const results = await Promise.all(
    allPods.items
      .filter(p => p.status.phase === "Running")
      .flatMap(p =>
        p.spec.containers.map(c => ({
          namespace: p.metadata.namespace,
          pod: p.metadata.name,
          container: c.name
        }))
      )
      .map(async ({ namespace, pod, container }) => {
        try {
          const logs = await kube(
            `api/v1/namespaces/${namespace}/pods/${pod}/log?container=${container}`
          );
          const lines = typeof logs === "string"
            ? logs.split("\n").filter(Boolean).length : 0;
          return { namespace, pod, container, lines };
        } catch {
          return { namespace, pod, container, lines: 0, error: true };
        }
      })
  );

  const total = results.reduce((sum, r) => sum + r.lines, 0);
  results.sort((a, b) => b.lines - a.lines);
  return { total_lines: total, per_pod: results };
}

Why This Matters

With traditional MCP tools, this would be 12+ sequential round trips: one to list pods, then one per container to fetch logs. Each round trip adds network latency, and every raw response gets dumped into the LLM’s context window. Code Mode does it in one — the agent’s code filters, aggregates, and formats the data inside the sandbox before returning. Only the summary hits the context window, not 35,000 lines of raw logs.

What the Agent Does

  1. Lists all pods across all namespaces
  2. Filters for running pods, expands to per-container list
  3. Fans out Promise.all() — 11 log fetches in parallel
  4. Counts lines per container, handles errors gracefully
  5. Sorts by line count, returns a clean summary
The heaviest pod (api-proxy, 24,828 lines) took 367ms. Small pods (~10 lines) came back in ~89ms. The parallel fan-out means total time equals the slowest fetch, not the sum.