AIToday

Code mode helps smaller AI models call tools reliably

Hacker News14h ago
Code mode helps smaller AI models call tools reliably

Key takeaway

Smaller language models struggle to generate correctly formatted commands for calling external tools like APIs and bash, a limitation that can be worked around by asking them to write code instead. Since these models are already trained on code, they succeed more often—especially when multiple tool calls are needed. Anthropic has adopted this "code mode" approach to reduce token costs, and it may also eliminate the need for expensive retraining of smaller models on tool-calling syntax.

Summaries like this, in your inbox every morning.

Sign up free →

3 Key Points

  • What happened

    Instead of training smaller language models to generate properly formatted tool-call signatures, developers can ask them to write Python or TypeScript code that calls APIs or bash commands, which these models already do well from their training data. Anthropic uses this approach to reduce token spending.

  • Why it matters

    Smaller language models often fail at generating correctly formatted tool calls, a problem code mode sidesteps by leveraging their existing code-writing ability. This removes the need for expensive retraining while reducing the number of tokens (the data units an AI processes) the system consumes.

  • What to watch

    Code execution happens in ephemeral Linux sandbox environments that spin up in under 200ms, adding minimal delay. The tradeoff is the small risk of executing malformed code, which providers mitigate by running each execution in an isolated microVM.

In Depth

Language models can interact with external tools—search APIs, bash commands, databases—in two ways. The traditional approach is to have the model generate a "tool call" signature, a structured command like search_web("query") or bash("ls -l") that directly invokes a function. This method works well for powerful models trained extensively on tool-calling syntax, but smaller language models frequently make mistakes generating these signatures because the training required to teach them proper formatting is tedious, and many models have been released without tool-calling ability altogether.

Code mode flips the problem. Instead of asking a model to output a formatted tool signature, developers ask it to write Python or TypeScript code that calls the same tools. Since smaller models are already well-trained on code from their ingestion of code repositories, they succeed far more often—particularly when a task requires multiple tool calls chained together. The model generates executable code, which is then run in an ephemeral (temporary) Linux sandbox environment to avoid security risks. Execution overhead is minimal; most sandbox providers start up in under 200ms, adding no noticeable delay to inference.

Anthropic has adopted code mode to reduce token spending significantly, a benefit that compounds the primary advantage of improved tool-calling reliability. Microsoft's recent recommendation to use specially trained models for specific tasks creates an additional use case: code mode allows developers to deploy smaller, cheaper models without having to retrain them on tool-calling syntax. The tradeoff is the small risk of executing malformed code, but sandbox isolation contains that risk. The approach thus offers a double benefit for cost-conscious deployments: it works better for smaller models out of the box, and it reduces the total tokens consumed in the process.

Context & Analysis

Code mode addresses a practical gap in smaller language models' abilities. While large language models have been trained extensively to format tool calls correctly, smaller models—which offer cost and latency advantages—often fail at this task because the training required to teach them the syntax is resource-intensive. The insight behind code mode is that these same smaller models are already strong at code generation, a skill they picked up naturally during their training on code repositories. By reframing the problem—asking the model to write executable code that calls tools rather than generate a tool-call signature—developers sidestep the need for retraining while improving reliability, especially in complex scenarios with multiple tool calls.

Anthropic's adoption of code mode signals a shift toward pragmatic architecture choices that balance capability with cost. The approach delivers a second benefit: reducing token consumption, which directly cuts inference expenses. For organizations considering smaller, task-specific models (as Microsoft has recently advocated), code mode removes one of the traditional barriers—the assumption that model specialization requires custom training. Instead, it leverages existing model strengths in a way that aligns with how developers already write and think about code.

FAQ

Why do smaller language models fail at tool calling?
Smaller models often make mistakes generating tool-call signatures because the training required to teach them proper formatting was tedious and many models were released without tool-calling ability. Code mode avoids this by asking models to generate code instead, which they are already good at.
What is the performance overhead of executing code in sandboxes?
Most ephemeral sandbox providers spin up in under 200ms, so the delay from running each code execution in an isolated Linux machine is not noticeable.

Get the latest Large Language Models news every morning

AI-summarized, only the topics you pick — one digest a day via Email, Slack, or Discord.

Free · takes 30 seconds · unsubscribe anytime

Discussion

No comments yet. Be the first to share your thoughts!

Log in to join the discussion

Related Articles

Stay ahead with AI news

Get curated AI news from 200+ sources delivered daily to your inbox. Free to use.

Get Started Free

Free · takes 30 seconds · unsubscribe anytime