---
title: "It's context efficient · Tempo AI Platform docs"
description: "How a cell runs as a program next to the tools, so only what it returns enters the model's context."
url: "https://gateway.tempo-dev.workers.dev/docs/guides/code-mode"
---

> Every page of the Tempo AI Platform is Markdown at its URL with .md, or with Accept: text/markdown. The index of every page is https://gateway.tempo-dev.workers.dev/llms.txt, the API https://gateway.tempo-dev.workers.dev/openapi.json and the MCP server https://gateway.tempo-dev.workers.dev/mcp.

# It's context efficient

In Code Mode the model does not call tools one step at a time. It writes a program that calls them, the program runs next to the tools inside the agent, and only what the program returns goes back into the model's context. Twenty reads become one cell, and the files never pass through the model.

[Get your Tempo Key](https://gateway.tempo-dev.workers.dev/key.md) · [How agents run](https://gateway.tempo-dev.workers.dev/docs/guides/swarms.md)

## Architecture

```text
without Code Mode                  with Code Mode
-----------------                  --------------
call 1   read app-01.log           call 1   exec: one program, run in
         <- 2k tokens                       QuickJS inside the agent:
call 2   2k in, read app-02.log               20 x tools.exec_command
         <- 2k more                           count the ERROR lines
  ...                                         return "app-07.log: 412"
call 21  40k in, the answer                 <- 0.2k
                                   call 2   0.2k in, the answer

21 calls, 420k tokens in           2 calls, 0.2k tokens in
```

The same question both ways. Without Code Mode every tool result enters the context and is sent again with each later call; with it, the results stay in the cell and the model reads one line.

The numbers are illustrative: 2k tokens a log, the whole context sent again on each call and no prompt cache. The shape is not: without Code Mode a turn's input grows with the square of its tool calls, and with it the input grows with what the cells return.

## How a cell runs

1. The model has two tools of its own: `exec`, which runs a cell, and `wait`, which waits for a cell that yielded. Everything else the agent can do is a function on `tools` inside the cell.
2. The cell is JavaScript, run as an async module in QuickJS, compiled to WebAssembly, inside the agent's own Durable Object. There is no process to start and no machine to rent for it, and it sits beside the session's storage.
3. Tool calls are async functions. `await tools.exec_command(...)` runs one, `Promise.all` runs many at once, and loops, filters and retries happen in code instead of in model turns.
4. Only output reaches the model: what the cell prints and the value it returns. Intermediate results stay in the cell.
5. A long cell yields after its time budget and keeps running; the model can do other work and come back with `wait`.

**one cell, twenty reads**

```ts
const logs = Array.from({ length: 20 }, (_, i) => "logs/app-" + String(i + 1).padStart(2, "0") + ".log")

const counts = await Promise.all(
  logs.map(async log => {
    const text = await tools.exec_command({ cmd: "cat " + log })
    return [log, text.split("\n").filter(line => line.includes("ERROR")).length]
  }),
)

const [log, errors] = counts.sort((a, b) => b[1] - a[1])[0]
return log + ": " + errors + " errors"
```

## What a cell can call

- **The harness's tools**\
  Codex's `exec_command`, `write_stdin` and `apply_patch`, or Claude's Bash, Read, Write and Edit, on the agent's workspace or on a hand.
- **Web search**\
  Codex's `web__run` and Claude's `WebSearch`, which the platform serves on any model. [Web search](https://gateway.tempo-dev.workers.dev/docs/guides/tools.md#web)
- **Subagents**\
  `spawn_agent`, `send_agent_message`, `wait_agent`, `list_agents`, `interrupt_agent` and `close_agent`, so one cell can start a swarm and collect it.
- **Hands**\
  `tools.hands()` lists the machines attached, and any call that names one, as in `tools.exec_command({ cmd: "uname -a", hand: "laptop" })`, runs there.
- **The platform's tools**\
  Memory, sandboxes, browsers, cards, mail and phone numbers as `tools.mcp__gateway__*`, on the agent's key in its app. [MCP tools](https://gateway.tempo-dev.workers.dev/docs/mcp.md)
- **fetch**\
  The cell's global `fetch` pays a 402 from any MPP service from the agent's key, up to a ceiling a call. [Paying services](https://gateway.tempo-dev.workers.dev/docs/guides/tools.md#fetch)

## What it saves

- **Tokens**\
  A model call sends the whole conversation again. Without Code Mode each tool result becomes part of every later call; with it, a result costs tokens only if the cell returns it.
- **Time**\
  Each model call waits for its first token, about a second on the platform's default model. A cell that makes twenty calls at once replaces twenty model round trips with one.
- **Mistakes**\
  Code that counts, sorts and compares does it exactly, where a model reading twenty files in its context can lose one.

## Cells that survive a restart

Every call a cell makes is journaled in the object's storage before it runs and its result after. When the object restarts in the middle of a cell, the cell replays from the journal: calls that finished answer from it instead of running again, and a call that was in flight on a hand is resumed when the hand is back, so its real output reaches the model. [What a turn survives](https://gateway.tempo-dev.workers.dev/docs/guides/swarms.md#durable)

## Where heavy work goes

QuickJS is an interpreter, meant for gluing tools together rather than for computing. The workspace's shell works on files in the object's own storage. For other programs, a GPU or more CPU, the cell starts a sandbox with `tools.mcp__gateway__compute_create` or runs on one of your machines as a hand. [Compute and storage](https://gateway.tempo-dev.workers.dev/docs/guides/resources.md)

## Go deeper

- [Agents](https://gateway.tempo-dev.workers.dev/docs/agents.md#how): turns, steering and the agent's tools.
- [Machines](https://gateway.tempo-dev.workers.dev/docs/machines.md): running a cell's calls on your hardware.
- [nanocode](https://nanocode.xyz/): the runtime behind the agents.

[<- It's multiplayer](https://gateway.tempo-dev.workers.dev/docs/guides/multiplayer.md) · [It's customizable ->](https://gateway.tempo-dev.workers.dev/docs/guides/personas.md)
