Skip to content
mentoor

Topic 2 of 4 · 5 min read

Programmatic tool calling and code mode

Goal: Rewrite a sequence of individual tool calls as a short script that calls the same tools and returns only the summary.

Programmatic tool calling gives the model your tools as functions inside a script, so one generated program can call many tools and return only a result. Several products ship this idea under different names. Claude has programmatic tool calling. Cloudflare calls its approach Code Mode, where MCP tools are presented as a typed API the model writes code against. Hugging Face smolagents agents write Python as their action by default.

Check yourself · 1 of 3

What does programmatic tool calling change about how a model uses your tools?

Choose one answer

Why it matters

Plain tool use asks the model to produce a JSON call, wait, and read the reply. That works for a handful of tools. With dozens of tools, the definitions alone fill the context, and chained calls pay a model turn each. Code lets the model import only what it needs, compose tools with ordinary loops and conditions, and pass data between calls without it passing through the model at all.

How it works

  1. Tools become functions. Each tool gets a name, typed arguments and a return type, often generated from its schema.
  2. The model writes a script. It calls the functions, loops, filters and prints a summary.
  3. A runtime executes the script. Each function call is routed to the real tool, which your code still controls.
  4. Only the output returns. Intermediate results stay in the script's variables.

The routing step matters. The script does not get raw network access. It gets the tools you chose, and each call can still be checked, logged and denied.

Before and after

Task: find the three largest open deals and email the owner of each.

As separate calls, the model fetches the deal list into context, reads it, then makes three owner lookups and three email calls, one turn each.

As a script:

deals = crm.list_deals(status="open")
top = sorted(deals, key=lambda d: d["amount"], reverse=True)[:3]
for d in top:
    owner = crm.get_user(d["owner_id"])
    mail.send(owner["email"], f"Deal {d['name']} needs a review")
print([(d["name"], d["amount"]) for d in top])

The full deal list never enters the context. The model sees three names and amounts. Note the email calls are side effects, so a real setup would ask for approval before mail.send runs.

Choosing between styles

Use plain tool calls for one or two steps, for actions a human must approve one at a time, and when your model or host does not support code. Use programmatic calling when many calls are chained or the data is large. Check each product's current documentation for what it supports, since limits differ.

What goes wrong

  • Exposing every tool. A script with access to deletion and payment functions is a bigger risk than the task needs.
  • Untyped functions. Without argument types the model guesses field names, and the script fails at run time.
  • Side effects inside loops. A mistake in a loop repeats 200 times before anyone looks.

Examples

Three calls turned into one script

Before: the model calls get_ticket, reads it, calls get_customer, reads it, then calls get_orders. After: one script fetches all three, picks the orders from the last 30 days and prints a four-line summary. Same tools, one model turn for the work and one to read the result.

Gating a side effect

A script for refunds exposes orders.find freely but wraps payments.refund so each call pauses for human approval and logs the amount. The model still writes one script, and the dangerous call is not left to the loop.

Key points

  • Programmatic tool calling presents tools as functions inside a script the model writes.
  • Claude programmatic tool calling, Cloudflare Code Mode and smolagents all apply the same idea with different runtimes.
  • Intermediate results stay in script variables, so only the summary returns to the model.
  • The runtime routes each function call to a real tool, where it can still be checked, logged or denied.
  • Plain tool calls remain the better choice for one or two steps and for actions that need approval each time.

Common mistakes

  • Exposing every tool to the script, including destructive ones the task does not need.
  • Leaving function arguments untyped, so the model guesses field names.
  • Putting an irreversible call inside a loop with no approval or limit.
  • Treating the products as identical when their supported languages and limits differ.

Check yourself · 2 of 3

In a script-based setup, where should an irreversible call such as issuing a refund be controlled?

Choose one answer

Check yourself · 3 of 3

When is a plain one-call-per-turn tool call still the better choice?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic