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

# Code Mode in Action

> See how agents compose complex multi-step operations in a single tool call

[Code Mode](/ai/platform-mcp#why-code-mode) replaces hundreds of individual MCP tools with just two: `search` and `execute`. The agent writes JavaScript that runs in a sandboxed V8 isolate — and that code can fan out dozens of internal API calls, run them in parallel, aggregate results, and return a summary. All in one tool invocation.

## Why It Works

LLMs have seen millions of lines of real-world JavaScript but only contrived tool-calling examples. Writing `Promise.all()` with a `.map()` is natural for them — chaining individual tool calls is not.

The sandbox provides:

* **`cnap.request()`** — typed API client with auth injected server-side
* **Full JavaScript** — `Promise.all`, `Array.map`, `Date.now()`, error handling, any logic the agent needs
* **Up to 50 API calls** per execution — enough for complex multi-step workflows
* **No network escape** — requests only reach the CNAP API, never the open internet

The agent's code filters, aggregates, and formats data inside the sandbox before returning — so only the summary hits the context window, not raw API responses.

## Examples

<CardGroup cols={3}>
  <Card title="Parallel Log Analysis" icon="file-lines" href="/ai/examples/parallel-log-analysis">
    35,000 lines across 11 pods in 506ms — parallel fan-out with in-sandbox aggregation
  </Card>

  <Card title="Resource Audit" icon="gauge" href="/ai/examples/resource-audit">
    CPU/memory capacity report in 56ms — unit parsing and computation from a single API call
  </Card>

  <Card title="Security Audit" icon="shield-halved" href="/ai/examples/security-audit">
    9 security checks across 7 parallel API calls — then auto-remediates with network policies
  </Card>

  <Card title="Incident Debugging" icon="bug" href="/ai/examples/incident-debugging">
    Full SRE triage — pod health, events, error logs, rollout history — adapting at each step
  </Card>

  <Card title="Database Operations" icon="database" href="/ai/examples/database-operations">
    Run SQL queries, check Redis memory, chain diagnostics — all through exec
  </Card>

  <Card title="Cross-Cluster Comparison" icon="code-compare" href="/ai/examples/cross-cluster-comparison">
    Compare versions, images, and CRDs across your entire fleet in one call
  </Card>

  <Card title="CRD Discovery" icon="magnifying-glass-chart" href="/ai/examples/crd-discovery">
    Discover, inspect, and interact with custom resources the agent has never seen
  </Card>
</CardGroup>

## What Agents Can Do

| Prompt                                    | What happens                                                  |
| ----------------------------------------- | ------------------------------------------------------------- |
| "Count log lines across all pods"         | Parallel log fetches, line counting, sorted summary — 506ms   |
| "Audit resource requests and limits"      | CPU/memory unit parsing, per-pod aggregation — 56ms           |
| "Audit the cluster for security"          | 9 checks across pods, RBAC, secrets, network policies         |
| "Create network policies for my installs" | Discovers ports, generates and applies policies per namespace |
| "Why is my app returning 500s?"           | Adaptive triage: pod health → events → logs → rollout history |
| "What's the largest table in Postgres?"   | Runs `psql` inside the container via exec                     |
| "Are my clusters on the same version?"    | Parallel fan-out across all clusters, drift detection         |
| "What CRDs are installed?"                | Discovers custom APIs, reads schemas, queries instances       |

Each row is one or two tool calls. The agent writes the code, the sandbox executes it, and only the results reach the context window.

## Related

<CardGroup cols={2}>
  <Card title="Platform MCP" icon="server" href="/ai/platform-mcp">
    How Code Mode works and the two tools it exposes
  </Card>

  <Card title="Kubernetes Access" icon="dharmachakra" href="/ai/kubernetes-access">
    Kube API proxy, pod logs, and command execution
  </Card>
</CardGroup>
