Code Mode: A New Way to Call Tools and do Data Tasks With 10% of the Tokens A blog post published on the toolkernel GitHub repository argues that agents should replace multi-tool JSON calling with a single "run this code" tool, citing an Anthropic example in which presenting MCP tools as code files cut an agent's usage from about 150,000 tokens to about 2,000. In the post's worked example, 24,000 spreadsheet rows (about 8.5 MB) stay in a Python variable and only the computed EMEA revenue figure, 15960536.65, returns to the model — 149 bytes of context. The post traces the approach to CodeAct (2024), Hugging Face smolagents' CodeAgent, Cloudflare's 2025 "Code Mode" for TypeScript in Workers isolates, and Anthropic's "Code execution with MCP" and programmatic tool calling. Ask an AI agent “What was our EMEA revenue last quarter?” and look at what actually happens. The agent calls a tool like read table "sales 2025.xlsx" . The tool returns 24,000 rows. All 24,000 rows go into the model's context , about 8.5 MB of text. The model then reads all of it, as tokens, to add up one column. That’s how tool calling works in most agents today, MCP included: the tool’s result goes straight into the conversation. That’s fine for “what’s the weather?”. For spreadsheets, CSV exports, log files, long web pages and API responses with thousands of records, it costs a lot of money, runs slowly, and is often wrong. Models are bad at adding up 2,000 numbers by reading them. This post covers: The fix is simple to state. Instead of giving the model 20 tools and putting each result into its context, give it one tool: “run this code” . The other tools become functions the code can call. rows = read table path="sales/sales 2025.xlsx", sheet="Orders" "rows" 24,000 rows stay in Pythonsum r "revenue" for r in rows if r "region" == "EMEA" and r "order date" 5:7 in "07", "08", "09" The 24,000 rows land in a Python variable. Only the last line’s value goes back to the model: 15960536.65. 8.5 MB of data, 149 bytes for the model to read. The arithmetic is done by Python, which is good at it, instead of by the model, which isn't. This is not my idea, and it’s not new. A short history: Who What How it works------------------------------------------------- --------------------------------------------------------------------- -------------------------------------------------------------------------------------CodeAct research paper, 2024 Agents that act by writing Python instead of emitting JSON tool calls Showed that code as the action format works better than JSON on many agent benchmarksHugging Face smolagents CodeAgent An open-source agent framework where the model writes Python that calls toolsCloudflare "Code Mode" 2025 Turns MCP tools into a TypeScript API The model writes TypeScript, which runs in a sandboxed Workers isolateAnthropic, "Code execution with MCP" 2025 Presents MCP tools as code files the agent can browse Their example cut an agent's usage from about 150,000 tokens to about 2,000Anthropic, programmatic tool calling Claude API Claude calls your tools from code it runs in a sandbox Intermediate results stay out of the context They all rest on the same insight: models are trained on huge amounts of code, and code can keep data out of the context. Code can filter, join, loop and add up, and only the result needs to come back. Links are at the end. Things move fast here, so check them for the latest. You don’t need a framework to try this. Here’s a complete, working version. The full file is examples/tiny code mode.py https://github.com/kkfaisal/toolkernel/blob/main/examples/tiny code mode.py . Step 1: the tools are plain Python functions , placed in a dictionary that will be the code’s namespace: php def read csv path: str - list dict : with open DATA / path, newline="" as f: return list csv.DictReader f php def read jsonl path: str - list dict : return json.loads line for line in open DATA / path namespace = {"read csv": read csv, "read jsonl": read jsonl, "list files": list files} Step 2: one tool for the model, run python. It runs the code in that same namespace, so variables survive from one step to the next. It returns what the code printed plus the value of the last line, cut to 2,000 characters: php def run python code: str - str: out = io.StringIO try: tree = ast.parse code last = tree.body.pop if tree.body and isinstance tree.body -1 , ast.Expr else None with contextlib.redirect stdout out : exec compile tree, "