Why smolagents bets on code as action and what changes in a multi-agent setup
Code as action collapses multi-call sequences into one turn; in multi-agent, ManagedAgent turns sub-agents into callable functions inside the supervisor's generated code, with a mandatory sandbox.
Imagine ordering at a counter where you have to step away and come back for every item: ask for fries, leave, come back for a drink, leave, come back for a burger. Slow. Now imagine you can write a one-line shopping list and the cook reads the whole thing at once. That is what code as action does. The agent writes a tiny program that says 'do A, then B, then if B fails do C', and the framework runs the whole script in one turn. In a team setting, the same trick lets a leader write code that calls each teammate by name, like calling functions, so coordinating teammates feels like programming rather than passing notes.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
An agent's 'action space' is the set of operations it can emit in one model turn. The mainstream choice is a JSON tool call: a single structured object naming a tool and its arguments, executed by the framework, with the result fed back as the next message. smolagents picks a different action space: a small Python program. That single design choice changes how agents compose tool calls, how long their traces are, and how multi-agent supervisors orchestrate sub-agents.
The question is testing whether you understand the design choice and its multi-agent consequences, not just whether you have heard of smolagents. The senior signal is articulating that code as action enables intra-turn composition and that the multi-agent payoff is supervisor as program via ManagedAgent.
One-line summary: action space matters. JSON is one tool per turn; code is a program per turn; in multi-agent, code lets the supervisor call sub-agents like functions.
What code as action changes for a single agent
The contrast with JSON tool calls
A JSON-tool-call agent emits one call per turn:
turn 1: {tool: read_file, args: {path: a.py}}
turn 2: {tool: read_file, args: {path: b.py}}
turn 3: {tool: diff, args: {a: <result1>, b: <result2>}}
Three turns. Three model calls. Three context replays.
A code-action agent emits one block:
a = read_file('a.py')
b = read_file('b.py')
result = diff(a, b)
One turn. One model call. One execution. The framework runs the block in a sandbox and feeds the resulting values (and any error) back as the next observation.
Intra-turn composition
The block can contain loops, conditionals, exception handling, and variable reuse. A JSON sequence cannot. For task shapes that are naturally programs over tools (process every file in a list, fetch a URL with retry, run an analysis with branching), this is a real efficiency win.
Token economics
Each turn carries a fixed per-turn overhead: system prompt, context replay, prompt template. Collapsing N calls into one saves (N-1) of those overheads. On short agent loops the saving is marginal. On long multi-tool tasks the saving compounds and shows up in both cost and latency.
Empirical evidence
Wang et al. (CodeAct, 2024) measured improvements on agent benchmarks when moving from JSON tool calls to executable code actions. The result has been replicated in multiple follow-ups through 2024-2025.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
from smolagents import CodeAgent, ManagedAgent, HfApiModel
model = HfApiModel()
research_agent = CodeAgent(
tools=[web_search, read_url],
model=model,
)
managed_research = ManagedAgent(
agent=research_agent, name="research_agent",
description="Search the web and summarise findings.",
)
supervisor = CodeAgent(
tools=[],
managed_agents=[managed_research],
model=model,
)
# Supervisor writes Python that calls research_agent("...")
supervisor.run("Summarise the 2026 multi-agent framework landscape.")Real products, models, and research that use this idea.
- Hugging Face smolagents documentation walks through a research and summarise example using ManagedAgent.
- CodeAct (Wang et al., 2024) is the academic paper that established the empirical case for code as action over JSON tool calls.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does intra-turn composition affect token cost in practice?
Per-turn cost has a fixed component (prompt template, system message, context replay). Collapsing N tool calls into one code block saves (N-1) of those fixed components. For long agent loops on multi-step tasks the saving is significant; for one or two call tasks it is negligible. Measure on your workload.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Running code-action agents without a sandbox. The 'agent writes Python' design is only safe when execution is isolated; trust without isolation is the failure mode.
60 second bullets to scan on the way to the call.
What code as action means as an action-space choice
How intra-turn composition reduces model turns and per-turn overhead
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.