smolagents

Hugging Face CodeAgent vs ToolCallingAgent, code execution, and sandbox executors.

Hugging Face's entry, and a genuinely different paradigm from the other three. Instead of the model requesting a JSON tool call one at a time, smolagents' default CodeAgent has the model write actual Python code that runs in a constrained interpreter.

Core concepts

  1. CodeAgent vs ToolCallingAgent: CodeAgent writes and executes real Python per turn; ToolCallingAgent is the drop-in JSON-tool-calling alternative if you want parity with other frameworks. smolagents Docs
  2. Why code execution is faster for multi-step tasks: A model can loop, branch, and combine several tool calls in one generated code block instead of one round trip per tool call.
  3. The @tool decorator and Args: docstrings: smolagents parses the Args: section of a docstring directly to build the tool schema — a malformed block breaks tool discovery, not just the docs.
  4. Security is not automatic: The default local executor is explicitly documented as not a sandbox — escaping it is expected behavior. Use executor_type="e2b" or "docker" for anything beyond local experimentation.
  5. additional_authorized_imports: Any import a tool needs beyond the safe allowlist must be explicitly declared when constructing the agent.

Why it matters

It's the quickest way to try code-as-action with Hugging Face models, and it forces the sandbox question on day one instead of after launch.

Where people get stuck

  • Running CodeAgent without a real sandbox. That is arbitrary code execution on your machine.
  • Assuming a small library means no ops work. You still need logging, limits and model routing.
  • Using CodeAgent when your tools are few and stable. ToolCallingAgent is simpler to audit.

Try this

Run a ToolCallingAgent with one calculator tool locally. Then read the CodeAgent docs and list the sandbox you'd need before trying it.

Interview question

CodeAgent vs ToolCallingAgent?

CodeAgent writes Python for multi-step logic and needs strong isolation. ToolCallingAgent uses structured tool calls and is easier to audit when the tools are fixed.

Resources

YouTube learning

Core concepts from video walkthroughs:

Threads in the same domain as this chapter — go deeper on iHateReading without leaving the roadmap.

Python AI agent SDKs in 2026: OpenAI vs Pydantic-AI vs smolagents vs Strands cover

Python AI agent SDKs in 2026: OpenAI vs Pydantic-AI vs smolagents vs Strands

Same three-tool agent in four frameworks — pick by execution model and testing needs.

Smallest AI Agent cover

Smallest AI Agent

Minimal file-reading agent to learn LLM tool loops without framework magic.

Agent harnessing: the part of building AI agents nobody talks about cover

Agent harnessing: the part of building AI agents nobody talks about

Loop, tools, memory, sandbox, and tracing live in your code — not the model weights.