Skip to content

What code mode is ​

Ordinary tool use makes a model do the work one call at a time. Ask it to finish fifty tasks and you get fifty round trips, with every intermediate result passing back through the model.

Code mode flips that. The model writes a program instead of a call. The program loops, filters, and chains at machine speed inside a sandbox, and only the final answer comes back. The model is asked once.

The sandbox cannot reach your database, your network, or your secrets. It gets exactly one function, and that function goes back to Go, which checks a registry before letting anything run.

The shape of it ​

Two things come out of the registry, and keeping them apart is the whole security story. Both are narrowed per request to the roles or permissions your app says the caller holds, whether those are fixed in code or created by an admin at runtime. Search decides what the model knows about. The allowlist decides what may actually run. A program that invents a capability name is rejected by a map lookup, no matter how convincing the prompt was.

What you write, what you get ​

You write protobuf service definitions with two required annotations (roles and permissions are optional). Everything else is generated or configured.

proto
service TodoService {
  option (codemode.namespace) = "todos";

  rpc Complete(CompleteRequest) returns (CompleteResponse) {
    option (codemode.description) = "Marks a todo item as completed by ID.";
  }
}

From that, a model can be handed a working program to write:

js
const { todos } = codemode.execute("todos.list", {});
const pending = todos.filter(todo => !todo.completed);
codemode.execute("todos.complete", { id: pending[0].id });
return "completed " + pending[0].title;

Your application never changes. It validates its own input, enforces its own permissions, and owns what "complete" actually means.

Four swappable parts ​

Each is an interface with more than one implementation. Choose one in code, or let the matching *FromEnv constructor read it from an environment variable. Nothing else in the system knows which is in play.

PieceInterfaceOptionsVariable
Searchcodemode.SearchFunccapability.Keyword (no database), or a capability.Store: sqlite-vector, pgvectorCAPABILITY_DB
Modelllm.ClientClaude, OpenAI + compatibleLLM_PROVIDER
Sandbox languagesandbox.RunnerJavaScript, Starlark, LuaSANDBOX_ENGINE
Where programs runsandbox.Runnerin-process, codemode-worker, a containerSANDBOX_ISOLATION

The sandbox engine also writes its own prompt, so choosing one chooses the language the model is asked to write in — and nothing upstream notices.

Run it ​

With no keys and no database, everything still runs: search falls back to keywords and a fixed program stands in for the model, so you can watch the whole path without credentials.

sh
make demo-todo

With a model actually writing the program:

sh
export ANTHROPIC_API_KEY=...
go run ./examples/todo -request "finish anything about docs"

# Or the complete one: web UI, SQLite, per-user scoping, charts from a prompt.
# It talks to any OpenAI-compatible endpoint.
export OPENAI_API_KEY=... OPENAI_BASE_URL=https://api.deepseek.com/v1 OPENAI_MODEL=deepseek-chat
go run ./examples/taskreport -serve :8080

Where to go next ​

Development and production run the same code

Programs run in your process by default, which is what you want while building. For users you do not control, set SANDBOX_ISOLATION=process and every program runs in a separate codemode-worker, or a container per run for many tenants. See Development and production.