What you get
An agent that is connected to many tools spends its context on the catalog and on every result it has to pass along. Code mode changes that job. The agent writes a short program. The program calls the tools. The agent reads the answer, not the pile of rows in the middle.
- The prompt stays small. The agent is not handed hundreds of tool schemas before it starts.
- Middle results stay in the program. A sheet, a file list, or a page of issues does not have to be copied back into the model and out again.
- The model sees the answer you asked for, such as a count and the first few matching rows.
- The program cannot see your key, and it cannot reach the open network. It can only call tools your credential may use.
Anthropic described this as code execution with MCP. Cloudflare runs the same idea and calls it Code Mode. AnyRouter exposes it as one opt-in URL on the gateway you already use.
One example
You ask the agent to read a sheet and tell you how many rows are still pending, plus the first five of those rows. The sheet has two thousand rows.
Without code mode, the agent calls the sheet tool, receives all two thousand rows in its context, then writes the pending ones back out by hand. That copy is slow, it costs tokens twice, and a row can get dropped while the model is transcribing.
With code mode, the agent writes this instead. The sheet never leaves the program. The model sees one log line and five rows.
const rows = await codemode.call("sheets_get", { sheetId: "abc" })
const pending = rows.filter((row) => row.Status === "pending")
console.log("pending " + pending.length)
return pending.slice(0, 5)What comes back is the log and the return value, not the sheet.
{
"logs": ["pending 120"],
"result": [
{ "id": "row-4", "Status": "pending" },
{ "id": "row-9", "Status": "pending" }
]
}Why this is cheaper
A normal MCP client pays in two places. First it loads every tool definition into the prompt, before the model has read your question. A workspace with GitHub, Linear, Drive, and a few more servers can put hundreds of schemas there. Second, each tool result comes back through the model so the model can paste it into the next call. The sheet above would be copied in full, then copied again as the pending rows.
Code mode skips both copies. The agent looks a tool up when it needs it, writes the filter in JavaScript, and only the filtered value returns. Models are already good at writing that kind of code. They are less reliable at transcribing a long JSON blob from one tool into the next.
The same program can call more than one tool. Fetch the sheet, then look up the owner of each pending row, then return a short list of names. The join happens in the program. The model does not sit in the middle of it.
How the agent does it
Code mode lists two tools. Everything else stays off the list until the agent asks for it.
| Tool | What the agent does with it |
|---|---|
| search_tools | Looks up a tool by a few words or by its exact name. Each match includes a TypeScript signature, so the agent knows the arguments. |
| execute | Runs the program. The program calls tools with codemode.call(name, args). console.log and the return value come back. |
A lookup for the sheet tool comes back like this. The agent then writes the program above and sends it to execute.
{
"name": "sheets_get",
"description": "Read a sheet.",
"typescript": "/** Read a sheet. */\ndeclare function sheets_get(input: {\n /** The sheet id */\n sheetId: string\n}): Promise<unknown>"
}The program runs in a fresh sandbox. It has no network of its own. codemode.call comes back to the gateway, which runs the tool with your key and your connections. A tool your key cannot use was never registered, so the program cannot call it. If the program returns an email address, the model sees that address. Filter in the program, and do not return a secret you do not want in the transcript.
Turn it on
Code mode is off until you opt in. Each successful run costs $0.01 of workspace credit. A run that fails is not charged. anyr still connects search mode, which is the short tool list plus search_tools and call_tool. Search mode does not run a program and does not cost this fee.
curl -X PATCH https://anyrouter.dev/api/v1/mcp/config \
-H "Authorization: Bearer $ANYROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "code": true }'The dashboard MCP page has the same switch, next to auto-connect. After it is on, point a client at the code URL. Until then, that URL answers 403.
claude mcp add --transport http anyrouter-code \
"https://anyrouter.dev/api/v1/mcp?tools=code" \
--header "Authorization: Bearer $ANYROUTER_API_KEY"GET /api/v1/mcp/config returns that URL as code.url. Keys and scopes are the same as the rest of the gateway, in the MCP reference.
