In the rapidly evolving landscape of Large Language Models (LLMs), the transition from simple chatbots to autonomous "agents" is defined by a single, critical capability: the ability to take action in the real world. Whether it is querying a SQL database, invoking a REST API, or performing complex financial reconciliations, an AI agent is only as powerful as its "action primitive."
As developers move beyond experimental prototypes, they face an architectural fork in the road: should the agent rely on Tool Calling—the industry-standard method of discrete, model-driven function invocation—or should it shift toward Code Execution, where the model generates and runs entire scripts to achieve its objectives? This decision is not merely a matter of developer preference; it is a fundamental choice that dictates the cost, latency, reliability, and security of an AI application.
The Core Problem: Context Bloat and Orchestration Failure
To understand why this choice matters, consider a common enterprise scenario: auditing employee travel expenses. Imagine an agent tasked with identifying which of twenty employees exceeded their Q3 travel budget. To solve this, the agent must retrieve thousands of individual line items—flights, hotels, and meals—and compare them against specific, level-dependent budget caps.
If designed using traditional Tool Calling, the agent would be forced to loop through each employee, making individual API calls for every person. Each result returns fifty to a hundred line items, all of which must be pushed back into the model’s context window. This approach forces the LLM to process over 2,000 line items and roughly 50KB of raw data—data the model does not need to "read" or "understand" in a linguistic sense. It simply needs to calculate a sum. This "context bloat" is the silent killer of agentic performance, driving up costs and introducing unnecessary latency.
Defining the Action Primitives
Tool Calling: The Discrete Request-Response Model
Tool calling is the mechanism that brought AI agents into the mainstream. Mechanically, it functions as a highly structured conversation. The model is provided with a schema defining available functions. When the model decides to act, it generates special tokens—a "tool call" signal followed by a JSON payload. The host application intercepts these tokens, pauses the model’s generation, parses the JSON, executes the code, and injects the result back into the prompt as a simulated user message.
This architecture is prized for its auditability. Because every action is a discrete event, developers can log, monitor, and verify every step the agent takes. It is inherently safe, as the model never "decides" how to perform the logic; it only decides which pre-defined function to trigger.
Code Execution: The Programmatic Paradigm
Code execution represents a significant shift. Instead of treating the LLM as a controller that directs individual steps, the developer provides the model with an environment where it can write and execute its own programs (typically in Python or TypeScript).
Under the "Programmatic Tool Calling" pattern, the model doesn’t just request an action; it writes a script to perform a series of actions, loops, or complex data transformations. The intermediate results of these operations are handled within a sandboxed environment, and only the final, distilled output is returned to the model. This keeps the agent’s context window clean and focused, shifting the burden of orchestration from the model’s reasoning engine to the execution sandbox.

Chronology: The Evolution of Agentic Action
The trajectory of these primitives highlights how quickly the industry is maturing:
- 2023: The rise of LangChain and early "ReAct" (Reason + Act) patterns, where models were forced to output chains of thought and tool calls iteratively.
- 2024: The publication of the CodeAct paper, providing empirical evidence that agents performing actions through executable code (as opposed to JSON-based tool calls) outperformed their counterparts on complex, multi-step tasks by up to 20%.
- November 2025: Anthropic’s release of advanced "Programmatic Tool Calling" features, formalizing the ability for models to call specific tools from within sandboxed code, effectively merging the safety of tool definitions with the efficiency of code-based orchestration.
Supporting Data: Efficiency and Accuracy
The shift to code execution is backed by rigorous benchmarks. Anthropic’s internal studies demonstrate that moving from traditional tool-calling to code-based orchestration can reduce token usage on complex research tasks by nearly 40%. In some extreme cases, such as the migration of a Google Drive-to-Salesforce data workflow, token consumption dropped from 150,000 to 2,000—a massive 98.7% reduction.
More importantly, accuracy does not suffer; in fact, it often improves. On the GAIA benchmark—a rigorous test of agentic reasoning—agents utilizing programmatic code execution saw accuracy gains from 46.5% to 51.2%. By offloading mathematical and logical aggregation to the execution environment, the model is freed from the risk of hallucinating or miscalculating intermediate values in its "head."
Implications for Development and Infrastructure
Choosing between these primitives has deep implications for the engineering lifecycle:
- Cost and Latency: Tool calling is often "chatty." Every round-trip to the model involves network latency and per-token costs. For tasks requiring aggregation, code execution is almost always more cost-effective.
- Infrastructure Requirements: Code execution requires a secure, sandboxed runtime (such as E2B or Docker containers). If a team lacks the infrastructure to run untrusted code safely, the "cost" of building that security might outweigh the efficiency gains.
- Debugging and Auditability: Tool calls are easy to debug—you can see exactly what the model asked for in the logs. Debugging a script written by an LLM that failed halfway through is significantly more complex, requiring more sophisticated observability tools.
The Hybrid Reality
It is a mistake to view this as a binary choice. The most robust production agents currently in use operate on a hybrid model. They utilize standard tool calling for high-intent, single-shot actions (e.g., "What is the weather in London?") because the overhead of spinning up a sandbox for a trivial task is inefficient. However, the same agent will seamlessly pivot to code execution for complex, data-heavy tasks (e.g., "Compare the Q3 travel expenses of these 50 employees and summarize the outliers").
As Anthropic and other providers continue to integrate these features, the "primitive" is becoming less of a developer-defined constraint and more of an automated capability. Modern agent frameworks are increasingly allowing the model to decide which path is most efficient for the task at hand.
Conclusion: The Path Forward
The choice between tool calling and code execution is an architectural decision that defines the scalability of your AI product. While tool calling offers the comfort of predictability and simplicity, code execution offers the power of efficiency and computational scale.
For the modern developer, the goal is not to master one or the other, but to master the judgment of when to deploy each. By understanding the mechanical differences—specifically, whether your agent needs to reason over intermediate results or simply needs to produce a final, summarized output—you can build agents that are faster, cheaper, and significantly more accurate. The future of AI agency is not in having the "best" model, but in having the best architecture to harness that model’s potential.

