Two ways to drive tools with an LLM. Classic loop: model emits one JSON tool call, you run it, stuff the whole result back into chat, repeat. Every hop taxes the context window. Code-implemented tool calls flip that: the model writes a short program that can call several tools, a sandbox runs the program, and only the final answer comes back to the model. That’s the core of Mistral’s freshly issued US patent on code implemented tool calls – and it’s why this is blowing up on Hacker News right now.
If you only remember one thing, remember this: multi-step agent work is usually a control-flow problem, not a chat problem. Putting control flow in code (loops, branches, parallel awaits) beats stuffing every intermediate JSON blob into the prompt. Below is what the patent actually describes, how it maps to patterns you can run today with Mistral’s Agents and function calling, and where people will get burned.
What “code implemented tool calls” actually means
US 12,670,045 B1 (inventor Gabriel Vergnaud, assigned to Mistral AI, filed March 4, 2026 – USPTO gazette entry) describes a five-beat method:
- Server gets a user request that needs one or more tool calls.
- An LLM generates a code block (TypeScript in the write-up) that wraps those calls.
- The server runs that block inside a sandbox.
- Hit a pending client-side tool (local files, UI, device APIs)? Sandbox pauses. Server ships the call to the client. Client returns a result.
- Server resumes, substitutes the result, finishes the block, and hands the LLM only the final result – not the intermediate payloads.
Why bother? Classic discrete tool loops dump every intermediate state into the model context. Tokens burn. Responses slow down. Mixed client/server tools get ugly. The fix in the patent text is a resumable sandbox plus an evaluation stack that replays the block when new client results arrive, capturing non-deterministic bits so replay stays stable (full description on Justia).
Pro tip: Treat the code block as a temporary mini-orchestrator. The LLM plans once in a language it’s good at; the runtime does the await/pause/resume dance so the model doesn’t babysit every hop.
HN lit up fast on this filing (~100+ comments on the thread). People pointed at the same family of idea in Cloudflare’s Code Mode (Sept 2025): MCP tools become a TypeScript API, the model writes code against it, sandbox runs it, final logs/results come back. Same bet – models write code more reliably than one-off tool JSON they barely saw in training.
Step-by-step: run the spirit of it with Mistral today
You don’t need a court filing to use the pattern. Mistral already ships function calling and a sandboxed code_interpreter on the Agents / Conversations path. Here’s a beginner path that mirrors “code plans tools, sandbox executes, model sees the end state.”
1. Classic function calling baseline (for contrast)
JSON-schema tools in, tool calls out, you run the functions, tool messages go back. That’s the loop in Mistral’s function calling docs (as of mid-2026). tool_choice: auto, any, or none. Parallel calls optional.
tools = [{
"type": "function",
"function": {
"name": "get_order",
"description": "Fetch one order by id",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"]
}
}
}]
# model may return tool_calls → you run get_order → append role=tool result → model answers
That works. It’s also exactly the discrete loop the patent says bloats context when you chain five dependent calls.
2. Agents + code_interpreter when the job is multi-step
Built-in sandboxed code tool. Create an agent, start a conversation, let it compute. (API surface gotcha lives in the pitfalls section below – read it before you wire Chat Completions.)
from mistralai import Mistral
import os
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
agent = client.beta.agents.create(
model="mistral-medium-latest",
name="Coding Agent",
description="Runs code when analysis needs it",
instructions="Use the code interpreter when you must compute or transform data.",
tools=[{"type": "code_interpreter"}],
completion_args={"temperature": 0.3, "top_p": 0.95},
)
resp = client.beta.conversations.start(
agent_id=agent.id,
inputs="Compute the first 20 Fibonacci numbers and return only the list."
)
print(resp)
Response shape you’ll see in practice: a tool.execution entry with the code and code_output, then a final assistant message. Practical cousin of “block runs in a sandbox; model consumes the outcome.” Agents themselves shipped with connectors for code execution, web search, image generation, document library, plus MCP and stateful conversations (Mistral Agents API announcement, May 27, 2025).
3. Hybrid: your tools + code as the planner
Full patent flavor needs custom tools plus orchestration code. JSON tools handle side effects you own (DB, HTTP, client-local FS). One glue path – a run_plan-style tool or the interpreter – does map/filter, branch, retry, aggregate. Host loop injects final summaries when you can get away with it.
Sketch the system prompt so the model emits one small script of calls instead of five chat turns:
SYSTEM = """When multiple tools are needed, write a short plan as code-like steps
(tool names + args + how results feed the next step). Prefer one plan over
many back-and-forth calls. Return only the user-facing answer after tools run."""
Your runtime is the sandbox: execute steps, pause for client-only ops, substitute results, then ask the model to narrate. Same sequence as the patent without pretending you’ve reimplemented every claim. One scope note: claim 1 centers on server sandbox + pause + transmit pending call + substitute result. Pure client-side “LLM writes TypeScript against tools” with no that split may sit outside the claimed method – useful if you’re reading HN scope debates, not a license to ignore your own security model.
Common pitfalls when you try this
Replay cost is real. The patent’s evaluation stack replays the block from the start when a new client result lands so execution stays deterministic. Fine for short plans. Painful if every client hop re-runs a heavy preamble – multi-step client round-trips pick up O(n) re-execution inside the sandbox. Keep generated code tiny and pure.
Wrong API surface. Paste code_interpreter into Chat Completions and nothing runs. Per Mistral’s Code Interpreter docs (as of mid-2026), that built-in tool is Agents/Conversations only. Function calling on chat still works; the interpreter path does not.
Debugging goes dark. Intermediate tool payloads never hit the LLM – that’s the point – so a wrong final answer leaves you without the usual message-trace. Log sandbox stdout, tool IDs, and substituted values yourself or you’ll stare at a polite but empty final reply.
Client vs server boundary. Park tools on the client only when they truly need the client (local files, browser, hardware). Everything else stays server-side so you don’t pay a network pause per call. Errors during sandbox/tool execution can be classified and fed back so the model regenerates corrected code; still log them.
Funny how the same industry that mocks software patents still rediscovers the same orchestration idea every six months under a new name. Makes you wonder whether the real scarce resource is shared vocabulary, not novelty.
How this compares with alternatives
| Approach | What the model sees | Best for | Watch-out |
|---|---|---|---|
| Classic tool JSON loop | Every call + full results | 1-2 simple tools | Context bloat on chains |
| Mistral Agents + code_interpreter | Code + outputs inside agent run | Math, plots, transforms | Not on Chat Completions |
| Patent-style pause/resume sandbox | Only final code result | Mixed client/server tools | Replay cost, host complexity |
| Cloudflare Code Mode / MCP→TS | Typed API + one execute tool | Huge tool catalogs | Need a secure isolate |
~1,000 tokens vs ~1.17M. That’s the Cloudflare Code Mode MCP example for exposing a fat API as search/execute instead of dumping every tool schema (their Feb 2026 write-up). Your mileage depends on tool count and result size. Direction matches the patent’s token argument.
Practically: pick the stack you already run, implement the pattern, and don’t wait for anyone’s exclusive license fairy tale.
FAQ
Does the Mistral patent block me from using code-mode tool calling?
No outsider can promise that. Build on open patterns – your sandbox host, published Code Mode ideas, standard function calling – and if you ship a commercial US agent platform, talk to counsel. Not legal advice.
When should I stick with normal function calling instead?
Single tool, tiny result, no branching. “What’s the status of order T1001?” One schema, one call, done. A code sandbox there is pure latency tax. Escalate when you hit three or more dependent calls, or fat intermediate JSON you never want the model to re-read.
Is Mistral’s code_interpreter the same as the patented method?
Common mix-up. No – related family, not a 1:1 map. The interpreter runs Python in an isolated container and folds code/output into an agent conversation; great for compute, and (again) only on the Agents/Conversations surface. The patent text is about LLM-generated orchestration code, a resumable sandbox, client-delegated pending calls, result substitution, and returning only the final block result to the LLM. Approximate that with Agents + custom tools + your own pause/resume host even when you skip patented details.
Next action: spin up one Mistral agent with code_interpreter, add a single custom function tool, force a two-step task (fetch → transform in code → answer). Compare token use and latency against the same task as pure multi-turn function calling. That one experiment beats another hour of patent-thread scrolling.